-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (89 loc) · 2.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"errors"
"flag"
"fmt"
"net"
"os"
"time"
"github.com/common-nighthawk/go-figure"
"github.com/ksaritek/paper-rock-scissors/internal/api"
"github.com/ksaritek/paper-rock-scissors/internal/cmd"
"github.com/ksaritek/paper-rock-scissors/internal/domain/repository/memory"
"github.com/ksaritek/paper-rock-scissors/internal/domain/service"
"github.com/ksaritek/paper-rock-scissors/internal/logger"
"github.com/ksaritek/paper-rock-scissors/internal/observability"
"github.com/ksaritek/paper-rock-scissors/internal/version"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
gitRepo = "none"
gitCommit = "none"
buildDate = "none"
buildUser = "none"
buildTag = "none"
)
func main() {
initFigure := figure.NewFigure("!! Game Server !!", "", true)
initFigure.Print()
port := flag.String("port", "50051", "port to listen")
flag.Parse()
ver := version.Set(
gitRepo,
gitCommit,
buildDate,
buildUser,
buildTag,
)
l := logger.NewLogger("debug")
defer l.Sync()
l = l.With(zap.String("type", "grpc-server"))
ctx, stop := cmd.AppContext()
defer stop()
if value, present := os.LookupEnv("OTEL_EXPORTER_ENABLED"); present && value == "true" {
l.Info("otel exporter is enabled")
otelShutdown, err := observability.SetupOTelSDK(ctx)
if err != nil {
return
}
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
}()
}
address := fmt.Sprintf(":%s", *port)
lis, err := net.Listen("tcp", address)
if err != nil {
l.Fatal("failed to listen", zap.Error(err))
}
l.Info("listening on", zap.String("address", address))
grpcServer := cmd.Server(ctx, l)
repo := memory.NewMemoryStore(ctx, l, time.Minute*10)
srv := service.NewSessionService(ctx, l, service.GenerateSessionID, service.RandomMove, repo)
api.Routes(ctx, l, grpcServer, srv)
defer grpcServer.GracefulStop()
go func() {
if err := grpcServer.Serve(lis); err != nil {
l.Fatal("failed to serve", zap.Error(err))
}
}()
l.Info(
"game-server is started",
getLogParameters(ver)...,
)
<-ctx.Done()
l.Info("shutting down")
}
func getLogParameters(ver *version.Version) []zapcore.Field {
return []zapcore.Field{
zap.String("GoVersion", ver.GoVersion),
zap.String("GoOs", ver.GoOs),
zap.String("GoArch", ver.GoArch),
zap.String("GitRepo", ver.GitRepo),
zap.String("GitCommit", ver.GitCommit),
zap.String("BuildDate", ver.BuildDate),
zap.String("BuildUser", ver.BuildUser),
zap.String("BuildTag", ver.BuildTag),
}
}