-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (41 loc) · 1001 Bytes
/
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
package main
import (
"log"
"log/slog"
"os"
"github.com/gin-gonic/gin"
"source-score/pkg/api"
"source-score/pkg/conf"
"source-score/pkg/helpers"
"source-score/pkg/logger"
)
func main() {
// initialize the config
conf.LoadConfig()
// initialize the logger
slog.SetDefault(
slog.New(&logger.ContextHandler{
Handler: slog.NewJSONHandler(os.Stdout, nil),
}),
)
// initialize the server
loggerOpts := api.GinServerOptions{
Middlewares: []api.MiddlewareFunc{
// function to add request headers to log fields
func(c *gin.Context) {
for _, fieldKey := range helpers.ApiReqLogFields {
fieldVal := c.Request.Header.Get(fieldKey)
logger.AppendGinCtx(c, slog.String(fieldKey, fieldVal))
}
},
},
}
server := gin.Default()
api.RegisterHandlersWithOptions(server, api.NewRouter(), loggerOpts)
err := server.Run()
if err != nil {
log.Fatalf("failed to start the server : %s\n", err.Error())
} else {
log.Println("Server is up and running!")
}
}