2023-09-18 23:57:18 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-29 03:01:49 +00:00
|
|
|
"embed"
|
2023-09-18 23:57:18 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-11-03 23:38:31 +00:00
|
|
|
"sync"
|
2023-09-18 23:57:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"reichard.io/bbank/api"
|
|
|
|
"reichard.io/bbank/config"
|
|
|
|
"reichard.io/bbank/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
API *api.API
|
|
|
|
Config *config.Config
|
|
|
|
Database *database.DBManager
|
|
|
|
httpServer *http.Server
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
func NewServer(assets *embed.FS) *Server {
|
2023-09-18 23:57:18 +00:00
|
|
|
c := config.Load()
|
|
|
|
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
|
|
|
|
|
|
|
// Create Paths
|
|
|
|
docDir := filepath.Join(c.DataPath, "documents")
|
|
|
|
coversDir := filepath.Join(c.DataPath, "covers")
|
2023-11-03 23:38:31 +00:00
|
|
|
os.Mkdir(docDir, os.ModePerm)
|
|
|
|
os.Mkdir(coversDir, os.ModePerm)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
return &Server{
|
|
|
|
API: api,
|
|
|
|
Config: c,
|
|
|
|
Database: db,
|
2023-11-03 23:38:31 +00:00
|
|
|
httpServer: &http.Server{
|
|
|
|
Handler: api.Router,
|
|
|
|
Addr: (":" + c.ListenPort),
|
|
|
|
},
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
func (s *Server) StartServer(wg *sync.WaitGroup, done <-chan struct{}) {
|
|
|
|
ticker := time.NewTicker(15 * time.Minute)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
wg.Add(2)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
go func() {
|
2023-11-03 23:38:31 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
err := s.httpServer.ListenAndServe()
|
2023-11-03 23:38:31 +00:00
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Error("Error Starting Server:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
s.RunScheduledTasks()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
s.RunScheduledTasks()
|
|
|
|
case <-done:
|
|
|
|
log.Info("Stopping Task Runner...")
|
|
|
|
return
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
func (s *Server) RunScheduledTasks() {
|
|
|
|
log.Info("[RunScheduledTasks] Refreshing Temp Table Cache")
|
|
|
|
if err := s.API.DB.CacheTempTables(); err != nil {
|
|
|
|
log.Warn("[RunScheduledTasks] Refreshing Temp Table Cache Failure:", err)
|
|
|
|
}
|
|
|
|
log.Info("[RunScheduledTasks] Refreshing Temp Table Success")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) StopServer(wg *sync.WaitGroup, done chan<- struct{}) {
|
|
|
|
log.Info("Stopping HTTP Server...")
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
2023-09-18 23:57:18 +00:00
|
|
|
defer cancel()
|
2023-11-03 23:38:31 +00:00
|
|
|
if err := s.httpServer.Shutdown(ctx); err != nil {
|
|
|
|
log.Info("Shutting Error")
|
|
|
|
}
|
|
|
|
s.API.DB.Shutdown()
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
log.Info("Server Stopped")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|