-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
90 lines (77 loc) · 2 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
package main
import (
"context"
"log"
"os"
"os/signal"
"strings"
"github.com/watzon/0x45/internal/config"
"github.com/watzon/0x45/internal/server"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// @title 0x45 API
// @version 1.0
// @description API for 0x45
// @license.name MIT
// @license.url https://github.com/watzon/0x45/blob/main/LICENSE
// @host localhost:3000
// @BasePath /
func main() {
// Load config
cfg, err := config.Load()
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
// Initialize logger with environment-based level
logLevel := getLogLevel()
logConfig := zap.NewDevelopmentConfig()
logConfig.Level = zap.NewAtomicLevelAt(logLevel)
logger, err := logConfig.Build()
if err != nil {
log.Fatalf("failed to initialize logger: %v", err)
}
defer func() { _ = logger.Sync() }()
logger.Info("logger initialized", zap.String("level", logLevel.String()))
// Initialize server with storage manager
srv := server.New(cfg, logger)
// Create and setup server
srv.SetupRoutes()
go func() {
// Start server
log.Printf("Starting server on %s", cfg.Server.Address)
log.Fatal(srv.Start(cfg.Server.Address))
}()
// Wait for shutdown signal and initiate graceful shutdown once received.
<-ctx.Done()
defer func() {
if err := srv.Cleanup(); err != nil {
log.Printf("failed cleaning up server: %v", err)
}
}()
log.Print("shutdown signal received, initiate shutdown")
if err := srv.Shutdown(context.Background()); err != nil {
log.Printf("failed shutting down gracefully: %v", err)
}
}
func getLogLevel() zapcore.Level {
env := os.Getenv("0X_LOG_LEVEL")
if env == "" {
return zapcore.InfoLevel
}
switch strings.ToLower(env) {
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
default:
log.Fatalf("unknown log level: %s", env)
return zapcore.InfoLevel
}
}