refactor: layout & settings
This commit is contained in:
parent
0810d82411
commit
ef5bbf28c0
8
.dockerignore
Normal file
8
.dockerignore
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
.mockery.yaml
|
||||||
|
.golangci.yaml
|
||||||
|
.gitlab-ci.yaml
|
||||||
|
.gitingore
|
||||||
|
.Makefile
|
||||||
|
.README.md
|
||||||
|
deployments
|
||||||
|
tests
|
72
Dockerfile
Normal file
72
Dockerfile
Normal file
@ -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"]
|
47
Makefile
Normal file
47
Makefile
Normal file
@ -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
|
@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/danilsolovyov/croupierCbrf/app"
|
"github.com/danilsolovyov/croupierCbrf/internal/app"
|
||||||
"github.com/skeris/appInit"
|
"github.com/skeris/appInit"
|
||||||
)
|
)
|
||||||
|
|
26
deployments/dev/docker-compose.yaml
Normal file
26
deployments/dev/docker-compose.yaml
Normal file
@ -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:
|
28
deployments/staging/docker-compose.yaml
Normal file
28
deployments/staging/docker-compose.yaml
Normal file
@ -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
|
||||||
|
|
35
deployments/test/docker-compose.yaml
Normal file
35
deployments/test/docker-compose.yaml
Normal file
@ -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:
|
@ -5,9 +5,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/danilsolovyov/croupierCbrf/dal"
|
"github.com/danilsolovyov/croupierCbrf/internal/dal"
|
||||||
"github.com/danilsolovyov/croupierCbrf/handlers"
|
"github.com/danilsolovyov/croupierCbrf/internal/handlers"
|
||||||
"github.com/danilsolovyov/croupierCbrf/worker"
|
"github.com/danilsolovyov/croupierCbrf/internal/worker"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/skeris/appInit"
|
"github.com/skeris/appInit"
|
||||||
"github.com/themakers/hlog"
|
"github.com/themakers/hlog"
|
@ -2,9 +2,10 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/danilsolovyov/croupierCbrf/dal"
|
|
||||||
"github.com/themakers/hlog"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/danilsolovyov/croupierCbrf/internal/dal"
|
||||||
|
"github.com/themakers/hlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
//#region ======== Handler Struct ========
|
//#region ======== Handler Struct ========
|
@ -4,8 +4,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/danilsolovyov/croupierCbrf/dal"
|
"github.com/danilsolovyov/croupierCbrf/internal/dal"
|
||||||
"github.com/danilsolovyov/croupierCbrf/utils"
|
"github.com/danilsolovyov/croupierCbrf/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handler) GetQuotes(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) GetQuotes(w http.ResponseWriter, r *http.Request) {
|
@ -3,7 +3,7 @@ package utils_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/danilsolovyov/croupierCbrf/utils"
|
"github.com/danilsolovyov/croupierCbrf/internal/utils"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"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) {
|
func CurrencyExchange(currencies map[string]dal.Quote, fromCurrency, toCurrency string, amount int64) (int64, error) {
|
@ -4,8 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/danilsolovyov/croupierCbrf/dal"
|
"github.com/danilsolovyov/croupierCbrf/internal/dal"
|
||||||
"github.com/danilsolovyov/croupierCbrf/utils"
|
"github.com/danilsolovyov/croupierCbrf/internal/utils"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
@ -5,14 +5,15 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/danilsolovyov/croupierCbrf/dal"
|
|
||||||
"github.com/themakers/hlog"
|
|
||||||
"golang.org/x/text/encoding/charmap"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/danilsolovyov/croupierCbrf/internal/dal"
|
||||||
|
"github.com/themakers/hlog"
|
||||||
|
"golang.org/x/text/encoding/charmap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type worker struct {
|
type worker struct {
|
Loading…
Reference in New Issue
Block a user