Skip to content
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
56 changes: 56 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,62 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] v0.3.3

## 10 Feb 2025

### Added

- HostPort condition with "localhost" for dev and service name for prod

### Changed

- Events service name (event -> events)
- http.addr is without port now

## 7 Feb 2025

### Added

- Fighters service Dockerfile
- Events service Dockerfile
- Auth service Dockerfile
- Gateway service Dockerfile
- Postgres Dockerfile
- docker-compose file

## 6 Feb 2025

### Added

- sql init files
- fighters collection in fighters service

### Changed

- filepath for fighters json

## 4 Feb 2025

### Added

- --env flag for config selection
- nginx conf

### ### Changed

- cfg_backup script updated

## 31 Jan 2025

### Added

- Redis registry script

### Changed

- Consul registry script name
- Health check time interval 1s -> 15s
- Gate-way service name

## 30 Jan 2025

### Changed
Expand Down
38 changes: 38 additions & 0 deletions auth/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM golang:alpine as build

RUN apk --update add ca-certificates git

WORKDIR /build

COPY go.mod go.sum /build/
COPY auth/main.go /build/auth/
COPY auth/cmd /build/auth/cmd
COPY auth/internal /build/auth/internal
COPY auth/migrations /build/auth/migrations
COPY auth/pkg /build/auth/pkg
COPY pkg/discovery /build/pkg/discovery
COPY pkg/logger /build/pkg/logger
COPY pkg/model /build/pkg/model
COPY pkg/sigx /build/pkg/sigx
COPY pkg/pgxs /build/pkg/pgxs
COPY pkg/utils /build/pkg/utils
COPY gen /build/gen

RUN go mod tidy

RUN GOOS=linux CGO_ENABLED=0 go build -o auth-service ./auth


FROM alpine:latest as runtime

RUN apk --update add ca-certificates

WORKDIR /app

COPY documentation/openapi/yaml /app/api-docs
COPY auth/configs /app/configs
COPY auth/hack/dev/certs /app/hack/dev/certs
COPY --from=build /build/auth-service /app/

ENTRYPOINT ["/app/auth-service"]
CMD ["serve", "auth", "--env", "prod"]
26 changes: 18 additions & 8 deletions auth/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
"strings"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/DoRightt/pickfighter-server/auth/internal/controller/auth"
grpchandler "github.com/DoRightt/pickfighter-server/auth/internal/handler/grpc"
"github.com/DoRightt/pickfighter-server/auth/internal/repository/psql"
service "github.com/DoRightt/pickfighter-server/auth/internal/service/auth"
"github.com/DoRightt/pickfighter-server/auth/pkg/version"
"github.com/DoRightt/pickfighter-server/pkg/discovery"
"github.com/DoRightt/pickfighter-server/pkg/discovery/consul"
"github.com/DoRightt/pickfighter-server/pkg/discovery/redis"
logs "github.com/DoRightt/pickfighter-server/pkg/logger"
"github.com/DoRightt/pickfighter-server/pkg/model"
"github.com/DoRightt/pickfighter-server/pkg/sigx"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)

var allowedApiRoutes = []string{
Expand Down Expand Up @@ -67,20 +68,29 @@ func validateServerArgs(cmd *cobra.Command, args []string) error {
// runServe is the main function executed when the serve command is run.
// It initializes the application, sets up service and runs the HTTP server.
func runServe(cmd *cobra.Command, args []string) {
var hostName string
port := viper.GetInt("http.port")
serviceName := version.Name
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if viper.GetString("app.env") == "prod" {
hostName = serviceName
} else {
hostName = "localhost"
}

route := args[0]

app := service.New()

registry, err := consul.NewRegistry("localhost:8500")
registry, err := redis.NewRegistry(viper.GetString("registry.redis.url"))
if err != nil {
panic(err)
}
instanceID := discovery.GenerateInstanceID(app.ServiceName)
if err := registry.Register(ctx, instanceID, app.ServiceName, fmt.Sprintf("localhost:%d", port)); err != nil {

instanceID := discovery.GenerateInstanceID(serviceName)
if err := registry.Register(ctx, instanceID, serviceName, fmt.Sprintf("%s:%d", hostName, port)); err != nil {
panic(err)
}

Expand All @@ -90,7 +100,7 @@ func runServe(cmd *cobra.Command, args []string) {
logs.Error("Failed to report healthy state", zap.Error(err))
}

time.Sleep(1 * time.Second)
time.Sleep(15 * time.Second)
}
}()
defer registry.Deregister(ctx, instanceID, app.ServiceName)
Expand Down
14 changes: 9 additions & 5 deletions auth/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"os"
"time"

"github.com/DoRightt/pickfighter-server/auth/pkg/logger"
"github.com/DoRightt/pickfighter-server/auth/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap/zapcore"
"github.com/DoRightt/pickfighter-server/auth/pkg/logger"
"github.com/DoRightt/pickfighter-server/auth/pkg/version"
)

var (
Expand Down Expand Up @@ -49,9 +49,11 @@ func init() {
rootCmd.PersistentFlags().String("name", version.Name, "Application name label")
rootCmd.PersistentFlags().Bool("log_json", false, "Enable JSON formatted logs output")
rootCmd.PersistentFlags().Int("log_level", int(zapcore.DebugLevel), "Log level")
rootCmd.PersistentFlags().String("env", "dev", "Application runtime environment")

bindViperPersistentFlag(rootCmd, "config_path", "config")
bindViperPersistentFlag(rootCmd, "app.name", "name")
bindViperPersistentFlag(rootCmd, "app.env", "env")
bindViperPersistentFlag(rootCmd, "log_json", "log_json")
bindViperPersistentFlag(rootCmd, "log_level", "log_level")

Expand All @@ -76,9 +78,11 @@ func initConfig() {
if cfgPath != "" {
viper.SetConfigFile(cfgPath)
} else {
env := viper.GetString("app.env")

viper.AddConfigPath("./configs")
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.SetConfigName("config." + env)
}

viper.AutomaticEnv()
Expand All @@ -97,7 +101,7 @@ func setConfigDefaults() {
viper.SetDefault("app.run_date", time.Unix(version.RunDate, 0).Format(time.RFC1123))

// http server
viper.SetDefault("http.addr", "127.0.0.1:9092")
viper.SetDefault("http.addr", "127.0.0.1")
viper.SetDefault("http.port", "9092")
viper.SetDefault("http.ssl.enabled", false)

Expand All @@ -107,7 +111,7 @@ func setConfigDefaults() {
viper.SetDefault("postgres.main.port", "5432")
viper.SetDefault("postgres.main.name", "postgres")
viper.SetDefault("postgres.main.user", "postgres")

// web
viper.SetDefault("web.host", "http://localhost")
viper.SetDefault("web.port", "4200")
Expand Down
2 changes: 1 addition & 1 deletion auth/internal/repository/psql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package psql
import (
"context"

"github.com/DoRightt/pickfighter-server/fighters/pkg/cfg"
"github.com/DoRightt/pickfighter-server/auth/pkg/cfg"
"github.com/DoRightt/pickfighter-server/pkg/pgxs"
)

Expand Down
8 changes: 4 additions & 4 deletions auth/internal/service/auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"fmt"
"net"
"strings"

grpchandler "github.com/DoRightt/pickfighter-server/auth/internal/handler/grpc"
"github.com/DoRightt/pickfighter-server/auth/pkg/version"
Expand Down Expand Up @@ -49,17 +48,18 @@ func (s *ApiService) Init(h *grpchandler.Handler) error {
func (s *ApiService) Run() error {
port := viper.GetString("http.port")
srvAddr := viper.GetString("http.addr")
if len(srvAddr) < 1 || !strings.Contains(srvAddr, ":") {

if len(srvAddr) < 1 || len(port) < 1 {
return fmt.Errorf("'%s' service address not specified", s.ServiceName)
}

lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%v", port))
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%v", srvAddr, port))
if err != nil {
return err
}

logs.Infof("Start listen '%s' http: %s", s.ServiceName, srvAddr)
fmt.Printf("Server is listening at: %s\n", srvAddr)
fmt.Printf("Server is listening at: %s\n", fmt.Sprintf("%s:%v", srvAddr, port))

return s.Server.Serve(lis)
}
117 changes: 117 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
services:
db:
container_name: pickfighter-db
build:
context: .
dockerfile: migrations/init/Dockerfile
volumes:
- postgres:/var/lib/postgresql/data/pgdata
expose:
- "5432"
environment:
PGDATA: "/var/lib/postgresql/data/pgdata"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: ${DB_PASS}
POSTGRES_DB: "pickfighter"
POSTGRES_HOST_AUTH_METHOD: "trust"
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 10s
retries: 5
start_period: 30s
timeout: 10s
networks:
- pickfighter_network

redis:
image: redis
container_name: dev-redis
expose:
- "6379"
networks:
- pickfighter_network

pickfighter-gateway-service:
image: pickfighter-service-image
container_name: pickfighter-gateway-service
build:
context: .
dockerfile: pickfighter/Dockerfile
depends_on:
- redis
environment:
- REDIS_URL=redis:6379
networks:
- pickfighter_network

auth-service:
image: auth-service-image
container_name: auth-service
build:
context: .
dockerfile: auth/Dockerfile
depends_on:
- redis
- db
environment:
- REDIS_URL=redis:6379
- DB_URL=postgres://postgres@db:5432/pickfighter
expose:
- "9092"
networks:
- pickfighter_network


fighters-service:
image: fighters-service-image
container_name: fighters-service
build:
context: .
dockerfile: fighters/Dockerfile
depends_on:
- redis
- db
environment:
- REDIS_URL=redis:6379
- DB_URL=postgres://postgres@db:5432/pickfighter
expose:
- "9093"
networks:
- pickfighter_network

events-service:
image: events-service-image
container_name: events-service
build:
context: .
dockerfile: events/Dockerfile
depends_on:
- redis
- db
environment:
- REDIS_URL=redis:6379
- DB_URL=postgres://postgres@db:5432/pickfighter
expose:
- "9094"
networks:
- pickfighter_network

nginx:
image: nginx:latest
container_name: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "8081:8080"
depends_on:
- pickfighter-gateway-service
networks:
- pickfighter_network

networks:
pickfighter_network:
name: pickfighter_network
driver: bridge

volumes:
postgres:
Loading
Loading