Skip to content

Commit

Permalink
Merge pull request #75 from Caknoooo/feat/bugfix-error-on-docker-air
Browse files Browse the repository at this point in the history
fix(docker): fix air temp
  • Loading branch information
Caknoooo authored Jan 19, 2025
2 parents 4078a46 + ee3509e commit ef28313
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 90 deletions.
47 changes: 6 additions & 41 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
tmp_dir = "docker/air/tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 2000
exclude_dir = ["assets", "tmp", "vendor", "testdata", "static"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = true
rerun_delay = 2000
send_interrupt = true
stop_on_error = true

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
bin = "docker/air/tmp/main"
cmd = "go build -o docker/air/tmp/main"
include_ext = ["go"]
exclude_dir = ["vendor", "tmp"]

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
level = "debug"
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
APP_NAME=Go.Gin.Template

DB_HOST = <your host>
DB_USER = postgres
DB_PASS = <your password>
DB_NAME = <your database name>
DB_PORT = 5432

NGINX_PORT=8080
NGINX_PORT=80
GOLANG_PORT=8888
APP_ENV=localhost
JWT_SECRET=<your secret key>
Expand Down
17 changes: 0 additions & 17 deletions Dockerfile

This file was deleted.

36 changes: 35 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Import .env file
ifneq (,$(wildcard ./.env))
include .env
export $(shell sed 's/=.*//' .env)
endif

# Variables
CONTAINER_NAME=${APP_NAME}-app
POSTGRES_CONTAINER_NAME=${APP_NAME}-db

# Commands
dep:
go mod tidy

Expand All @@ -23,4 +34,27 @@ down:
docker-compose down

logs:
docker-compose logs -f
docker-compose logs -f

# Postgres commands
container-postgres:
docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh

create-db:
docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh -c "createdb --username=${DB_USER} --owner=${DB_USER} ${DB_NAME}"

init-uuid:
docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh -c "psql -U ${DB_USER} -d ${DB_NAME} -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"

# Docker commands
container-go:
docker exec -it ${CONTAINER_NAME} /bin/sh

migrate:
docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run main.go --migrate"

seed:
docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run main.go --seed"

migrate-seed:
docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run main.go --migrate --seed"
2 changes: 1 addition & 1 deletion config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func SetUpDatabaseConnection() *gorm.DB {
dbName := os.Getenv("DB_NAME")
dbPort := os.Getenv("DB_PORT")

dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v TimeZone=Asia/Jakarta", dbHost, dbUser, dbPass, dbName, dbPort)
dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v", dbHost, dbUser, dbPass, dbName, dbPort)

db, err := gorm.Open(postgres.New(postgres.Config{
DSN: dsn,
Expand Down
2 changes: 1 addition & 1 deletion constants/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const (
ENUM_RUN_PRODUCTION = "production"
ENUM_RUN_TESTING = "testing"

ENUM_PAGINATION_LIMIT = 10
ENUM_PAGINATION_PER_PAGE = 10
ENUM_PAGINATION_PAGE = 1
)
51 changes: 29 additions & 22 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
version: '3.9'

services:
app:
build:
context: .
dockerfile: ./docker/Dockerfile
container_name: ${APP_NAME:-go-gin-clean-starter}-app
volumes:
- .:/app
ports:
- ${GOLANG_PORT:-8888}:8888
networks:
- app-network

nginx:
image: nginx:latest
container_name: nginx
ports:
- ${NGINX_PORT:-81}:80
volumes:
- .:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
networks:
- app-network

postgres:
hostname: postgres
container_name: ${APP_NAME:-go-gin-clean-starter}-db
image: postgres:latest
ports:
- ${DB_PORT}:5432
volumes:
- ./volumes/postgres:/var/lib/postgresql/data
- ./docker/postgresql/tmp:/var/lib/postgresql/data
- app-data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}
- POSTGRES_DB=${DB_NAME}
networks:
- app-network

app:
hostname: app
container_name: go-gin-clean-starter
build:
context: .
dockerfile: Dockerfile
ports:
- ${GOLANG_PORT}:8888
restart: always
volumes:
- ./:/app
depends_on:
- postgres
env_file:
- .env
networks:
- app-network

volumes:
app_vol:
app-data:

networks:
app-network:
Expand Down
11 changes: 11 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23-alpine

WORKDIR /app

RUN go install github.com/air-verse/air@latest

COPY . .

RUN go mod tidy

CMD ["air"]
2 changes: 2 additions & 0 deletions docker/air/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
18 changes: 18 additions & 0 deletions docker/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
server {
listen 80;
server_name localhost;

location / {
proxy_pass http://app:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
}
2 changes: 2 additions & 0 deletions docker/postgresql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 1 addition & 1 deletion dto/user_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type (
}

GetAllUserRepositoryResponse struct {
Users []entity.User
Users []entity.User `json:"users"`
PaginationResponse
}

Expand Down
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/repository"
"github.com/Caknoooo/go-gin-clean-starter/routes"
"github.com/Caknoooo/go-gin-clean-starter/service"
"gorm.io/gorm"

"github.com/gin-gonic/gin"
)

func main() {
db := config.SetUpDatabaseConnection()
defer config.CloseDatabaseConnection(db)

func args(db *gorm.DB) bool {
if len(os.Args) > 1 {
flag := command.Commands(db)
if !flag {
return
return false
}
}

return true
}

func main() {
db := config.SetUpDatabaseConnection()
defer config.CloseDatabaseConnection(db)

if !args(db) {
return
}

var (
jwtService service.JWTService = service.NewJWTService()

Expand Down

0 comments on commit ef28313

Please sign in to comment.