reveil/app.go
Pierre-Olivier Mercier 2c146f5615
All checks were successful
continuous-integration/drone/tag Build is passing
Use system or default TZ env var
2022-12-24 00:04:01 +01:00

115 lines
2.3 KiB
Go

package main
import (
"context"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/reveil/api"
"git.nemunai.re/nemunaire/reveil/config"
"git.nemunai.re/nemunaire/reveil/model"
"git.nemunai.re/nemunaire/reveil/player"
"git.nemunai.re/nemunaire/reveil/ui"
)
type App struct {
cfg *config.Config
db *reveil.LevelDBStorage
router *gin.Engine
srv *http.Server
nextAlarm *time.Timer
}
func NewApp(cfg *config.Config) *App {
if cfg.DevProxy == "" {
gin.SetMode(gin.ReleaseMode)
}
gin.ForceConsoleColor()
router := gin.Default()
router.Use(func(c *gin.Context) {
c.Next()
})
// Open Database
db, err := reveil.NewLevelDBStorage(cfg.LevelDBPath)
if err != nil {
log.Fatal("Unable to open the database:", err)
}
// Prepare struct
app := &App{
cfg: cfg,
db: db,
router: router,
}
// Register routes
ui.DeclareRoutes(router, cfg)
ui.DeclareNoJSRoutes(router, cfg, db, app.ResetTimer)
api.DeclareRoutes(router, cfg, db, app.ResetTimer)
router.GET("/api/version", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"version": Version})
})
return app
}
func (app *App) Start() {
app.srv = &http.Server{
Addr: app.cfg.Bind,
Handler: app.router,
}
app.ResetTimer()
log.Printf("Ready, listening on %s\n", app.cfg.Bind)
if err := app.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}
func (app *App) ResetTimer() {
if app.nextAlarm != nil {
app.nextAlarm.Stop()
app.nextAlarm = nil
}
if na, routines, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
app.nextAlarm = nil
reveil.RemoveOldAlarmsSingle(app.db)
// Rearm timer for the next time
app.ResetTimer()
err := player.WakeUp(app.cfg, routines)
if err != nil {
log.Println(err.Error())
return
}
})
log.Println("Next timer programmed for", *na)
}
}
func (app *App) Stop() {
if app.nextAlarm != nil {
app.nextAlarm.Stop()
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := app.srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
if err := app.db.Close(); err != nil {
log.Fatal("Database Close:", err)
}
}