diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..7a61806 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.mockery.yaml +.golangci.yaml +.gitlab-ci.yaml +.gitingore +.Makefile +.README.md +deployments +tests \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..92522a7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,72 @@ +# BUILD +FROM golang:1.20.3-alpine AS build + +# Update depences +RUN apk update && apk add --no-cache curl +# Create build directory +RUN mkdir /app/bin -p +RUN mkdir /bin/golang-migrate -p +# Download migrate app +RUN GOLANG_MIGRATE_VERSION=v4.15.1 && \ + curl -L https://github.com/golang-migrate/migrate/releases/download/${GOLANG_MIGRATE_VERSION}/migrate.linux-amd64.tar.gz |\ + tar xvz migrate -C /bin/golang-migrate +# Download health check utility +RUN GRPC_HEALTH_PROBE_VERSION=v0.4.6 && \ + wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \ + chmod +x /bin/grpc_health_probe +# Set home directory +WORKDIR /app +# Copy go.mod +ADD go.mod go.sum /app/ +# Download go depences +RUN go mod download +# Copy all local files +ADD . /app +# Build app +RUN GOOS=linux go build -o bin ./... + + + +# TEST +FROM alpine:latest AS test + +# Install packages +RUN apk --no-cache add ca-certificates +ENV GO111MODULE=off +# Create home directory +WORKDIR /app +# Copy build file +COPY --from=build /app/bin/app ./app +# CMD +CMD [ "./app" ] + + +# MIGRATION +FROM alpine:latest AS migration + +# Install packages +RUN apk --no-cache add ca-certificates +# Create home directory +WORKDIR /app +# Copy migration dir +COPY --from=build /app/migrations/test ./migrations +# Install migrate tool +COPY --from=build /bin/golang-migrate /usr/local/bin + + + +# PRODUCTION +FROM alpine:latest AS production + +# Install packages +RUN apk --no-cache add ca-certificates +# Create home directory +WORKDIR /app +# Copy build file +COPY --from=build /app/bin/app ./app +# Copy grpc health probe dir +COPY --from=build /bin/grpc_health_probe /bin/grpc_health_probe +# Install migrate tool +COPY --from=build /bin/golang-migrate /usr/local/bin +# CMD +CMD ["./app"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..02d812b --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +help: ## show this help + @echo 'usage: make [target] ...' + @echo '' + @echo 'targets:' + @egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#' + +install: ## install all go dependencies + go get \ + github.com/bufbuild/buf/cmd/buf \ + github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \ + github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \ + google.golang.org/grpc/cmd/protoc-gen-go-grpc \ + google.golang.org/protobuf/cmd/protoc-gen-go + +generate: ## generate grpc proto for golang + buf generate + go generate ./internal/interface/swagger + +test: ## run all layers tests + @make test.unit + @make test.integration + +test.unit: ## run unit tests + go test ./... + +test.integration: ## run integration tests + @make test.integration.up + @make test.integration.start + @make test.integration.down + +test.integration.up: ## build integration test environment + docker-compose -f deployments/test/docker-compose.yaml --env-file ./.env.test up -d + +test.integration.start: ## run integration test + go test -tags integration ./tests/integration/... + +test.integration.down: ## shutting down integration environment + docker-compose -f deployments/test/docker-compose.yaml --env-file ./.env.test down --volumes --rmi local + +run: ## run app + go run ./cmd/app/main.go + +dev.up: ## run dev environment + docker-compose -f deployments/dev/docker-compose.yaml up -d + +dev.down: ## shutting down dev environment + docker-compose -f deployments/dev/docker-compose.yaml down --volumes --rmi local \ No newline at end of file diff --git a/main.go b/cmd/app/main.go similarity index 68% rename from main.go rename to cmd/app/main.go index 64bcaff..8d295dd 100644 --- a/main.go +++ b/cmd/app/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/danilsolovyov/croupierCbrf/app" + "github.com/danilsolovyov/croupierCbrf/internal/app" "github.com/skeris/appInit" ) diff --git a/deployments/dev/docker-compose.yaml b/deployments/dev/docker-compose.yaml new file mode 100644 index 0000000..a1298e6 --- /dev/null +++ b/deployments/dev/docker-compose.yaml @@ -0,0 +1,26 @@ +version: "3.3" + +services: + cbrfworker-app: + hostname: cbrfworker-service + container_name: cbrfworker-service + tty: true + build: + context: ../../. + dockerfile: Dockerfile + target: test + environment: + - IS_PRODUCTION=false + - DEVELOPMENT=true + - APP_NAME=croupierWorkerQuotes + - APP_ADDR=0.0.0.0:3131 + - DATABASE_URI=mongodb://test:test@mongo:27017/croupier?authSource=admin&authMechanism=SCRAM-SHA-1 + - DATABASE_TABLE=croupier + - COLLECTION_NAME=quote + ports: + - 3131:3131 + networks: + - dev + +networks: + dev: diff --git a/deployments/staging/docker-compose.yaml b/deployments/staging/docker-compose.yaml new file mode 100644 index 0000000..9fdf1c5 --- /dev/null +++ b/deployments/staging/docker-compose.yaml @@ -0,0 +1,28 @@ +version: "3.3" + +services: + cbrfworker-app-staging: + hostname: cbrfworker-service-staging + container_name: cbrfworker-service-staging + image: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG.$CI_PIPELINE_ID + tty: true + environment: + - IS_PRODUCTION=false + - DEVELOPMENT=true + - APP_NAME=croupierWorkerQuotes + - APP_ADDR=0.0.0.0:3131 + - DATABASE_URI=mongodb://test:test@mongo:27017/croupier?authSource=admin&authMechanism=SCRAM-SHA-1 + - DATABASE_TABLE=croupier + - COLLECTION_NAME=quote + ports: + - 3131:3131 + networks: + - backend_external + - default + +networks: + backend_external: + driver: bridge + attachable: true + internal: true + diff --git a/deployments/test/docker-compose.yaml b/deployments/test/docker-compose.yaml new file mode 100644 index 0000000..e88ca2b --- /dev/null +++ b/deployments/test/docker-compose.yaml @@ -0,0 +1,35 @@ +version: "3.3" + +services: + cbrfworker-app: + build: + context: ../../. + dockerfile: Dockerfile + target: test + environment: + - IS_PRODUCTION=false + - DEVELOPMENT=true + - APP_NAME=croupierWorkerQuotes + - APP_ADDR=0.0.0.0:3131 + - DATABASE_URI=mongodb://admin:admin@mongo:27017/admin?authSource=admin&authMechanism=SCRAM-SHA-1 + - DATABASE_TABLE=croupier + - COLLECTION_NAME=quote + ports: + - 3131:3131 + networks: + - integartion_test + depends_on: + - mongo + + mongo: + image: 'mongo:6.0.3' + environment: + MONGO_INITDB_ROOT_USERNAME: admin + MONGO_INITDB_ROOT_PASSWORD: admin + ports: + - '27017:27017' + networks: + - integartion_test + +networks: + integartion_test: diff --git a/app/app.go b/internal/app/app.go similarity index 96% rename from app/app.go rename to internal/app/app.go index d9cf13d..5ac0fea 100644 --- a/app/app.go +++ b/internal/app/app.go @@ -5,9 +5,9 @@ import ( "errors" "net/http" - "github.com/danilsolovyov/croupierCbrf/dal" - "github.com/danilsolovyov/croupierCbrf/handlers" - "github.com/danilsolovyov/croupierCbrf/worker" + "github.com/danilsolovyov/croupierCbrf/internal/dal" + "github.com/danilsolovyov/croupierCbrf/internal/handlers" + "github.com/danilsolovyov/croupierCbrf/internal/worker" "github.com/gorilla/mux" "github.com/skeris/appInit" "github.com/themakers/hlog" diff --git a/dal/hlogger.go b/internal/dal/hlogger.go similarity index 100% rename from dal/hlogger.go rename to internal/dal/hlogger.go diff --git a/dal/mongo.go b/internal/dal/mongo.go similarity index 100% rename from dal/mongo.go rename to internal/dal/mongo.go diff --git a/dal/quote.go b/internal/dal/quote.go similarity index 100% rename from dal/quote.go rename to internal/dal/quote.go diff --git a/handlers/handlers.go b/internal/handlers/handlers.go similarity index 97% rename from handlers/handlers.go rename to internal/handlers/handlers.go index 754bf39..00e0764 100644 --- a/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -2,9 +2,10 @@ package handlers import ( "encoding/json" - "github.com/danilsolovyov/croupierCbrf/dal" - "github.com/themakers/hlog" "net/http" + + "github.com/danilsolovyov/croupierCbrf/internal/dal" + "github.com/themakers/hlog" ) //#region ======== Handler Struct ======== diff --git a/handlers/quote.go b/internal/handlers/quote.go similarity index 92% rename from handlers/quote.go rename to internal/handlers/quote.go index fa2910a..9f0a2b5 100644 --- a/handlers/quote.go +++ b/internal/handlers/quote.go @@ -4,8 +4,8 @@ import ( "net/http" "strconv" - "github.com/danilsolovyov/croupierCbrf/dal" - "github.com/danilsolovyov/croupierCbrf/utils" + "github.com/danilsolovyov/croupierCbrf/internal/dal" + "github.com/danilsolovyov/croupierCbrf/internal/utils" ) func (h *Handler) GetQuotes(w http.ResponseWriter, r *http.Request) { diff --git a/utils/array_to_map.go b/internal/utils/array_to_map.go similarity index 100% rename from utils/array_to_map.go rename to internal/utils/array_to_map.go diff --git a/utils/array_to_map_test.go b/internal/utils/array_to_map_test.go similarity index 96% rename from utils/array_to_map_test.go rename to internal/utils/array_to_map_test.go index 9ebf735..ae8e020 100644 --- a/utils/array_to_map_test.go +++ b/internal/utils/array_to_map_test.go @@ -3,7 +3,7 @@ package utils_test import ( "testing" - "github.com/danilsolovyov/croupierCbrf/utils" + "github.com/danilsolovyov/croupierCbrf/internal/utils" "github.com/stretchr/testify/assert" ) diff --git a/utils/currency_exchange.go b/internal/utils/currency_exchange.go similarity index 95% rename from utils/currency_exchange.go rename to internal/utils/currency_exchange.go index 535a26b..8c2986b 100644 --- a/utils/currency_exchange.go +++ b/internal/utils/currency_exchange.go @@ -4,7 +4,7 @@ import ( "fmt" "math" - "github.com/danilsolovyov/croupierCbrf/dal" + "github.com/danilsolovyov/croupierCbrf/internal/dal" ) func CurrencyExchange(currencies map[string]dal.Quote, fromCurrency, toCurrency string, amount int64) (int64, error) { diff --git a/utils/currency_exchange_test.go b/internal/utils/currency_exchange_test.go similarity index 94% rename from utils/currency_exchange_test.go rename to internal/utils/currency_exchange_test.go index 11213b2..e1991ad 100644 --- a/utils/currency_exchange_test.go +++ b/internal/utils/currency_exchange_test.go @@ -4,8 +4,8 @@ import ( "fmt" "testing" - "github.com/danilsolovyov/croupierCbrf/dal" - "github.com/danilsolovyov/croupierCbrf/utils" + "github.com/danilsolovyov/croupierCbrf/internal/dal" + "github.com/danilsolovyov/croupierCbrf/internal/utils" "github.com/stretchr/testify/assert" ) diff --git a/worker/hlogger.go b/internal/worker/hlogger.go similarity index 100% rename from worker/hlogger.go rename to internal/worker/hlogger.go diff --git a/worker/worker.go b/internal/worker/worker.go similarity index 98% rename from worker/worker.go rename to internal/worker/worker.go index cb93c9e..af5a38f 100644 --- a/worker/worker.go +++ b/internal/worker/worker.go @@ -5,14 +5,15 @@ import ( "encoding/xml" "errors" "fmt" - "github.com/danilsolovyov/croupierCbrf/dal" - "github.com/themakers/hlog" - "golang.org/x/text/encoding/charmap" "io" "net/http" "strconv" "strings" "time" + + "github.com/danilsolovyov/croupierCbrf/internal/dal" + "github.com/themakers/hlog" + "golang.org/x/text/encoding/charmap" ) type worker struct {