Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NM-7: infrastructure changes for deploy #12

Merged
merged 4 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ENV_FILE = ./config/.dev.env
ENV_FILE = ./docker/.env
include $(ENV_FILE)

POSTGRES_CONNECTION = postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DB)?sslmode=disable

Expand All @@ -8,12 +9,6 @@ BINARY_PATH = ./bin/main
DOCKER_COMPOSE_PATH = ./docker/docker-compose.yaml
GOLANGCI_LINT_PATH = ./.golangci.yaml

# include env file
ifneq ("$(wildcard $(ENV_FILE))","")
include $(ENV_FILE)
export
endif

# use `gawk` on mac os
AWK := awk
ifeq ($(shell uname -s), Darwin)
Expand Down Expand Up @@ -122,26 +117,24 @@ lint:
.PHONY: docker-build
## Build docker container with microservice binary.
docker-build:
@docker compose -f $(DOCKER_COMPOSE_PATH) build
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) build

.PHONY: docker-%-migrate
.PHONY: docker-migrate
## Start docker compose service of database and apply migrations.
## Name of docker compose service must match in available database.
## Available databases: postgres, mysql, mssql, redshift, tidb,
## clickhouse, vertica, ydb.
## Format: `docker-<database>-migrate`.
## Example: `docker-postgres-migrate`.
docker-%-migrate:
@docker compose -f $(DOCKER_COMPOSE_PATH) up -d $*
@make $*-migrate
@docker compose -f $(DOCKER_COMPOSE_PATH) stop $*
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) up -d migrations

.PHONY: docker-start
## Start docker compose containers (all by default).
## Format: `docker-start [compose=<docker-compose-service>]`.
## Example: `docker-start`, `docker-stop compose=postgres`.
docker-start:
@docker compose -f $(DOCKER_COMPOSE_PATH) up -d $(compose)
@docker compose -f $(DOCKER_COMPOSE_PATH) --env-file $(ENV_FILE) up -d $(compose)

.PHONY: docker-stop
## Stop docker compose containers (all by default).
Expand All @@ -165,15 +158,17 @@ docker-psql:
docker-clean:
@docker compose -f $(DOCKER_COMPOSE_PATH) down

version ?= latest

.PHONY: build-image
## Build docker image of microservice with name.
build-image:
@docker build -f docker/Dockerfile.prod -t daronenko/$(SERVICE_NAME):latest .
@docker build -f docker/Dockerfile.$(ENV) -t daronenko/$(SERVICE_NAME):$(version) .

.PHONY: push-image
## Push docker image of microservice to the docker hub.
push-image:
@docker push daronenko/$(SERVICE_NAME):latest
@docker push daronenko/$(SERVICE_NAME):$(version)

################################################################################
# Cleaning
Expand Down
11 changes: 0 additions & 11 deletions config/.prod.env

This file was deleted.

2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func newViper() (*viper.Viper, error) {
v.SetConfigName(os.Getenv("CONFIG_NAME"))
v.SetConfigType(os.Getenv("CONFIG_TYPE"))

fmt.Println(os.Getenv("CONFIG_PATH"), os.Getenv("CONFIG_NAME"), os.Getenv("CONFIG_TYPE"))

if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return nil, errors.New("config file not found")
Expand Down
40 changes: 40 additions & 0 deletions config/prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
service:
port: :8080
readTimeout: 10
writeTimeout: 10
idleTimeout: 120
contextTimeout: 5

cors:
allowOrigin: "http://185.241.194.24"
allowMethods: "POST,GET,OPTIONS,PUT,DELETE"
allowHeaders: "Content-Type"
allowCredentials: true

logger:
level: info
format: json

auth:
jwt:
secret: secret
expire: 3600
cookie:
name: jwt-token
maxAge: 86400
secure: false
httpOnly: true

postgres:
host: postgres
port: 5432
user: user
password: password
dbName: db
sslMode: false
driver: postgres

maxOpenConns: 60
connMaxLifetime: 120
maxIdleConns: 30
connMaxIdleTime: 20
6 changes: 4 additions & 2 deletions config/.dev.env → docker/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
SERVICE_NAME=auth
SERVICE_NAME=novamusic

ENV=prod

CONFIG_PATH=config/
CONFIG_NAME=dev
CONFIG_NAME=prod
CONFIG_TYPE=yaml

POSTGRES_HOST=postgres
Expand Down
25 changes: 8 additions & 17 deletions docker/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine3.20 AS builder
FROM golang:1.22-alpine3.20

WORKDIR /service

Expand All @@ -7,21 +7,12 @@ RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o bin/main ./cmd/main.go
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GO111MODULE=on

FROM alpine:latest
ENV GOCACHE=/root/.cache/go-build
RUN --mount=type=cache,target="/root/.cache/go-build" \
go build -o bin/main cmd/main.go

RUN apk update && apk upgrade
RUN rm -rf /var/cache/apk/* && \
rm -rf /tmp/*

RUN adduser -D guest
USER guest

WORKDIR /service

COPY --from=builder /service/bin/main ./bin/

EXPOSE 8080

ENTRYPOINT ["./bin/main"]
ENTRYPOINT [ "/service/bin/main" ]
30 changes: 24 additions & 6 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
services:
auth:
container_name: auth
novamusic-backend:
image: daronenko/novamusic-backend:latest
container_name: novamusic-backend
platform: linux/amd64
env_file: .env
build:
dockerfile: docker/Dockerfile.dev
dockerfile: docker/Dockerfile.${ENV}
context: ..
env_file: ../config/.dev.env
ports:
- 8080:8080
restart: on-failure
Expand All @@ -13,9 +15,10 @@ services:
condition: service_healthy

postgres:
container_name: auth-postgres
container_name: novamusic-postgres
image: postgres:16
env_file: ../config/.dev.env
platform: linux/amd64
env_file: .env
environment:
- POSTGRES_HOST=${POSTGRES_HOST}
- POSTGRES_DB=${POSTGRES_DB}
Expand All @@ -36,5 +39,20 @@ services:
volumes:
- postgres-data:/var/lib/postgresql/data

migrations:
image: ghcr.io/kukymbr/goose-docker:3.22.1
platform: linux/amd64
restart: no
env_file: .env
depends_on:
postgres:
condition: service_healthy
environment:
- GOOSE_DRIVER=postgres
- GOOSE_DBSTRING=host=${POSTGRES_HOST} port=${POSTGRES_PORT} user=${POSTGRES_USER} password=${POSTGRES_PASSWORD} dbname=${POSTGRES_DB}
- GOOSE_VERBOSE=true
volumes:
- ../internal/db/postgres/migrations:/migrations

volumes:
postgres-data:
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
-- +goose StatementBegin
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- DROP TYPE role_type;
-- CREATE TYPE role_type AS ENUM ('regular', 'admin');
CREATE TYPE role_type AS ENUM ('regular', 'admin');

CREATE TABLE IF NOT EXISTS "user" (
user_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
Expand All @@ -20,6 +19,5 @@ CREATE TABLE IF NOT EXISTS "user" (

-- +goose Down
-- +goose StatementBegin
-- DROP TYPE IF EXISTS role_type;
DROP TABLE IF EXISTS "user";
-- +goose StatementEnd