Skip to content

Commit

Permalink
Update DB creation strategy, Dockerfile and build.
Browse files Browse the repository at this point in the history
  • Loading branch information
gencebay-demir committed Dec 1, 2020
1 parent 9fd6877 commit f72e509
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.db
*.exe
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Binaries for programs and plugins
*.exe
*.db
*.dll
*.so
*.dylib
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.11.x
- 1.15.x
install: true
os:
- linux
Expand All @@ -9,12 +9,12 @@ dist:
env:
global:
- GOARCH=amd64
- GO_FOR_RELEASE=1.11
- GO_FOR_RELEASE=1.15
- GO111MODULE=on
before_install:
- go get github.com/mitchellh/gox
script:
- go get -t -v
- go mod download -x
- gox -os="linux darwin windows" -arch="amd64" -verbose
- gox -os="linux darwin windows" -arch="386" -verbose
before_deploy:
Expand Down
38 changes: 23 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
FROM golang:1.9.2 as builder
WORKDIR /go/src/github.com/gencebay/httplive/
RUN go get -d -v github.com/gin-gonic/gin
RUN go get -d -v github.com/boltdb/bolt
RUN go get -d -v github.com/urfave/cli
RUN go get -d -v github.com/gorilla/websocket
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM nginx:alpine
ENV APPDIRPATH /go/src/github.com/gencebay/httplive/
FROM golang:1.15.5-alpine as builder

WORKDIR /src/app

ENV CGO_ENABLED=0

COPY . .

RUN GOOS=linux go build -a -installsuffix cgo -o httplive .

FROM alpine:3.7

WORKDIR /src/app

EXPOSE 5003

VOLUME /src/app

ENV GIN_MODE release
RUN apk --no-cache add ca-certificates
WORKDIR ${APPDIRPATH}
COPY --from=builder ${APPDIRPATH}/app .

ENTRYPOINT ["./app"]
COPY --from=builder /src/app/httplive .

RUN chmod +x /src/app/httplive

ENTRYPOINT ["./httplive"]
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Make sure your PATH includes the \$GOPATH/bin directory so your commands can be

--dbpath, -d

Fullpath of the httplive.db with forward slash.
Fullpath of the httplive.db with forward slash. If not present, default db name (httplive-1a.db) will be used.

--ports, -p

Expand All @@ -40,6 +40,12 @@ For httplive application running with port 5003:

this key will be stored in the **bucket 5003**. Therefor if you running app as single port with 5004 you can not access the keys of 5003 port. You can use multi-port host to overcome this situation.

### Docker Build & Run

docker build -t local-httplive .

docker run -it -p 5003:5003 -v httpfs:/src/app local-httplive

### Compiling the UI into the Go binary

go get github.com/jteeuwen/go-bindata/...
Expand All @@ -49,10 +55,6 @@ this key will be stored in the **bucket 5003**. Therefor if you running app as s

Tests

CI Build Integration.

Simple console to display the information of the incoming request under the UI editor. (WebSocket)

Upload a database file from the web interface.

[Watch the video](https://youtu.be/AG5_llcBogk)
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
module github.com/gencebay/httplive

go 1.15

require (
github.com/boltdb/bolt v1.3.1
github.com/gin-contrib/sse v0.0.0-20190125020943-a7658810eb74 // indirect
github.com/gin-gonic/gin v1.3.0
github.com/golang/protobuf v1.2.0 // indirect
github.com/gorilla/websocket v1.4.0
github.com/json-iterator/go v1.1.10 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mitchellh/gox v1.0.1 // indirect
github.com/ugorji/go/codec v0.0.0-20190128213124-ee1426cffec0 // indirect
github.com/urfave/cli v1.20.0
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3 // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
4 changes: 3 additions & 1 deletion lib/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"mime"
"net/http"
"path"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -73,8 +74,9 @@ func (ctrl WebCliController) Backup(c *gin.Context) {
OpenDb()
defer CloseDb()
err := db.View(func(tx *bolt.Tx) error {
var filename string = filepath.Base(Environments.DatabaseFullPath)
c.Writer.Header().Set("Content-Type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+Environments.DatabaseName+`"`)
c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`)
c.Writer.Header().Set("Content-Length", strconv.Itoa(int(tx.Size())))
_, err := tx.WriteTo(c.Writer)
return err
Expand Down
9 changes: 1 addition & 8 deletions lib/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ import (
var db *bolt.DB
var dbOpen bool

// Database ...
var Database = "httplive.db"

// DatabasePath ...
var DatabasePath string

// OpenDb ...
func OpenDb() error {
var err error
config := &bolt.Options{Timeout: 1 * time.Second}
db, err = bolt.Open(Environments.DbFile, 0600, config)
db, err = bolt.Open(Environments.DatabaseFullPath, 0600, config)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -163,7 +157,6 @@ func GetEndpoint(endpointKey string) (*APIDataModel, error) {
return nil
})
if err != nil {
fmt.Printf("Could not get content with key: %s", endpointKey)
return nil, err
}
return model, nil
Expand Down
10 changes: 4 additions & 6 deletions lib/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package lib

// EnvironmentVariables ...
type EnvironmentVariables struct {
WorkingDirectory string
DbFile string
DatabaseName string
DatabaseAttachedFullPath string
DefaultPort string
HasMultiplePort bool
WorkingDirectory string
DatabaseFullPath string
DefaultPort string
HasMultiplePort bool
}

// IPResponse ...
Expand Down
5 changes: 4 additions & 1 deletion lib/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package lib
import "github.com/gorilla/websocket"

// Environments ...
var Environments = EnvironmentVariables{DatabaseName: "httplive.db"}
var Environments = EnvironmentVariables{}

// DefaultDbName ...
const DefaultDbName = "httplive-1a.db"

// DefaultMemory Form data ...
const DefaultMemory = 32 * 1024 * 1024
Expand Down
39 changes: 27 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"

Expand Down Expand Up @@ -44,19 +45,25 @@ func main() {
app.Run(os.Args)
}

func createDb() error {
func createDb(dbPath string, dbPathPresent bool) error {
var err error
var dbfile string
if Environments.DatabaseAttachedFullPath != "" {
if _, err := os.Stat(Environments.DatabaseAttachedFullPath); os.IsNotExist(err) {
log.Fatal(err)

if _, err := os.Stat(dbPath); os.IsNotExist(err) {
var filename string = filepath.Base(dbPath)
if filename == DefaultDbName && !dbPathPresent {
_, err := os.Create(dbPath)
if err != nil {
log.Fatal(err)
}
} else {
if err != nil {
log.Fatal(err)
}
}
dbfile = Environments.DatabaseAttachedFullPath
} else {
dbfile = path.Join(Environments.WorkingDirectory, Environments.DatabaseName)
}

Environments.DbFile = dbfile
Environments.DatabaseFullPath = dbPath

CreateDbBucket()
return err
}
Expand All @@ -72,12 +79,19 @@ func host(ports string, dbPath string) {
}

_, filename, _, _ := runtime.Caller(0)
Environments.WorkingDirectory = path.Dir(filename)
var workdir string = path.Dir(filename)
Environments.WorkingDirectory = workdir
Environments.DefaultPort = port
Environments.HasMultiplePort = hasMultiplePort
Environments.DatabaseAttachedFullPath = dbPath

createDb()
dbPathPresent := false
if dbPath == "" {
dbPath = path.Join(workdir, DefaultDbName)
} else {
dbPathPresent = true
}

createDb(dbPath, dbPathPresent)

InitDbValues()

Expand Down Expand Up @@ -121,6 +135,7 @@ func host(ports string, dbPath string) {
}
}

fmt.Printf("Httplive started with port: %s", port)
r.Run(":" + port)
}

Expand Down

0 comments on commit f72e509

Please sign in to comment.