Skip to content

Commit

Permalink
feat: add relationship-manager-service
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Nov 3, 2024
1 parent 4239530 commit 4d4ea3c
Show file tree
Hide file tree
Showing 20 changed files with 1,768 additions and 47 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
lint:
golangci-lint run

go-tidy:
go work sync
go mod tidy
# Loop through all directories containing a go.mod file and run go mod tidy in each
find . -type f -name "go.mod" -execdir go mod tidy \;

pre-commit:
go-tidy
lint
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ use (
./services/matchmaker
./services/mc-player-service
./services/message-handler
./services/relationship-manager-service
)
59 changes: 12 additions & 47 deletions go.work.sum

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions services/relationship-manager-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS build

ARG SERVICE_NAME=relationship-manager-service

WORKDIR /build

# Copy sources
COPY . .

WORKDIR /build/services/$SERVICE_NAME

RUN go mod download

WORKDIR /build

ARG TARGETOS
ARG TARGETARCH

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 \
GOOS=$TARGETOS \
GOARCH=$TARGETARCH \
go build -ldflags="-s -w" -o $SERVICE_NAME ./services/$SERVICE_NAME/cmd/$SERVICE_NAME

FROM alpine

ARG SERVICE_NAME=relationship-manager-service

WORKDIR /app

COPY --from=build /build/$SERVICE_NAME /build/services/$SERVICE_NAME/run ./
CMD ["./relationship-manager-service"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/app"
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/config"
"go.uber.org/zap"
"log"
)

func main() {
cfg, err := config.LoadGlobalConfig()
if err != nil {
log.Fatalf("failed to load config: %v", err)
}

logger, err := createLogger(cfg)
if err != nil {
log.Fatal(err)
}

app.Run(cfg, logger)
}

func createLogger(cfg *config.Config) (*zap.SugaredLogger, error) {
var logger *zap.Logger
var err error
if cfg.Development {
logger, err = zap.NewDevelopment()
} else {
logger, err = zap.NewProduction()
}
if err != nil {
return nil, err
}
return logger.Sugar(), nil
}
51 changes: 51 additions & 0 deletions services/relationship-manager-service/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module github.com/emortalmc/mono-services/services/relationship-manager-service

go 1.23

require (
github.com/emortalmc/proto-specs/gen/go v0.0.0-20240927103241-2584fd28e0f9
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/segmentio/kafka-go v0.4.47
github.com/spf13/viper v1.19.0
go.mongodb.org/mongo-driver v1.17.1
go.uber.org/zap v1.27.0
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.35.1
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
249 changes: 249 additions & 0 deletions services/relationship-manager-service/go.sum

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions services/relationship-manager-service/internal/app/relationship.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package app

import (
"context"
"go.uber.org/zap"
"os/signal"
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/clients"
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/config"
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/kafka"
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/repository"
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/service"
"sync"
"syscall"
)

func Run(cfg *config.Config, logger *zap.SugaredLogger) {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
wg := &sync.WaitGroup{}

// Mongo and Kafka get a delayed context to make sure they are shut down after requests are finished.
delayedWg := &sync.WaitGroup{}
delayedCtx, delayedCancel := context.WithCancel(ctx)

repo, err := repository.NewMongoRepository(delayedCtx, logger, delayedWg, cfg.MongoDB)
if err != nil {
logger.Fatalw("failed to create repository", "error", err)
}

notif := kafka.NewKafkaNotifier(delayedCtx, wg, cfg.Kafka, logger)
if err != nil {
logger.Fatalw("failed to create notifier", "error", err)
}

service.RunServices(ctx, logger, wg, cfg, repo, notif)

playerTracker, err := clients.NewPlayerTrackerClient(cfg.PlayerTrackerService)
if err != nil {
logger.Fatalw("failed to create player tracker client", "error", err)
}

kafka.NewConsumer(ctx, wg, cfg.Kafka, logger, repo, notif, playerTracker)

wg.Wait()
logger.Info("stopped services")

logger.Info("shutting down repository and kafka")
delayedCancel()
delayedWg.Wait()
}
27 changes: 27 additions & 0 deletions services/relationship-manager-service/internal/clients/creator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package clients

import (
"fmt"
"github.com/emortalmc/mono-services/services/relationship-manager-service/internal/config"
"github.com/emortalmc/proto-specs/gen/go/grpc/mcplayer"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func NewPlayerTrackerClient(cfg *config.PlayerTrackerServiceConfig) (mcplayer.PlayerTrackerClient, error) {
lis, err := createConnection(cfg.Host, cfg.Port)
if err != nil {
return nil, err
}

return mcplayer.NewPlayerTrackerClient(lis), nil
}

func createConnection(addr string, port uint16) (*grpc.ClientConn, error) {
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", addr, port), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}

return conn, nil
}
48 changes: 48 additions & 0 deletions services/relationship-manager-service/internal/config/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package config

import (
"github.com/spf13/viper"
"strings"
)

type Config struct {
Kafka *KafkaConfig
MongoDB *MongoDBConfig

PlayerTrackerService *PlayerTrackerServiceConfig

Development bool
Port uint16
}

type KafkaConfig struct {
Host string
Port int
}

type MongoDBConfig struct {
URI string
}

type PlayerTrackerServiceConfig struct {
Host string
Port uint16
}

func LoadGlobalConfig() (cfg *Config, err error) {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

viper.SetConfigName("config")
viper.AddConfigPath(".")

if err = viper.ReadInConfig(); err != nil {
return
}

if err = viper.Unmarshal(&cfg); err != nil {
return
}

return
}
Loading

0 comments on commit 4d4ea3c

Please sign in to comment.