-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (61 loc) · 1.66 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
package main
import (
"context"
"log"
"os"
"os/signal"
"github.com/watzon/0x45/internal/config"
"github.com/watzon/0x45/internal/database"
"github.com/watzon/0x45/internal/server"
"github.com/watzon/0x45/internal/storage"
)
// @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)
}
// Initialize database
db, err := database.New(cfg)
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
// Run migrations
if err := db.Migrate(cfg); err != nil {
log.Fatalf("Error running migrations: %v", err)
}
// Initialize storage manager
storageManager, err := storage.NewStorageManager(cfg)
if err != nil {
log.Fatalf("Failed to initialize storage: %v", err)
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
// Initialize server with storage manager
srv := server.New(db, storageManager, cfg)
// 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)
}
}