-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (65 loc) · 2.04 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
package main
import (
"context"
"embed"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/nodeboxhq/nodebox-dashboard/internal/cmd"
"github.com/nodeboxhq/nodebox-dashboard/internal/config"
"github.com/nodeboxhq/nodebox-dashboard/internal/db"
"github.com/nodeboxhq/nodebox-dashboard/internal/handlers"
"github.com/nodeboxhq/nodebox-dashboard/internal/handlers/middleware"
"github.com/nodeboxhq/nodebox-dashboard/internal/logger"
"github.com/nodeboxhq/nodebox-dashboard/internal/services"
)
//go:embed web/build/*
var webFS embed.FS
func main() {
handlers.EmbeddedWebFS = webFS
cmd.AsciiArt()
cfg := config.ParseConfig(cmd.ParseFlags())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
serviceRegistry := services.NewServiceRegistry(db.SetupDatabase(cfg))
sS := serviceRegistry.StatsService
hS := serviceRegistry.HostService
nS := serviceRegistry.NodeService
hS.InstallService()
go sS.StartStatsCollection(ctx)
go hS.StartHostInfoCollection(ctx)
go hS.StartUpdateChecker(ctx)
go nS.GetNodeInfo()
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard
gin.DefaultErrorWriter = io.Discard
r := gin.New()
r.Use(middleware.CORSConfig())
handlers.RegisterRoutes(r, cfg.Environment, sS, hS, nS)
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.IP, cfg.Port),
Handler: r,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.L.Fatal().Any("error", err).Msg("Failed to start server")
}
}()
logger.L.Info().Msgf("Server started on %s:%d", cfg.IP, cfg.Port)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.L.Info().Msg("Shutting down gracefully...")
cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.L.Fatal().Err(err).Msg("Server forced to shutdown")
}
logger.L.Info().Msg("Server exited properly")
}