From 1533914e4d346e44ddad52ed4fe68741d017263a Mon Sep 17 00:00:00 2001 From: Efril Date: Thu, 9 Jul 2026 22:11:03 +0700 Subject: [PATCH] config: prod and staging --- .gitignore | 4 ++ Makefile | 34 +++++++++--- config/configs.go | 3 +- deployment.sh | 54 +++++++++++++++--- infra/{development.yaml => production.yaml} | 14 ++--- infra/staging.yaml | 61 +++++++++++++++++++++ 6 files changed, 147 insertions(+), 23 deletions(-) rename infra/{development.yaml => production.yaml} (77%) create mode 100644 infra/staging.yaml diff --git a/.gitignore b/.gitignore index 0e866c8..8cfef51 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,7 @@ vendor # Firebase service account credentials infra/firebase-service-account.json + +# Config files containing secrets (manage manually on each server) +# infra/production.yaml +# infra/staging.yaml diff --git a/Makefile b/Makefile index ebecae2..6b79f01 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,21 @@ #PROJECT_NAME = "enaklo-pos-backend" -DB_USERNAME :=apskel -DB_PASSWORD :=7a8UJbM2GgBWaseh0lnP3O5i1i5nINXk -DB_HOST :=62.72.45.250 -DB_PORT :=5433 -DB_NAME :=apskel_pos + +# ─── Environment (default: staging) ────────────────────────────────────────── +ENV ?= staging + +ifeq ($(ENV),production) + DB_USERNAME :=apskel + DB_PASSWORD :=7a8UJbM2GgBWaseh0lnP3O5i1i5nINXk + DB_HOST :=62.72.45.250 + DB_PORT :=5433 + DB_NAME :=apskel_pos +else + DB_USERNAME :=apskel + DB_PASSWORD :=7a8UJbM2GgBWaseh0lnP3O5i1i5nINXk + DB_HOST :=62.72.45.250 + DB_PORT :=5433 + DB_NAME :=apskel_pos_staging +endif DB_URL = postgres://$(DB_USERNAME):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=disable @@ -16,15 +28,19 @@ endif .SILENT: help help: @echo - @echo "Usage: make [command]" + @echo "Usage: make [command] [ENV=staging|production]" @echo @echo "Commands:" + @echo " run Run server (default: staging)" + @echo " run ENV=production Run server with production config" + @echo @echo " rename-project name={name} Rename project" @echo @echo " build-http Build http server" @echo @echo " migration-create name={name} Create migration" @echo " migration-up Up migrations" + @echo " migration-up ENV=production Up migrations (production DB)" @echo " migration-down Down last migration" @echo @echo " docker-up Up docker services" @@ -114,7 +130,11 @@ fmt: @go fmt ./... start: - go run main.go --env-path .env + ENV_MODE=$(ENV) go run cmd/server/main.go + +.SILENT: run +run: + ENV_MODE=$(ENV) go run cmd/server/main.go # Default diff --git a/config/configs.go b/config/configs.go index d589bf4..e5a9d9e 100644 --- a/config/configs.go +++ b/config/configs.go @@ -12,13 +12,14 @@ import ( const ( YAML_PATH = "infra/%s" ENV_MODE = "ENV_MODE" - DEFAULT_ENV_MODE = "development" + DEFAULT_ENV_MODE = "staging" ) var ( validEnvMode = map[string]struct{}{ "local": {}, "development": {}, + "staging": {}, "production": {}, } ) diff --git a/deployment.sh b/deployment.sh index 5530966..565defa 100644 --- a/deployment.sh +++ b/deployment.sh @@ -2,23 +2,61 @@ set -euo pipefail APP_NAME="apskel-pos" -PORT="4000" + +# ─── Deteksi environment dari branch aktif ─────────────────────────────────── +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) + +case "$CURRENT_BRANCH" in + main) + ENV_MODE="production" + PORT="4000" + ;; + staging) + ENV_MODE="staging" + PORT="4001" + ;; + *) + echo "❌ Branch '$CURRENT_BRANCH' tidak dikenali untuk deployment." + echo " Gunakan branch 'main' (production) atau 'staging' (staging)." + exit 1 + ;; +esac + +CONTAINER_NAME="$APP_NAME-$ENV_MODE" +IMAGE_NAME="$APP_NAME:$ENV_MODE" + +echo "📦 Environment : $ENV_MODE" +echo "🌿 Branch : $CURRENT_BRANCH" +echo "🐳 Container : $CONTAINER_NAME" +echo "🔌 Port : $PORT" +echo "" + +# ─── Pastikan config file ada ───────────────────────────────────────────────── +CONFIG_FILE="infra/$ENV_MODE.yaml" +if [ ! -f "$CONFIG_FILE" ]; then + echo "❌ Config file '$CONFIG_FILE' tidak ditemukan." + exit 1 +fi echo "🔄 Pulling latest code..." git pull -echo "🐳 Building Docker image (production target)..." -docker build --target production -t $APP_NAME:latest . +echo "🐳 Building Docker image ($ENV_MODE)..." +docker build --target production -t "$IMAGE_NAME" . echo "🛑 Stopping and removing old container..." -docker rm -f $APP_NAME 2>/dev/null || true +docker rm -f "$CONTAINER_NAME" 2>/dev/null || true echo "🚀 Running new container..." -docker run -d --name $APP_NAME \ - -p $PORT:$PORT \ +docker run -d --name "$CONTAINER_NAME" \ + -p "$PORT:4000" \ -e TZ=Asia/Jakarta \ + -e ENV_MODE="$ENV_MODE" \ -v "$(pwd)/infra":/infra:ro \ -v "$(pwd)/templates":/templates:ro \ - $APP_NAME:latest + "$IMAGE_NAME" -echo "✅ Deployment complete." +echo "" +echo "✅ Deployment $ENV_MODE complete." +echo " Container : $CONTAINER_NAME" +echo " Port : $PORT" diff --git a/infra/development.yaml b/infra/production.yaml similarity index 77% rename from infra/development.yaml rename to infra/production.yaml index 7e90f8f..a9857aa 100644 --- a/infra/development.yaml +++ b/infra/production.yaml @@ -1,7 +1,7 @@ server: base-url: local-url: - self-order-url: http://localhost:5173 + self-order-url: port: 4000 jwt: @@ -9,7 +9,7 @@ jwt: expires-ttl: 144000 secret: "5Lm25V3Qd7aut8dr4QUxm5PZUrSFs" refresh_token: - expires-ttl: 7776000 # 3 months in minutes (90 days * 24 hours * 60 minutes) + expires-ttl: 7776000 secret: "R3fr3sh_T0k3n_S3cr3t_K3y_2024_P0S" customer: expires-ttl: 7776000 @@ -21,7 +21,7 @@ postgresql: driver: postgres db: apskel_pos username: apskel - password: '7a8UJbM2GgBWaseh0lnP3O5i1i5nINXk' + password: "7a8UJbM2GgBWaseh0lnP3O5i1i5nINXk" ssl-mode: disable max-idle-connections-in-second: 600 max-open-connections-in-second: 600 @@ -45,11 +45,11 @@ s3: endpoint: sin1.contabostorage.com bucket_name: enaklo log_level: Error - host_url: 'https://sin1.contabostorage.com/fda98c2228f246f29a7e466b86b3b9e7:' + host_url: "https://sin1.contabostorage.com/fda98c2228f246f29a7e466b86b3b9e7:" log: - log_format: 'json' - log_level: 'debug' + log_format: "json" + log_level: "info" fonnte: api_url: "https://api.fonnte.com/send" @@ -58,4 +58,4 @@ fonnte: fcm: credentials_file: "infra/firebase-service-account.json" - project_id: "apskel-pos-v2" \ No newline at end of file + project_id: "apskel-pos-v2" diff --git a/infra/staging.yaml b/infra/staging.yaml new file mode 100644 index 0000000..2a56260 --- /dev/null +++ b/infra/staging.yaml @@ -0,0 +1,61 @@ +server: + base-url: + local-url: + self-order-url: + port: 4000 + +jwt: + token: + expires-ttl: 144000 + secret: "eZ7LAZJuSOGSHxb1ZYaZCkrBo5YBvc" + refresh_token: + expires-ttl: 7776000 + secret: "EMx2DKPtMp0jQNpLvzzCsZkoUHe0d9" + customer: + expires-ttl: 7776000 + secret: "layCV2rne0X57acWzSS3NxENmYJs7B" + +postgresql: + host: 62.72.45.250 + port: 5432 + driver: postgres + db: apskel_pos_staging + username: apskel + password: "7a8UJbM2GgBWaseh0lnP3O5i1i5nINXk" + ssl-mode: disable + max-idle-connections-in-second: 600 + max-open-connections-in-second: 600 + connection-max-life-time-in-second: 600 + debug: false + +redis: + host: 62.72.45.250 + port: 6380 + password: "CmICdmnX1EZPhVBYzQPEGw==U" + db: 1 + dial_timeout: 5s + read_timeout: 3s + write_timeout: 3s + pool_size: 10 + min_idle_connections: 5 + +s3: + access_key_id: cf9a475e18bc7626cbdbf09709d82a64 + access_key_secret: 91f3321294d3e23035427a0ecb893ada + endpoint: sin1.contabostorage.com + bucket_name: enaklo + log_level: Error + host_url: "https://sin1.contabostorage.com/fda98c2228f246f29a7e466b86b3b9e7:" + +log: + log_format: "json" + log_level: "info" + +fonnte: + api_url: "https://api.fonnte.com/send" + token: "bADQrf9NTXfLZQCK2wGg" + timeout: 30 + +fcm: + credentials_file: "infra/firebase-service-account.json" + project_id: "apskel-pos-v2"