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 @@ Fiber Boilerplate - + @@ -26,7 +26,7 @@

Users

- + diff --git a/static/public/js/app.js b/static/public/js/app.js index d3f4e66..af5fb2a 100644 --- a/static/public/js/app.js +++ b/static/public/js/app.js @@ -5,9 +5,9 @@ $(document).ready(function () { function listUsers() { $.getJSON("/api/v1/users", (data) => { var users = '' - for (var i = 0; i < data.user.length; i++) { - users += '
  • ' + data.user[i].name + '
  • ' - } + $.each(data.user, function(index, val) { + users += '
  • ' + val.name + '
  • ' + }); $('#users').html('') $('#users').append(users) }) From 55d2bcaf52c248bcceabdd51011c65de8ebd8df7 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Sun, 4 Sep 2022 00:03:48 -0400 Subject: [PATCH 2/6] Update all workflows --- .github/workflows/gotidy.yml | 2 +- .github/workflows/linter.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gotidy.yml b/.github/workflows/gotidy.yml index d3c5ea5..428593b 100644 --- a/.github/workflows/gotidy.yml +++ b/.github/workflows/gotidy.yml @@ -18,7 +18,7 @@ jobs: uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: 1.19 - diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 8a0d42d..26492c5 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -7,6 +7,6 @@ jobs: - name: Fetch Repository uses: actions/checkout@v3 - name: Run Golint - uses: reviewdog/action-golangci-lint@v1 + uses: reviewdog/action-golangci-lint@v2 with: golangci_lint_flags: "--tests=false" From 5ba2cbc292ae7caff883db1260e89ce1b20055e2 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Sun, 4 Sep 2022 00:13:40 -0400 Subject: [PATCH 3/6] Remove semicolon --- static/public/js/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/public/js/app.js b/static/public/js/app.js index af5fb2a..3e4af58 100644 --- a/static/public/js/app.js +++ b/static/public/js/app.js @@ -7,7 +7,7 @@ function listUsers() { var users = '' $.each(data.user, function(index, val) { users += '
  • ' + val.name + '
  • ' - }); + }) $('#users').html('') $('#users').append(users) }) From 4891ea65a5404fe5ac96f16712a11a4f09f45a6b Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Tue, 6 Sep 2022 08:55:38 -0400 Subject: [PATCH 4/6] Auto-merge Fiber updates --- .github/dependabot.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 76770b5..339558d 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -6,6 +6,9 @@ updates: directory: "/" schedule: interval: "daily" + automerged_updates: + - match: + dependency_name: "gofiber/fiber/*" - package-ecosystem: "github-actions" directory: "/" schedule: @@ -13,4 +16,4 @@ updates: - package-ecosystem: "docker" directory: "/" schedule: - interval: "daily" \ No newline at end of file + interval: "daily" From b54649eb3f18420a2561066346b552c2b038230e Mon Sep 17 00:00:00 2001 From: RW Date: Tue, 6 Sep 2022 17:45:33 +0200 Subject: [PATCH 5/6] Update app.js --- static/public/js/app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/public/js/app.js b/static/public/js/app.js index 3e4af58..4fc11b1 100644 --- a/static/public/js/app.js +++ b/static/public/js/app.js @@ -6,7 +6,7 @@ function listUsers() { $.getJSON("/api/v1/users", (data) => { var users = '' $.each(data.user, function(index, val) { - users += '
  • ' + val.name + '
  • ' + users += '
  • ' + val.name + '
  • '; }) $('#users').html('') $('#users').append(users) @@ -18,4 +18,4 @@ $('#add_user').on('click', (e) => { $.post("/api/v1/users", "user=" + user, (data) => { $('#users').prepend('
  • ' + data.user.name + '
  • ') }) -}) \ No newline at end of file +}) From 7c2caee62bf93f4849ee04ec2ae962aee9a26a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Tue, 6 Sep 2022 17:51:28 +0200 Subject: [PATCH 6/6] refresh go.mod file --- go.mod | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/go.mod b/go.mod index e585b2f..6533633 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,12 @@ module boilerplate go 1.19 require github.com/gofiber/fiber/v2 v2.37.0 + +require ( + github.com/andybalholm/brotli v1.0.4 // indirect + github.com/klauspost/compress v1.15.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.39.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect +)