-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (64 loc) · 1.54 KB
/
Copy pathmain.go
File metadata and controls
83 lines (64 loc) · 1.54 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
83
package main
import (
"context"
"farm_management_system/fleet"
"log"
"os"
"os/signal"
"syscall"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/logger"
"github.com/gofiber/fiber/v3/middleware/recover"
)
func main() {
appCtx, appCancel := context.WithCancel(context.Background())
defer appCancel()
setupShutdownListener(appCancel)
fms, err := NewFMS(
appCtx,
WithAppName("FMS v1.0"),
WithPort(3000),
)
if err != nil {
log.Fatal("Failed to initialize FMS:", err)
}
app := fiber.New(fiber.Config{
AppName: "FMS v1.0",
})
mapRoutes(app)
//TODO: this will run any stored devices in db on startup
registerExistingDevices(fms)
go func() {
<-appCtx.Done()
log.Println("Shutting down HTTP server...")
app.Shutdown()
}()
log.Fatal(app.Listen(":3000"))
}
func setupShutdownListener(appCancel context.CancelFunc) {
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.Println("Shutdown signal received")
appCancel()
}()
}
func registerExistingDevices(fms *FMS) {
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()
// fleet.NewSprinkler(ctx, fms.broker, "a")
// fms.manager.RegisterDevice(sprinklerZoneA)
}
func mapRoutes(app *fiber.App) {
app.Use(logger.New())
app.Use(recover.New())
app.Get("/health", func(c fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": "ok",
})
})
api := app.Group("/api")
fleetHandler := fleet.NewFleetHandler(nil)
fleet.RegisterFleetRoutes(api, fleetHandler)
}