-
Notifications
You must be signed in to change notification settings - Fork 631
Expand file tree
/
Copy pathmain_web.go
More file actions
82 lines (69 loc) · 1.67 KB
/
main_web.go
File metadata and controls
82 lines (69 loc) · 1.67 KB
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
//go:build web
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"tinyrdm/backend/api"
"tinyrdm/backend/services"
)
var version = "0.0.0"
func main() {
// Wire up event bridge callbacks (breaks import cycle: services -> api)
services.EmitEventFunc = func(event string, data any) {
api.Hub().Emit(event, data)
}
services.RegisterHandlerFunc = api.RegisterHandler
// Initialize all services with a background context
ctx := context.Background()
sysSvc := services.System()
connSvc := services.Connection()
browserSvc := services.Browser()
cliSvc := services.Cli()
monitorSvc := services.Monitor()
pubsubSvc := services.Pubsub()
prefSvc := services.Preferences()
prefSvc.SetAppVersion(version)
prefSvc.UpdateEnv()
// Start services
sysSvc.Start(ctx, version)
connSvc.Start(ctx)
browserSvc.Start(ctx)
cliSvc.Start(ctx)
monitorSvc.Start(ctx)
pubsubSvc.Start(ctx)
services.GA().SetSecretKey("", "")
// Initialize auth
api.InitAuth()
// Get port from env or default
port := os.Getenv("PORT")
if port == "" {
port = "8088"
}
// Setup HTTP server
router := api.SetupRouter()
srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", port),
Handler: router,
}
// Graceful shutdown
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
log.Println("Shutting down...")
browserSvc.Stop()
cliSvc.CloseAll()
monitorSvc.StopAll()
pubsubSvc.StopAll()
srv.Close()
}()
log.Printf("Tiny RDM Web starting on http://0.0.0.0:%s", port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Failed to start server: %v", err)
}
}