From 3fb173036e06a87e8b34c9d24ae3e195caf0324c Mon Sep 17 00:00:00 2001 From: Ruhollah Date: Wed, 28 Aug 2024 00:06:38 +0330 Subject: [PATCH] refactor(niki): stage deployment docker setup (#130) --- .gitignore | 1 + Dockerfile | 30 +++++++------------ config.yml | 4 --- deploy/stage/app/.env.example | 11 +++++++ deploy/stage/app/config.yml | 12 ++++++++ deploy/stage/app/docker-compose.yml | 20 +++++++++++-- deploy/stage/config.yml | 36 ---------------------- deploy/stage/mariadb/.env.example | 4 +++ deploy/stage/mariadb/docker-compose.yml | 16 +++++----- deploy/stage/redis/.env.example | 1 + deploy/stage/redis/docker-compose.yml | 13 ++++---- docker-compose.dev.yaml | 40 ------------------------- go.mod | 2 -- 13 files changed, 71 insertions(+), 119 deletions(-) create mode 100644 deploy/stage/app/.env.example create mode 100644 deploy/stage/app/config.yml delete mode 100644 deploy/stage/config.yml create mode 100644 deploy/stage/mariadb/.env.example create mode 100644 deploy/stage/redis/.env.example delete mode 100644 docker-compose.dev.yaml diff --git a/.gitignore b/.gitignore index 6520d64..29059e3 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ activate.mise.toml # Dependency directories (remove the comment below to include it) .idea bin +tmp #.env *.env diff --git a/Dockerfile b/Dockerfile index 2d3305a..f89bbb3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,12 @@ # Build Stage -# First pull Golang image -FROM golang:1.23.0-alpine as builder - +FROM golang:1.23.0-alpine AS builder + # Set environment variable ENV APP_NAME niki -ENV CMD_PATH main.go # Add a work directory WORKDIR /$APP_NAME -## Cache and install dependencies -#COPY go.mod go.sum ./ -#RUN go mod download - # Copy app files COPY . . @@ -20,18 +14,16 @@ COPY . . RUN CGO_ENABLED=0 go build -mod=vendor -v -o $APP_NAME . # Run Stage +FROM alpine:3.20 AS runtime -FROM alpine:3.20 as Runtime +# Copy the binary from the builder stage +COPY --from=builder /niki/niki . + +# Copy migration files +COPY --from=builder /niki/repository/mysql/migration ./repository/mysql/migration - -# Set environment variable -ENV APP_NAME niki - -# Copy only required data into this image -COPY --from=builder /$APP_NAME . - # Expose application port EXPOSE 8313 - -# Start app -CMD ./$APP_NAME + +# Start the application +CMD ["./niki", "--migrate"] diff --git a/config.yml b/config.yml index 3928093..9a4488b 100644 --- a/config.yml +++ b/config.yml @@ -20,10 +20,6 @@ redis: password: "" db: 0 -sms_provider: - host: localhost - port: 443 - benefactor_service: length_of_otp_code: 5 diff --git a/deploy/stage/app/.env.example b/deploy/stage/app/.env.example new file mode 100644 index 0000000..ccaec54 --- /dev/null +++ b/deploy/stage/app/.env.example @@ -0,0 +1,11 @@ +COMPOSE_MARIADB_PORT=3306 +COMPOSE_MARIADB_DATABASE=niki_db +COMPOSE_MARIADB_USER=niki +COMPOSE_MARIADB_UR_PASSWORD=n0ki2agd23 + +COMPOSE_REDIS_PORT=6379 +COMPOSE_REDIS_DB=0 +COMPOSE_REDIS_PASSWORD=n0ki2agd23 + +COMPOSE_AUTH_SIGN_KEY=jwt_secret_stage_nik +COMPOSE_ADMIN_AUTH_SIGN_KEY=admin-jwt_secret_stage_nik \ No newline at end of file diff --git a/deploy/stage/app/config.yml b/deploy/stage/app/config.yml new file mode 100644 index 0000000..dd6c42a --- /dev/null +++ b/deploy/stage/app/config.yml @@ -0,0 +1,12 @@ +--- +type: yml + +http_server: + port: 1313 + +benefactor_service: + length_of_otp_code: 5 + +kavenegar_sms_provider: + api_key: insert_your_api_key + sender: insert_sender_number \ No newline at end of file diff --git a/deploy/stage/app/docker-compose.yml b/deploy/stage/app/docker-compose.yml index 4945183..95a286b 100644 --- a/deploy/stage/app/docker-compose.yml +++ b/deploy/stage/app/docker-compose.yml @@ -1,12 +1,26 @@ -version: '3.7' +version: '3.9' services: niki_app_stage: - image: niki:${STAGE_NIKI_IMAGE_VERSION} + image: niki:${STAGE_NIKI_IMAGE_VERSION?error} container_name: niki_app_stage + environment: + EB_MARIADB__HOST: niki_stage_mariadb + EB_MARIADB__PORT: ${COMPOSE_MARIADB_PORT?error} + EB_MARIADB__DB_NAME: ${COMPOSE_MARIADB_DATABASE?error} + EB_MARIADB__USERNAME: ${COMPOSE_MARIADB_USER?error} + EB_MARIADB__PASSWORD: ${COMPOSE_MARIADB_UR_PASSWORD?error} + + EB_REDIS__HOST: niki_stage_redis + EB_REDIS__PORT: ${COMPOSE_REDIS_PORT?error} + EB_REDIS__DB: ${COMPOSE_REDIS_DB?error} + EB_REDIS__PASSWORD: ${COMPOSE_REDIS_PASSWORD?error} + + EB_AUTH__SIGN_KEY: ${COMPOSE_AUTH_SIGN_KEY?error} + EB_ADMIN_AUTH__SIGN_KEY: ${COMPOSE_ADMIN_AUTH_SIGN_KEY?error} restart: always ports: - - '127.0.0.1:8198:1313' + - "127.0.0.1:8313:1313" networks: - niki-stage volumes: diff --git a/deploy/stage/config.yml b/deploy/stage/config.yml deleted file mode 100644 index df4939e..0000000 --- a/deploy/stage/config.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -type: yml - -auth: - sign_key: jwt_secret_stage_nik - -http_server: - port: 1313 - -mysql: - port: 3306 - host: niki_stage_mariadb - db_name: niki_db - username: niki - password: n0ki2agd23 - -redis: - port: 6379 - host: niki_stage_redis - password: "" - db: 0 - -sms_provider: - host: localhost - port: 443 - -benefactor_service: - length_of_otp_code: 5 - -kavenegar_sms_provider: - api_key: insert_your_api_key - otp_template_new_user: ebhomeverify - otp_template_registered_user: ebhomeverify - -admin_auth: - sign_key: admin-jwt_secret_test_nik \ No newline at end of file diff --git a/deploy/stage/mariadb/.env.example b/deploy/stage/mariadb/.env.example new file mode 100644 index 0000000..3d9001c --- /dev/null +++ b/deploy/stage/mariadb/.env.example @@ -0,0 +1,4 @@ +COMPOSE_MARIADB_DATABASE=niki_db +COMPOSE_MARIADB_USER=niki +COMPOSE_MARIADB_UR_PASSWORD=n0ki2agd23 +COMPOSE_MARIADB_RT_PASSWORD=n0ki2agd23 \ No newline at end of file diff --git a/deploy/stage/mariadb/docker-compose.yml b/deploy/stage/mariadb/docker-compose.yml index c33656a..80060e3 100644 --- a/deploy/stage/mariadb/docker-compose.yml +++ b/deploy/stage/mariadb/docker-compose.yml @@ -1,22 +1,22 @@ -version: '3.1' +version: '3.9' services: niki_stage_mariadb: image: docker.io/bitnami/mariadb:11.1 container_name: niki_stage_mariadb restart: always - ports: - - '127.0.0.1:3429:3306' networks: - niki-stage + expose: + - "3306" volumes: - 'niki_stage_mariadb_data:/bitnami/mariadb' environment: - - MARIADB_USER=niki - - MARIADB_PASSWORD=${NIKI_STAGE_MARIADB_UR_PASSWORD} - - MARIADB_DATABASE=niki_db - - MARIADB_ROOT_PASSWORD=${NIKI_STAGE_MARIADB_RT_PASSWORD} - - ALLOW_EMPTY_PASSWORD=no + MARIADB_USER: ${COMPOSE_MARIADB_USER?error} + MARIADB_PASSWORD: ${COMPOSE_MARIADB_UR_PASSWORD?error} + MARIADB_DATABASE: ${COMPOSE_MARIADB_DATABASE?error} + MARIADB_ROOT_PASSWORD: ${COMPOSE_MARIADB_RT_PASSWORD?error} + ALLOW_EMPTY_PASSWORD: no healthcheck: test: ['CMD', '/opt/bitnami/scripts/mariadb/healthcheck.sh'] interval: 15s diff --git a/deploy/stage/redis/.env.example b/deploy/stage/redis/.env.example new file mode 100644 index 0000000..d47f14a --- /dev/null +++ b/deploy/stage/redis/.env.example @@ -0,0 +1 @@ +COMPOSE_REDIS_PASSWORD=n0ki2agd23 \ No newline at end of file diff --git a/deploy/stage/redis/docker-compose.yml b/deploy/stage/redis/docker-compose.yml index 685483f..d1674a6 100644 --- a/deploy/stage/redis/docker-compose.yml +++ b/deploy/stage/redis/docker-compose.yml @@ -1,18 +1,17 @@ -version: '3.1' +version: '3.9' services: niki_stage_redis: image: bitnami/redis:6.2 container_name: niki_stage_redis restart: always - ports: - - '127.0.0.1:6380:6379' - # TODO - remove `--save "" --appendonly no` from command to persist data - command: redis-server --loglevel warning --protected-mode no --save "" --appendonly no + command: redis-server --loglevel warning --protected-mode no environment: - - ALLOW_EMPTY_PASSWORD=yes + REDIS_PASSWORD: ${COMPOSE_REDIS_PASSWORD?error} networks: - - core + - niki-stage + expose: + - "6379" volumes: - niki__stage_redis_data:/data diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml deleted file mode 100644 index d649945..0000000 --- a/docker-compose.dev.yaml +++ /dev/null @@ -1,40 +0,0 @@ -version: '3.9' - -services: - mysqltest: - image: mysql:8.0 - ports: - - "3305:3306" - container_name: niki-database-test - volumes: - - dbdatatest:/var/lib/mysql - restart: always - command: [ 'mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ] - environment: - MYSQL_ROOT_PASSWORD: 123456 - MYSQL_DATABASE: test_db - MYSQL_USER: testuser - MYSQL_PASSWORD: test1234 - - niki-redis-test: - image: bitnami/redis:6.2 - container_name: niki-redis-test - restart: always - ports: - - '6381:6379' - # TODO - remove `--save "" --appendonly no` from command to persist data - command: redis-server --loglevel warning --protected-mode no --save "" --appendonly no - environment: - - ALLOW_EMPTY_PASSWORD=yes - volumes: - - niki-redis-data-test:/data - - - -volumes: - dbdatatest: - niki-redis-data-test: - - - -# docker-compose -f docker-compose.dev.yaml up -d \ No newline at end of file diff --git a/go.mod b/go.mod index c12b376..49e3696 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module git.gocasts.ir/ebhomengo/niki go 1.23 -toolchain go1.23.0 - require ( github.com/go-ozzo/ozzo-validation v3.6.0+incompatible github.com/go-ozzo/ozzo-validation/v4 v4.3.0