Skip to content

Commit

Permalink
feat: create simulation service
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldooli committed Dec 20, 2024
1 parent 27b460b commit b31dcd0
Show file tree
Hide file tree
Showing 12 changed files with 545 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/simulation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Publish Simulation Service
on:
workflow_dispatch:

jobs:
build-and-push:
name: Build and Publish
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Get the version (release tag)
run: echo "IMAGE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: ./
file: ./simulation/Dockerfile
push: true
tags: tfgco/maestro-simulation:${{ env.IMAGE_VERSION }}
18 changes: 18 additions & 0 deletions simulation/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.23-alpine AS build

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o room .

FROM alpine:edge

WORKDIR /app
COPY --from=build /app/room .

RUN apk --no-cache add ca-certificates tzdata

ENTRYPOINT ["/app/room"]
38 changes: 38 additions & 0 deletions simulation/app/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package app

import (
"context"
"errors"
"net/http"

ginadapter "git.topfreegames.com/maestro/test-service/internal/adapters/gin"
"git.topfreegames.com/maestro/test-service/internal/core"
"github.com/gin-gonic/gin"
)

type Server struct {
RoomManager *core.RoomManager
httpServer *http.Server
}

func (s *Server) Start() {
handler := ginadapter.RoomHandler{RoomManager: s.RoomManager}

r := gin.Default()
r.PUT("/room", handler.UpdateGameRoom)
r.PUT("/status", handler.UpdateStatus)
r.PUT("/matches", handler.UpdateRunningMatches)

s.httpServer = &http.Server{
Addr: ":8080",
Handler: r.Handler(),
}

if err := s.httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}

func (s *Server) Shutdown(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
}
45 changes: 45 additions & 0 deletions simulation/app/woker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package app

import (
"context"
"log"
"time"

"git.topfreegames.com/maestro/test-service/internal/core"
)

type Worker struct {
Worker *core.Worker

ticker *time.Ticker
quit chan bool
cancel context.CancelFunc
}

func (w *Worker) Start() {
w.ticker = time.NewTicker(1 * time.Second)
w.quit = make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
w.cancel = cancel

go func() {
for {
select {
case <-w.quit:
return
case <-w.ticker.C:
go w.Worker.Sync(ctx)
}
}
}()
}

func (w *Worker) Stop(_ context.Context) {
defer w.cancel()
w.ticker.Stop()

log.Println("Stopping worker...")
time.Sleep(2 * time.Second) // wait ongoing sync
w.quit <- true
log.Println("Worker stopped")
}
47 changes: 47 additions & 0 deletions simulation/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module git.topfreegames.com/maestro/test-service

go 1.23.2

require (
github.com/gin-gonic/gin v1.10.0
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/env v1.0.0
github.com/knadh/koanf/providers/file v1.1.2
github.com/knadh/koanf/providers/structs v0.1.0
github.com/knadh/koanf/v2 v2.1.2
)

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
63 changes: 63 additions & 0 deletions simulation/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
github.com/knadh/koanf/providers/env v1.0.0/go.mod h1:mzFyRZueYhb37oPmC1HAv/oGEEuyvJDA98r3XAa8Gak=
github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
github.com/knadh/koanf/providers/structs v0.1.0/go.mod h1:sw2YZ3txUcqA3Z27gPlmmBzWn1h8Nt9O6EP/91MkcWE=
github.com/knadh/koanf/v2 v2.1.2/go.mod h1:Gphfaen0q1Fc1HTgJgSTC4oRX9R2R5ErYMZJy8fLJBo=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
68 changes: 68 additions & 0 deletions simulation/internal/adapters/gin/room.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package gin

import (
"fmt"

"git.topfreegames.com/maestro/test-service/internal/core"
"github.com/gin-gonic/gin"
)

type RoomHandler struct {
RoomManager *core.RoomManager
}

type Request struct {
Status string `form:"status"`
RunningMatches int `form:"running_matches"`
}

func (h *RoomHandler) UpdateGameRoom(c *gin.Context) {
status, runningMatches, err := parseRequest(c)
if err != nil {
c.JSON(400, gin.H{"error": "invalid request"})
return
}

room := h.RoomManager.UpdateGameRoom(status, runningMatches)

c.JSON(200, gin.H{"status": room.Status, "runningMatches": room.RunningMatches})
}

func (h *RoomHandler) UpdateStatus(c *gin.Context) {
status, _, err := parseRequest(c)
if err != nil {
c.JSON(400, gin.H{"error": "invalid request"})
return
}

room := h.RoomManager.UpdateStatus(status)

c.JSON(200, gin.H{"status": room.Status, "runningMatches": room.RunningMatches})
}

func (h *RoomHandler) UpdateRunningMatches(c *gin.Context) {
_, runningMatches, err := parseRequest(c)
if err != nil {
c.JSON(400, gin.H{"error": "invalid request"})
return
}

room := h.RoomManager.UpdateRunningMatches(runningMatches)

c.JSON(200, gin.H{"status": room.Status, "runningMatches": room.RunningMatches})
}

func parseRequest(c *gin.Context) (core.Status, int, error) {
var req Request

if c.ShouldBind(&req) != nil {
return core.Unknown, 0, fmt.Errorf("invalid request")
}

status, err := core.StatusFromString(req.Status)
if err != nil {
return core.Unknown, 0, err
}

return status, req.RunningMatches, nil
}
61 changes: 61 additions & 0 deletions simulation/internal/core/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package core

import (
"log"
"strings"
"time"

"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
)

type RoomConfig struct {
ID string `koanf:"id"`
}

type SchedulerConfig struct {
Name string `koanf:"name"`
}

type Config struct {
SyncInterval time.Duration `koanf:"syncinterval"`
InitialStatus string `koanf:"initialstatus"`
URL string `koanf:"url"`
Room RoomConfig `koanf:"room"`
Scheduler SchedulerConfig `koanf:"scheduler"`
}

func NewDefaultConfig() *Config {
return &Config{
SyncInterval: 5 * time.Second,
InitialStatus: "unready",
}
}

func LoadConfig() *Config {
k := koanf.New(".")

_ = k.Load(structs.Provider(NewDefaultConfig(), "room"), nil)

// This is to load MAESTRO_* environment variables
if err := k.Load(env.Provider("", ".", parseEnv), nil); err != nil {
log.Fatalf("error loading config from env: %v", err)
}

if err := k.Load(env.Provider("SIMULATION_", ".", parseEnv), nil); err != nil {
log.Fatalf("error loading config from env: %v", err)
}

var config Config
if err := k.Unmarshal("", &config); err != nil {
log.Fatalf("error unmarshalling config: %v", err)
}

return &config
}

func parseEnv(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "SIMULATION_")), "_", ".", -1)
}
Loading

0 comments on commit b31dcd0

Please sign in to comment.