1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00
strimertul/app.go

109 lines
2.6 KiB
Go
Raw Normal View History

2022-11-16 11:23:54 +00:00
package main
import (
"context"
2022-11-18 19:28:13 +00:00
"strconv"
"github.com/strimertul/strimertul/modules"
"github.com/strimertul/strimertul/modules/database"
"github.com/strimertul/strimertul/modules/http"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
2022-11-16 11:23:54 +00:00
)
// App struct
type App struct {
ctx context.Context
cliParams *cli.Context
driver DatabaseDriver
2022-11-18 19:28:13 +00:00
manager *modules.Manager
2022-11-16 11:23:54 +00:00
}
// NewApp creates a new App application struct
func NewApp(cliParams *cli.Context) *App {
return &App{
cliParams: cliParams,
}
2022-11-16 11:23:54 +00:00
}
// startup is called when the app starts
2022-11-16 11:23:54 +00:00
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Create module manager
2022-11-18 19:28:13 +00:00
a.manager = modules.NewManager(logger)
// Make KV hub
var err error
a.driver, err = getDatabaseDriver(a.cliParams)
failOnError(err, "error opening database")
// Start database backup task
backupOpts := BackupOptions{
BackupDir: a.cliParams.String("backup-dir"),
BackupInterval: a.cliParams.Int("backup-interval"),
MaxBackups: a.cliParams.Int("max-backups"),
}
if backupOpts.BackupInterval > 0 {
go BackupTask(a.driver, backupOpts)
}
hub := a.driver.Hub()
go hub.Run()
2022-11-18 19:28:13 +00:00
db, err := database.NewDBModule(hub, a.manager)
failOnError(err, "failed to initialize database module")
// Set meta keys
_ = db.PutKey("stul-meta/version", appVersion)
for module, constructor := range moduleList {
2022-11-18 19:28:13 +00:00
err := constructor(a.manager)
if err != nil {
logger.Error("could not register module", zap.String("module", string(module)), zap.Error(err))
} else {
//goland:noinspection GoDeferInLoop
defer func() {
2022-11-18 19:28:13 +00:00
if err := a.manager.Modules[module].Close(); err != nil {
logger.Error("could not close module", zap.String("module", string(module)), zap.Error(err))
}
}()
}
}
// Create logger and endpoints
2022-11-18 19:28:13 +00:00
httpServer, err := http.NewServer(a.manager)
failOnError(err, "could not initialize http server")
defer func() {
if err := httpServer.Close(); err != nil {
logger.Error("could not close DB", zap.Error(err))
}
}()
// Run HTTP server
go failOnError(httpServer.Listen(), "HTTP server stopped")
2022-11-18 19:28:13 +00:00
// Wait until server is up
}
func (a *App) stop(context.Context) {
failOnError(a.driver.Close(), "could not close driver")
}
2022-11-18 19:28:13 +00:00
func (a *App) AuthenticateKVClient(id string) {
idInt, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return
}
a.driver.Hub().SetAuthenticated(idInt, true)
}
func (a *App) IsServerReady() bool {
return a.manager.Modules[modules.ModuleHTTP].Status().Working
}
func (a *App) GetKilovoltBind() string {
return a.manager.Modules[modules.ModuleHTTP].Status().Data.(http.StatusData).Bind
2022-11-16 11:23:54 +00:00
}