From f72e5099c322ca35717fb41dd8b8e597aa3c9af9 Mon Sep 17 00:00:00 2001 From: gencebay-demir Date: Wed, 2 Dec 2020 00:47:45 +0300 Subject: [PATCH] Update DB creation strategy, Dockerfile and build. --- .dockerignore | 2 ++ .gitignore | 1 + .travis.yml | 6 +++--- Dockerfile | 38 +++++++++++++++++++++++--------------- README.md | 12 +++++++----- go.mod | 5 +++++ lib/controller.go | 4 +++- lib/db.go | 9 +-------- lib/types.go | 10 ++++------ lib/variables.go | 5 ++++- main.go | 39 +++++++++++++++++++++++++++------------ 11 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..29c2b61 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +*.db +*.exe \ No newline at end of file diff --git a/.gitignore b/.gitignore index fdf0dce..0ba1bb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Binaries for programs and plugins *.exe +*.db *.dll *.so *.dylib diff --git a/.travis.yml b/.travis.yml index d84138b..d793010 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.11.x + - 1.15.x install: true os: - linux @@ -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: diff --git a/Dockerfile b/Dockerfile index c2f9afb..010c584 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file +COPY --from=builder /src/app/httplive . + +RUN chmod +x /src/app/httplive + +ENTRYPOINT ["./httplive"] \ No newline at end of file diff --git a/README.md b/README.md index 95dc903..5dbb553 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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/... @@ -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) diff --git a/go.mod b/go.mod index 46c68d5..6de48f5 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/lib/controller.go b/lib/controller.go index a84610e..6fb3e24 100644 --- a/lib/controller.go +++ b/lib/controller.go @@ -6,6 +6,7 @@ import ( "mime" "net/http" "path" + "path/filepath" "strconv" "strings" @@ -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 diff --git a/lib/db.go b/lib/db.go index bd79a39..c7a5d5c 100644 --- a/lib/db.go +++ b/lib/db.go @@ -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) } @@ -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 diff --git a/lib/types.go b/lib/types.go index 349e8bf..66fe118 100644 --- a/lib/types.go +++ b/lib/types.go @@ -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 ... diff --git a/lib/variables.go b/lib/variables.go index b43ee0d..d386cfd 100644 --- a/lib/variables.go +++ b/lib/variables.go @@ -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 diff --git a/main.go b/main.go index aefdf78..72c387e 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "path" + "path/filepath" "runtime" "strings" @@ -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 } @@ -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() @@ -121,6 +135,7 @@ func host(ports string, dbPath string) { } } + fmt.Printf("Httplive started with port: %s", port) r.Run(":" + port) }