Skip to content

Commit f2e56f7

Browse files
committed
✨ Project template started
1 parent 9fb444a commit f2e56f7

22 files changed

+788
-0
lines changed

.air.toml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
2+
3+
# Working directory
4+
# . or absolute path, please note that the directories following must be under root.
5+
root = "."
6+
tmp_dir = "tmp"
7+
8+
[build]
9+
# Just plain old shell command. You could use `make` as well.
10+
cmd = "go build -o ./tmp/main ./cmd"
11+
# Binary file yields from `cmd`.
12+
bin = "tmp/main"
13+
# Customize binary, can setup environment variables when run your app.
14+
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
15+
# Watch these filename extensions.
16+
include_ext = ["go", "tpl", "tmpl", "html"]
17+
# Ignore these filename extensions or directories.
18+
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
19+
# Watch these directories if you specified.
20+
include_dir = []
21+
# Watch these files.
22+
include_file = []
23+
# Exclude files.
24+
exclude_file = []
25+
# Exclude specific regular expressions.
26+
exclude_regex = ["_test\\.go"]
27+
# Exclude unchanged files.
28+
exclude_unchanged = true
29+
# Follow symlink for directories
30+
follow_symlink = true
31+
# This log file places in your tmp_dir.
32+
log = "air.log"
33+
# Poll files for changes instead of using fsnotify.
34+
poll = false
35+
# Poll interval (defaults to the minimum interval of 500ms).
36+
poll_interval = 500 # ms
37+
# It's not necessary to trigger build each time file changes if it's too frequent.
38+
delay = 0 # ms
39+
# Stop running old binary when build errors occur.
40+
stop_on_error = true
41+
# Send Interrupt signal before killing process (windows does not support this feature)
42+
send_interrupt = false
43+
# Delay after sending Interrupt signal
44+
kill_delay = 500 # ms
45+
# Rerun binary or not
46+
rerun = false
47+
# Delay after each executions
48+
rerun_delay = 500
49+
# Add additional arguments when running binary (bin/full_bin). Will run './tmp/main hello world'.
50+
args_bin = ["hello", "world"]
51+
52+
[log]
53+
# Show log time
54+
time = false
55+
# Only show main log (silences watcher, build, runner)
56+
main_only = false
57+
58+
[color]
59+
# Customize each part's color. If no color found, use the raw app log.
60+
main = "magenta"
61+
watcher = "cyan"
62+
build = "yellow"
63+
runner = "green"
64+
65+
[misc]
66+
# Delete tmp directory on exit
67+
clean_on_exit = true

.env.example

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DB_PORT=5432
2+
DB_USER=example_user
3+
DB_PASSWORD=example_password
4+
DB_NAME=example_db
5+
SECRET=example_secret
6+
PGADMIN_DEFAULT_EMAIL=[email protected]
7+
PGADMIN_DEFAULT_PASSWORD=SecurePassword

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.dll
88
*.so
99
*.dylib
10+
main
1011

1112
# Test binary, built with `go test -c`
1213
*.test
@@ -19,3 +20,6 @@
1920

2021
# Go workspace file
2122
go.work
23+
24+
# Environment variables file
25+
.env

Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:1.21@sha256:cffaba795c36f07e372c7191b35ceaae114d74c31c3763d442982e3a4df3b39e
2+
3+
# Enviroment variable
4+
WORKDIR /usr/src/some-api
5+
6+
RUN go install github.com/cosmtrek/air@latest
7+
8+
#Copying files to work directory
9+
COPY go.mod ./
10+
RUN go mod download && go mod verify
11+
COPY . .
12+
13+
# Run and expose the server on port 3000
14+
EXPOSE 3000

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Go Fiber + JWT Auth + Docker + PostgreSQL Boilerplate
2+
3+
Boilerplate for Go Fiber that makes use of Docker, PostgreSQL and JWT.
4+
5+
## Development
6+
7+
### In order to have an optimal development experience you need to have Docker installed
8+
9+
Set the environment variables in a `.env` file:
10+
11+
- DB_PORT=5432
12+
- DB_USER=example_user
13+
- DB_PASSWORD=example_password
14+
- DB_NAME=example_db
15+
- SECRET=example_secret
16+
17+
Be sure you don't have any conflicting containers.
18+
Then run the commands:
19+
20+
```sh
21+
docker-compose up -d
22+
```
23+
24+
This should start the API and the database.
25+
26+
## Database Management
27+
28+
You can manage the database via `psql` with the command:
29+
30+
```sh
31+
docker-compose exec db psql -U <DB_USER (from the environment variables)>
32+
```

cmd/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"app/database"
5+
"app/router"
6+
"log"
7+
8+
"github.com/gofiber/fiber/v2"
9+
// "github.com/gofiber/fiber/v2/middleware/cors"
10+
)
11+
12+
func main() {
13+
app := fiber.New(fiber.Config{
14+
Prefork: true,
15+
CaseSensitive: true,
16+
StrictRouting: true,
17+
ServerHeader: "Fiber",
18+
AppName: "App Name",
19+
})
20+
// app.Use(cors.New())
21+
22+
database.ConnectDB()
23+
24+
router.SetupRoutes(app)
25+
log.Fatal(app.Listen(":3000"))
26+
}

config/config.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/joho/godotenv"
8+
)
9+
10+
// Config func to get env value
11+
func Config(key string) string {
12+
// load .env file
13+
err := godotenv.Load(".env")
14+
if err != nil {
15+
fmt.Print("Error loading .env file")
16+
}
17+
return os.Getenv(key)
18+
}

create_user.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"username": "johndoe",
3+
"email": "[email protected]",
4+
"password": "1234567890"
5+
}

database/connect.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package database
2+
3+
import (
4+
"app/config"
5+
"app/model"
6+
"fmt"
7+
"strconv"
8+
9+
"gorm.io/driver/postgres"
10+
"gorm.io/gorm"
11+
)
12+
13+
// ConnectDB connect to db
14+
func ConnectDB() {
15+
var err error
16+
p := config.Config("DB_PORT")
17+
port, err := strconv.ParseUint(p, 10, 32)
18+
19+
if err != nil {
20+
panic("failed to parse database port")
21+
}
22+
23+
dsn := fmt.Sprintf(
24+
"host=db port=%d user=%s password=%s dbname=%s sslmode=disable",
25+
port,
26+
config.Config("DB_USER"),
27+
config.Config("DB_PASSWORD"),
28+
config.Config("DB_NAME"),
29+
)
30+
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
31+
32+
if err != nil {
33+
panic("failed to connect database")
34+
}
35+
36+
fmt.Println("Connection Opened to Database")
37+
DB.AutoMigrate(&model.Product{}, &model.User{})
38+
fmt.Println("Database Migrated")
39+
}

database/database.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package database
2+
3+
import "gorm.io/gorm"
4+
5+
// DB gorm connector
6+
var DB *gorm.DB

docker-compose.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: '3.8'
2+
services:
3+
web:
4+
build: .
5+
env_file:
6+
- .env
7+
ports:
8+
- 3000:3000
9+
volumes:
10+
- .:/usr/src/some-api
11+
depends_on:
12+
- db
13+
command: air cmd/main.go -b 0.0.0.0
14+
15+
db:
16+
image: postgres:alpine
17+
environment:
18+
- POSTGRES_USER=${DB_USER}
19+
- POSTGRES_PASSWORD=${DB_PASSWORD}
20+
- POSTGRES_DB=${DB_NAME}
21+
ports:
22+
- ${DB_PORT}:${DB_PORT}
23+
volumes:
24+
- postgres-db:/var/lib/postgresql/data
25+
26+
pgadmin:
27+
image: dpage/pgadmin4
28+
environment:
29+
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL}
30+
- PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD}
31+
ports:
32+
- 5050:80
33+
depends_on:
34+
- db
35+
restart: unless-stopped
36+
37+
volumes:
38+
postgres-db:

go.mod

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module app
2+
3+
go 1.20
4+
5+
require (
6+
github.com/go-playground/validator/v10 v10.18.0
7+
github.com/gofiber/contrib/jwt v1.0.7
8+
github.com/gofiber/fiber/v2 v2.52.1
9+
github.com/golang-jwt/jwt/v5 v5.2.0
10+
github.com/joho/godotenv v1.5.1
11+
golang.org/x/crypto v0.21.0
12+
gorm.io/driver/postgres v1.5.2
13+
gorm.io/gorm v1.25.7
14+
)
15+
16+
require (
17+
github.com/MicahParks/keyfunc/v2 v2.1.0 // indirect
18+
github.com/andybalholm/brotli v1.0.5 // indirect
19+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
20+
github.com/go-playground/locales v0.14.1 // indirect
21+
github.com/go-playground/universal-translator v0.18.1 // indirect
22+
github.com/google/uuid v1.5.0 // indirect
23+
github.com/jackc/pgpassfile v1.0.0 // indirect
24+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
25+
github.com/jackc/pgx/v5 v5.5.4 // indirect
26+
github.com/jackc/puddle/v2 v2.2.1 // indirect
27+
github.com/jinzhu/inflection v1.0.0 // indirect
28+
github.com/jinzhu/now v1.1.5 // indirect
29+
github.com/klauspost/compress v1.17.0 // indirect
30+
github.com/leodido/go-urn v1.4.0 // indirect
31+
github.com/mattn/go-colorable v0.1.13 // indirect
32+
github.com/mattn/go-isatty v0.0.20 // indirect
33+
github.com/mattn/go-runewidth v0.0.15 // indirect
34+
github.com/rivo/uniseg v0.4.4 // indirect
35+
github.com/valyala/bytebufferpool v1.0.0 // indirect
36+
github.com/valyala/fasthttp v1.51.0 // indirect
37+
github.com/valyala/tcplisten v1.0.0 // indirect
38+
golang.org/x/net v0.21.0 // indirect
39+
golang.org/x/sync v0.1.0 // indirect
40+
golang.org/x/sys v0.18.0 // indirect
41+
golang.org/x/text v0.14.0 // indirect
42+
)

0 commit comments

Comments
 (0)