brunel/server.go

107 lines
2.8 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"
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-08-02 22:07:39 +02:00
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
err = handlers_packages.UpdateFreezeCache()
if err != nil {
slog.Error("unable to update freeze cache: " + err.Error())
return err
}
2024-07-29 23:40:26 +02:00
buildqueue.StartPackageQueueWorker(ctx)
2024-08-02 22:55:17 +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)
server.Get("/api/package/:package", handlers_packages.Package)
2024-07-30 00:38:23 +02:00
server.Get("/api/queue", handlers_build.Queue)
server.Get("/api/errored", handlers_build.Errored)
2024-07-30 21:36:04 +02:00
server.Get("/api/isloggedin", handlers_auth.IsLoggedIn)
2024-08-02 22:07:39 +02:00
server.Get("/api/i386", handlers_packages.I386)
server.Get("/api/freezes", handlers_packages.Freezes)
2024-07-28 20:59:50 +02:00
server.Post("/api/login", handlers_auth.Login)
2024-07-30 21:36:04 +02:00
adminRoutes.Post("/triggerbuild", handlers_build.TriggerBuild)
2024-08-02 22:07:39 +02:00
adminRoutes.Post("/bulkRebuild", handlers_build.BulkRebuild)
2024-07-28 20:59:50 +02:00
adminRoutes.Post("/register", handlers_auth.Register)
adminRoutes.Post("/trigger-freeze", handlers_build.TriggerFreeze)
2024-07-28 20:59:50 +02:00
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
}