2023-09-18 23:57:18 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-02-25 19:54:50 +00:00
|
|
|
"io/fs"
|
2023-09-18 23:57:18 +00:00
|
|
|
"net/http"
|
2023-11-03 23:38:31 +00:00
|
|
|
"sync"
|
2023-09-18 23:57:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2024-01-11 01:23:36 +00:00
|
|
|
"reichard.io/antholume/api"
|
|
|
|
"reichard.io/antholume/config"
|
|
|
|
"reichard.io/antholume/database"
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
type server struct {
|
|
|
|
db *database.DBManager
|
|
|
|
api *api.API
|
|
|
|
done chan int
|
|
|
|
wg sync.WaitGroup
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
// Create new server
|
2024-02-25 19:54:50 +00:00
|
|
|
func New(c *config.Config, assets fs.FS) *server {
|
2023-09-18 23:57:18 +00:00
|
|
|
db := database.NewMgr(c)
|
2023-11-29 03:01:49 +00:00
|
|
|
api := api.NewApi(db, c, assets)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
return &server{
|
|
|
|
db: db,
|
|
|
|
api: api,
|
|
|
|
done: make(chan int),
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
// Start server
|
|
|
|
func (s *server) Start() {
|
|
|
|
log.Info("Starting server...")
|
|
|
|
s.wg.Add(2)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
go func() {
|
2024-01-27 19:56:01 +00:00
|
|
|
defer s.wg.Done()
|
2023-11-03 23:38:31 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
err := s.api.Start()
|
2023-11-03 23:38:31 +00:00
|
|
|
if err != nil && err != http.ErrServerClosed {
|
2024-01-27 19:56:01 +00:00
|
|
|
log.Error("Starting server failed: ", err)
|
2023-11-03 23:38:31 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2024-01-27 19:56:01 +00:00
|
|
|
defer s.wg.Done()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(15 * time.Minute)
|
2023-11-03 23:38:31 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2024-01-27 19:56:01 +00:00
|
|
|
s.runScheduledTasks()
|
|
|
|
case <-s.done:
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Info("Stopping task runner...")
|
2023-11-03 23:38:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
log.Info("Server started")
|
2023-11-03 23:38:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
// Stop server
|
|
|
|
func (s *server) Stop() {
|
|
|
|
log.Info("Stopping server...")
|
2023-11-03 23:38:31 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
if err := s.api.Stop(); err != nil {
|
|
|
|
log.Error("HTTP server stop failed: ", err)
|
2023-11-03 23:38:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
close(s.done)
|
|
|
|
s.wg.Wait()
|
2023-11-03 23:38:31 +00:00
|
|
|
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Info("Server stopped")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2024-01-27 19:56:01 +00:00
|
|
|
|
|
|
|
// Run normal scheduled tasks
|
|
|
|
func (s *server) runScheduledTasks() {
|
|
|
|
start := time.Now()
|
|
|
|
if err := s.db.CacheTempTables(); err != nil {
|
|
|
|
log.Warn("Refreshing temp table cache failed: ", err)
|
|
|
|
}
|
|
|
|
log.Debug("Completed in: ", time.Since(start))
|
|
|
|
}
|