Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes and improvements #84

Merged
merged 6 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/" # Location of package manifests
directory: "/"
schedule:
interval: "daily"
automerged_updates:
- match:
dependency_name: "gofiber/fiber/*"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "daily"
6 changes: 3 additions & 3 deletions .github/workflows/gotidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
uses: actions/setup-go@v3
with:
go-version: 1.13
go-version: 1.19
-
name: Tidy
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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
uses: reviewdog/action-golangci-lint@v2
with:
golangci_lint_flags: "--tests=false"
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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"]
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module boilerplate

go 1.15
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
)
5 changes: 4 additions & 1 deletion handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions static/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fiber Boilerplate</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css">
</head>

<body>
Expand All @@ -26,7 +26,7 @@ <h4>Users</h4>
</ul>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="/js/app.js"></script>
</body>

Expand Down
8 changes: 4 additions & 4 deletions static/public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 += '<li class="list-group-item">' + data.user[i].name + '</li>'
}
$.each(data.user, function(index, val) {
users += '<li class="list-group-item">' + val.name + '</li>';
})
$('#users').html('')
$('#users').append(users)
})
Expand All @@ -18,4 +18,4 @@ $('#add_user').on('click', (e) => {
$.post("/api/v1/users", "user=" + user, (data) => {
$('#users').prepend('<li class="list-group-item">' + data.user.name + '</li>')
})
})
})