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 {
|
2022-10-06 12:17:58 +00:00
|
|
|
cfg *config.Config
|
|
|
|
db *reveil.LevelDBStorage
|
|
|
|
router *gin.Engine
|
|
|
|
srv *http.Server
|
|
|
|
nextAlarm *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)
|
2022-12-08 15:44:17 +00:00
|
|
|
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()
|
|
|
|
app.nextAlarm = nil
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:49:42 +00:00
|
|
|
if na, routines, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
|
2022-10-06 12:17:58 +00:00
|
|
|
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
|
|
|
|
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()
|
|
|
|
|
2022-12-08 16:49:42 +00:00
|
|
|
err := player.WakeUp(app.cfg, routines)
|
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
|
|
|
}
|