reveil/app.go

150 lines
3.3 KiB
Go
Raw Normal View History

2022-10-01 17:37:12 +00:00
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"
2022-10-05 18:26:37 +00:00
"git.nemunai.re/nemunaire/reveil/model"
2022-10-14 18:08:03 +00:00
"git.nemunai.re/nemunaire/reveil/player"
2022-10-01 17:37:12 +00:00
"git.nemunai.re/nemunaire/reveil/ui"
)
type App struct {
2024-10-11 15:07:42 +00:00
cfg *config.Config
db *reveil.LevelDBStorage
router *gin.Engine
srv *http.Server
nextAlarm *time.Timer
nextPreAlarm *time.Timer
2022-10-01 17:37:12 +00:00
}
2022-10-06 12:17:58 +00:00
func NewApp(cfg *config.Config) *App {
2022-10-01 17:37:12 +00:00
if cfg.DevProxy == "" {
gin.SetMode(gin.ReleaseMode)
}
gin.ForceConsoleColor()
router := gin.Default()
router.Use(func(c *gin.Context) {
c.Next()
})
2022-10-05 18:26:37 +00:00
// Open Database
db, err := reveil.NewLevelDBStorage(cfg.LevelDBPath)
if err != nil {
log.Fatal("Unable to open the database:", err)
}
2022-10-06 12:17:58 +00:00
// Prepare struct
app := &App{
cfg: cfg,
db: db,
router: router,
}
2022-10-01 17:37:12 +00:00
// Register routes
ui.DeclareRoutes(router, cfg)
ui.DeclareNoJSRoutes(router, cfg, db, app.ResetTimer)
2022-10-06 12:17:58 +00:00
api.DeclareRoutes(router, cfg, db, app.ResetTimer)
2022-10-01 17:37:12 +00:00
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,
}
2022-10-06 12:17:58 +00:00
app.ResetTimer()
2022-10-01 17:37:12 +00:00
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)
}
}
2022-10-06 12:17:58 +00:00
func (app *App) ResetTimer() {
if app.nextAlarm != nil {
app.nextAlarm.Stop()
2024-10-11 15:07:42 +00:00
app.nextPreAlarm = nil
2022-10-06 12:17:58 +00:00
app.nextAlarm = nil
}
2024-10-11 15:07:42 +00:00
settings, _ := reveil.ReadSettings(app.cfg.SettingsFile)
2024-07-25 17:38:02 +00:00
if na, routines, federated, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
2024-10-11 15:07:42 +00:00
if settings != nil && settings.PreAlarmAction != "" {
app.nextPreAlarm = time.AfterFunc(time.Until(*na)-settings.PreAlarmActionDelay*time.Minute, func() {
app.nextPreAlarm = nil
settings, err := reveil.ReadSettings(app.cfg.SettingsFile)
if err != nil {
log.Println("Unable to read settings:", err.Error())
return
}
action, err := reveil.LoadAction(app.cfg, settings.PreAlarmAction)
if err != nil {
log.Println("Unable to load pre-alarm action:", err.Error())
}
cmd, err := action.Launch(settings)
if err != nil {
log.Println(err.Error())
return
}
go func() {
err := cmd.Wait()
if err != nil {
log.Printf("%q: %s", action.Name, err.Error())
}
}()
})
log.Println("Next pre-alarm programmed for", time.Time(*na).Add(settings.PreAlarmActionDelay*-1*time.Minute))
}
2022-10-06 12:17:58 +00:00
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
2024-10-11 15:07:42 +00:00
app.nextPreAlarm = nil
2022-10-06 12:17:58 +00:00
app.nextAlarm = nil
2022-10-14 18:08:03 +00:00
reveil.RemoveOldAlarmsSingle(app.db)
2022-12-15 15:40:49 +00:00
// Rearm timer for the next time
app.ResetTimer()
2024-07-25 17:38:02 +00:00
err := player.WakeUp(app.cfg, routines, federated)
2022-10-14 18:08:03 +00:00
if err != nil {
log.Println(err.Error())
return
}
2022-10-06 12:17:58 +00:00
})
log.Println("Next timer programmed for", *na)
}
}
2022-10-01 17:37:12 +00:00
func (app *App) Stop() {
2022-10-06 12:17:58 +00:00
if app.nextAlarm != nil {
app.nextAlarm.Stop()
}
2022-10-01 17:37:12 +00:00
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := app.srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
2022-10-05 18:26:37 +00:00
if err := app.db.Close(); err != nil {
log.Fatal("Database Close:", err)
}
2022-10-01 17:37:12 +00:00
}