diff --git a/.air.toml b/.air.toml index 3d283f7..42646b2 100644 --- a/.air.toml +++ b/.air.toml @@ -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 \ No newline at end of file + level = "debug" diff --git a/.env.example b/.env.example index a1b479b..ce7d277 100644 --- a/.env.example +++ b/.env.example @@ -1,10 +1,12 @@ +APP_NAME=Go.Gin.Template + DB_HOST = DB_USER = postgres DB_PASS = DB_NAME = DB_PORT = 5432 -NGINX_PORT=8080 +NGINX_PORT=80 GOLANG_PORT=8888 APP_ENV=localhost JWT_SECRET= diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b351083..0000000 --- a/Dockerfile +++ /dev/null @@ -1,17 +0,0 @@ -FROM golang:alpine - -RUN apk update && apk upgrade && \ - apk add --no-cache bash - -WORKDIR /app - -COPY go.mod go.sum ./ -RUN go mod download - -COPY . . - -RUN go build -o main . - -EXPOSE 8888 - -CMD ["./main"] \ No newline at end of file diff --git a/Makefile b/Makefile index 71f78bf..17853dc 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -23,4 +34,27 @@ down: docker-compose down logs: - docker-compose logs -f \ No newline at end of file + 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" \ No newline at end of file diff --git a/config/database.go b/config/database.go index 4d911ac..5940172 100644 --- a/config/database.go +++ b/config/database.go @@ -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, diff --git a/constants/common.go b/constants/common.go index b51e834..02a22de 100644 --- a/constants/common.go +++ b/constants/common.go @@ -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 ) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 96d6beb..38ace7e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..2791c65 --- /dev/null +++ b/docker/Dockerfile @@ -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"] \ No newline at end of file diff --git a/docker/air/.gitignore b/docker/air/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/docker/air/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf new file mode 100644 index 0000000..330dc5c --- /dev/null +++ b/docker/nginx/default.conf @@ -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; + } +} \ No newline at end of file diff --git a/docker/postgresql/.gitignore b/docker/postgresql/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/docker/postgresql/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/dto/user_dto.go b/dto/user_dto.go index ec09055..45003b0 100644 --- a/dto/user_dto.go +++ b/dto/user_dto.go @@ -79,7 +79,7 @@ type ( } GetAllUserRepositoryResponse struct { - Users []entity.User + Users []entity.User `json:"users"` PaginationResponse } diff --git a/main.go b/main.go index 5260e66..b62be16 100644 --- a/main.go +++ b/main.go @@ -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()