From 179f5ae3bc665b500012e6cfa81233f59ffdd583 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Sun, 4 Sep 2022 00:01:16 -0400 Subject: [PATCH 1/6] Bug fixes and improvements --- .github/dependabot.yml | 10 +++++++++- .github/workflows/gotidy.yml | 4 ++-- .github/workflows/linter.yml | 2 +- .github/workflows/security.yml | 2 +- .github/workflows/test.yml | 6 +++--- Dockerfile | 11 ++++++++--- Makefile | 6 +++++- README.md | 9 ++++++--- go.mod | 2 +- handlers/handlers.go | 5 ++++- static/public/index.html | 4 ++-- static/public/js/app.js | 6 +++--- 12 files changed, 45 insertions(+), 22 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index fe5da08..76770b5 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -3,6 +3,14 @@ version: 2 updates: - package-ecosystem: "gomod" - directory: "/" # Location of package manifests + directory: "/" schedule: interval: "daily" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + - package-ecosystem: "docker" + directory: "/" + schedule: + interval: "daily" \ No newline at end of file diff --git a/.github/workflows/gotidy.yml b/.github/workflows/gotidy.yml index 6f2524e..d3c5ea5 100644 --- a/.github/workflows/gotidy.yml +++ b/.github/workflows/gotidy.yml @@ -15,12 +15,12 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2.3.1 + uses: actions/checkout@v3 - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.13 + go-version: 1.19 - name: Tidy run: | diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index f9bca03..8a0d42d 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Fetch Repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Run Golint uses: reviewdog/action-golangci-lint@v1 with: diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 2d5ad70..a2a3f15 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Fetch Repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Run Gosec uses: securego/gosec@master with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3eab896..13abff3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,15 +4,15 @@ jobs: Build: strategy: matrix: - go-version: [1.14.x, 1.15.x] + go-version: [1.14.x, 1.16.x, 1.19.x] platform: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: - name: Install Go - uses: actions/setup-go@v1 + uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Fetch Repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Run Test run: go test -race \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f5d4923..9eba4b3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Building the binary of the App -FROM golang:1.15 AS build +FROM golang:1.19 AS build # `boilerplate` should be replaced with your project name WORKDIR /go/src/boilerplate @@ -15,7 +15,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app # Moving the binary to the 'final Image' to make it smaller -FROM alpine:latest +FROM alpine:latest as release WORKDIR /app @@ -26,7 +26,12 @@ COPY ./static ./static # `boilerplate` should be replaced here as well COPY --from=build /go/src/boilerplate/app . +# Add packages +RUN apk -U upgrade \ + && apk add --no-cache dumb-init ca-certificates \ + && chmod +x /app/app + # Exposes port 3000 because our program listens on that port EXPOSE 3000 -CMD ["./app"] \ No newline at end of file +ENTRYPOINT ["/usr/bin/dumb-init", "--"] \ No newline at end of file diff --git a/Makefile b/Makefile index b5861a9..7d789ec 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,11 @@ build-no-cache: up-silent: make delete-container-if-exist - docker run -d -p 3000:3000 --name $(project_name) $(image_name) + docker run -d -p 3000:3000 --name $(project_name) $(image_name) ./app + +up-silent-prefork: + make delete-container-if-exist + docker run -d -p 3000:3000 --name $(project_name) $(image_name) ./app -prod delete-container-if-exist: docker stop $(project_name) || true && docker rm $(project_name) || true diff --git a/README.md b/README.md index c5a6544..5134eaa 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ Use the following plugins, in this boilerplate project: - Nome: Go - ID: golang.go - - Descrição: Rich Go language support for Visual Studio Code - - Versão: 0.29.0 + - Description: Rich Go language support for Visual Studio Code + - Version: 0.29.0 - Editor: Go Team at Google - Link do Marketplace do VS: https://marketplace.visualstudio.com/items?itemName=golang.Go @@ -49,6 +49,9 @@ make up # Run local container in background make up-silent +# Run local container in background with prefork +make up-silent-prefork + # Stop container make stop @@ -60,7 +63,7 @@ make start ```bash docker build -t gofiber . -docker run -d -p 3000:3000 gofiber +docker run -d -p 3000:3000 gofiber ./app -prod ``` Go to http://localhost:3000: diff --git a/go.mod b/go.mod index 8dc0efa..e585b2f 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module boilerplate -go 1.15 +go 1.19 require github.com/gofiber/fiber/v2 v2.37.0 diff --git a/handlers/handlers.go b/handlers/handlers.go index 116d3c9..0990506 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -5,11 +5,13 @@ import ( "boilerplate/models" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/utils" ) // UserGet returns a user func UserList(c *fiber.Ctx) error { users := database.Get() + return c.JSON(fiber.Map{ "success": true, "user": users, @@ -19,9 +21,10 @@ func UserList(c *fiber.Ctx) error { // UserCreate registers a user func UserCreate(c *fiber.Ctx) error { user := &models.User{ - Name: c.FormValue("user"), + Name: utils.CopyString(c.FormValue("user")), } database.Insert(user) + return c.JSON(fiber.Map{ "success": true, "user": user, diff --git a/static/public/index.html b/static/public/index.html index 003c8cb..447899d 100644 --- a/static/public/index.html +++ b/static/public/index.html @@ -5,7 +5,7 @@