Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
flew1x committed Aug 24, 2024
1 parent 2dca629 commit 669f0f5
Show file tree
Hide file tree
Showing 35 changed files with 1,269 additions and 216 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.bin
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DB_USER=string
DB_PASSWORD=string
DB_NAME=string

USER_JWT_SECRET=string
ADMIN_JWT_SECRET=string

CONFIG_PATH=configs
CONFIG_FILE=local.yml
File renamed without changes.
9 changes: 9 additions & 0 deletions .env.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DB_USER=
DB_PASSWORD=
DB_NAME=

USER_JWT_SECRET=
ADMIN_JWT_SECRET=

CONFIG_PATH=configs
CONFIG_FILE=prod.yml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ go.work

# Userdata
postgres
vendor
postgres-auth-data
85 changes: 85 additions & 0 deletions .golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
run:
deadline: 3m

build-tags:
- selinux
- seccomp

linters:
enable-all: true
disable:
- depguard
- gochecknoglobals
- gochecknoinits
- prealloc
- wrapcheck
- varnamelen
- errcheck
- nonamedreturns
- ireturn
- exhaustruct
- tagliatelle
- goimports
- exhaustive
- tagalign
- lll
- gofumpt
- gci
- funlen
- godot
- cyclop
- forcetypeassert
- gomnd
- execinquery

linters-settings:
govet:
settings:
printf:
funcs:
- (github.com/golang/glog.Verbose).Infof
- github.com/golang/glog.Infof
- github.com/golang/glog.Warningf
- github.com/golang/glog.Errorf
- github.com/golang/glog.Fatalf
- github.com/golang/glog.Exitf
golint:
min-confidence: 0
gocyclo:
min-complexity: 20

lll:
tab-width: 4
prealloc:
simple: true


issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck
- dupl
- gosec
- lll
- scopelint

- path: pkg/server/runtime/streaming.go
linters:
- gocyclo

- text: "vendor/github.com/kubernetes-sigs/cri-o/pkg/seccomp/seccomp.go"
linters:
- typecheck

- path: pkg/server
linters:
- lll

max-issues-per-linter: 0
max-same-issues: 0

service:
prepare:
- apt-get update
- apt-get install -y libseccomp-dev
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
FROM golang:1.22-alpine
FROM golang:1.22.3-alpine AS builder

WORKDIR /app

COPY . .

RUN go build ./cmd/app/main.go
RUN go build -o main ./cmd/app/main.go

FROM alpine:latest

WORKDIR /app

COPY --from=builder /app/main .

CMD ["./main"]
61 changes: 61 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.PHONY: run-docker-local
run-docker-local:
docker compose --env-file .env --env-file .env.local -f docker-compose.yml -f docker-compose.local.yml up --build --remove-orphans

.PHONY: stop-docker-local
stop-docker:
docker compose --env-file .env --env-file .env.local -f docker-compose.yml -f docker-compose.local.yml down

.PHONY: reset-docker-local
reset-docker-local:
docker compose --env-file .env --env-file .env.local -f docker-compose.yml -f docker-compose.local.yml down -v
docker compose --env-file .env --env-file .env.local -f docker-compose.yml -f docker-compose.local.yml up -d

.PHONY: run-docker-prod
run-docker-prod:
docker compose --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml up -d --build --remove-orphans

.PHONY: stop-docker-prod
stop-docker-prod:
docker compose --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml down

.PHONY: reset-docker-prod
reset-docker-prod:
docker compose --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml down -v
docker compose --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml up -d

.PHONY: pull-images
pull-images:
docker compose --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml pull

.PHONY: build

.DEFAULT_GOAL := build

.PHONY: lint
lint:
golangci-lint run --config .golangci-lint.yaml

.PHONY: build
build:
go build -o ./.bin/server cmd/app/main.go

.PHONY: build-docker
build-docker:
docker build -t auth .

.PHONY: run
run: build
./.bin/server

.PHONY: test
test:
go test -v ./...

.PHONY: test-cover
test-cover:
go test -coverprofile=coverage.out -covermode=atomic ./...

.PHONY: clean
clean:
rm -rf ./.bin
9 changes: 8 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"msauth/internal/app"
"msauth/internal/config"
Expand All @@ -20,7 +21,9 @@ func main() {
cfg := config.NewConfig()
cfg.InitConfig(os.Getenv("CONFIG_PATH"), os.Getenv("CONFIG_FILE"))

newLogger := logger.InitLogger(cfg.LoggerConfig.GetLoggingMode())
parsedMode := logger.ParseMode(cfg.LoggerConfig.GetLoggingMode())
logLevel := logger.ConvertLoggerMode(parsedMode)
newLogger := logger.InitLogger(logLevel)

application, err := app.New(newLogger, cfg)
if err != nil {
Expand All @@ -35,6 +38,10 @@ func main() {
return application.GRPCServer.Run()
})

errorGroup.Go(func() error {
return application.RESTServer.RunREST(context.Background())
})

errorGroup.Go(func() error {
return gracefulStop(ErrGracefulStop)
})
Expand Down
3 changes: 3 additions & 0 deletions configs/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ grpc:
port: 22000
timeout: 5000

rest:
host: "0.0.0.0:8081"

logging:
mode: "dev"

Expand Down
3 changes: 3 additions & 0 deletions configs/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ grpc:
port:
timeout:

rest:
port:

logging:
mode: "prod"

Expand Down
12 changes: 12 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.9"

services:
auth:
build:
context: .
dockerfile: Dockerfile
env_file:
- .env.local

postgres:
env_file: .env.local
19 changes: 19 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3.9"

services:
auth:
env_file:
- .env.prod
deploy:
replicas: 1
update_config:
parallelism: 1
delay: 10s
order: start-first
resources:
limits:
memory: '512M'
cpus: '0.5'

postgres:
env_file: .env.prod
70 changes: 37 additions & 33 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
version: "3.9"

services:
postgres:
image: postgres:16.2-alpine
container_name: postgres
restart: on-failure
env_file: .env
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
volumes:
- ./postgres-auth/data:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- auth_network
auth:
image: your-registry/auth
container_name: auth
env_file:
- .env
environment:
- PGSQL_PASSWORD=${DB_PASSWORD}
restart: on-failure
ports:
- "5000:22000"
- "8085:8081"
depends_on:
- postgres
volumes:
- ./configs:/app/configs
networks:
- network

auth:
image: auth
build:
context: .
dockerfile: ./Dockerfile
container_name: auth
env_file:
- .env
environment:
- POSTGRES_PASSWORD=${DB_PASSWORD}
restart: on-failure
ports:
- "22000:22000"
depends_on:
- postgres
networks:
- auth_network
postgres:
image: postgres:16.2-alpine
restart: on-failure
env_file: .env
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
volumes:
- ./postgres-auth-data:/var/lib/postgresql/data
ports:
- "5500:5432"
networks:
- network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
interval: 5s
timeout: 5s
retries: 5

networks:
auth_network:
network:
driver: bridge
Loading

0 comments on commit 669f0f5

Please sign in to comment.