brunel/server.go

103 lines
2.4 KiB
Go
Raw Normal View History

2024-07-28 20:59:50 +02:00
package main
import (
2024-07-29 23:40:26 +02:00
"brunel/buildqueue"
2024-07-28 20:59:50 +02:00
"brunel/config"
handlers_auth "brunel/handlers/auth"
2024-07-29 23:40:26 +02:00
handlers_build "brunel/handlers/build"
2024-07-28 20:59:50 +02:00
handlers_packages "brunel/handlers/packages"
"brunel/helpers"
"brunel/middleware"
"brunel/packages"
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
2024-07-29 17:39:11 +02:00
"github.com/gofiber/fiber/v2/middleware/compress"
2024-07-28 20:59:50 +02:00
"github.com/gofiber/fiber/v2/middleware/recover"
2024-07-29 17:39:11 +02:00
"github.com/maypok86/otter"
2024-07-28 20:59:50 +02:00
slogfiber "github.com/samber/slog-fiber"
)
// runServer runs a new HTTP server with the loaded environment variables.
func runServer(ctx context.Context) error {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
2024-07-29 17:39:11 +02:00
var err error
2024-07-28 20:59:50 +02:00
2024-07-29 17:39:11 +02:00
ott := otter.MustBuilder[string, any](10000)
ott.WithTTL(time.Hour * 24)
helpers.Cache, err = ott.Build()
if err != nil {
panic(err)
}
err = config.Init()
2024-07-28 20:59:50 +02:00
if err != nil {
slog.Error("unable to load configuration: " + err.Error())
return err
}
go func() {
for {
select {
case <-ctx.Done():
return
default:
err := helpers.DBInst.DeleteExpiredSessions()
if err != nil {
slog.Error("unable to delete expired sessions: " + err.Error())
}
time.Sleep(time.Minute)
}
}
}()
2024-07-30 00:38:23 +02:00
start := time.Now()
2024-07-28 20:59:50 +02:00
err = packages.LoadFromDb()
if err != nil {
slog.Error("unable to load packages from db: " + err.Error())
return err
}
2024-07-30 00:38:23 +02:00
slog.Info("packages loaded in " + time.Since(start).String())
2024-07-28 20:59:50 +02:00
2024-07-29 23:40:26 +02:00
buildqueue.StartPackageQueueWorker(ctx)
2024-07-30 02:30:26 +02:00
buildqueue.StartQueueAndStatusWorker(ctx)
2024-07-28 20:59:50 +02:00
cfg := fiber.Config{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
AppName: "brunel",
Prefork: false,
}
server := fiber.New(cfg)
server.Use(slogfiber.NewWithConfig(logger, slogfiber.Config{
WithSpanID: true,
WithTraceID: true,
}))
server.Use(recover.New())
2024-07-29 17:39:11 +02:00
server.Use(compress.New(compress.Config{
Level: compress.LevelBestSpeed,
}))
2024-07-28 20:59:50 +02:00
adminRoutes := server.Group("api/admin")
adminRoutes.Use(middleware.NewAuth())
server.Get("/api/counts", handlers_packages.Counts)
server.Get("/api/packages", handlers_packages.Packages)
2024-07-30 00:38:23 +02:00
server.Get("/api/queue", handlers_build.Queue)
2024-07-28 20:59:50 +02:00
server.Post("/api/login", handlers_auth.Login)
adminRoutes.Post("/register", handlers_auth.Register)
adminRoutes.Post("/updatePassword", handlers_auth.UpdatePassword)
2024-07-29 17:39:11 +02:00
return server.Listen(fmt.Sprintf("localhost:%d", config.Configs.Port))
2024-07-28 20:59:50 +02:00
}