Files
oslstats/internal/server/setup.go
2026-02-14 19:48:59 +11:00

82 lines
1.9 KiB
Go

// Package server provides setup utilities for the HTTP server
package server
import (
"io/fs"
"net/http"
"git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/hws"
"github.com/pkg/errors"
"git.haelnorr.com/h/oslstats/internal/config"
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/discord"
"git.haelnorr.com/h/oslstats/internal/handlers"
"git.haelnorr.com/h/oslstats/internal/rbac"
"git.haelnorr.com/h/oslstats/internal/store"
)
func Setup(
staticFS *fs.FS,
cfg *config.Config,
logger *hlog.Logger,
conn *db.DB,
store *store.Store,
discordAPI *discord.APIClient,
) (server *hws.Server, err error) {
if staticFS == nil {
return nil, errors.New("No filesystem provided")
}
fs := http.FS(*staticFS)
httpServer, err := hws.NewServer(cfg.HWS)
if err != nil {
return nil, errors.Wrap(err, "hws.NewServer")
}
ignoredPaths := []string{
"/static/*",
"/.well-known/*",
"/ws/notifications",
}
auth, err := setupAuth(
cfg.HWSAuth, logger, conn, httpServer, ignoredPaths)
if err != nil {
return nil, errors.Wrap(err, "setupAuth")
}
err = httpServer.AddErrorPage(handlers.ErrorPage)
if err != nil {
return nil, errors.Wrap(err, "httpServer.AddErrorPage")
}
err = httpServer.AddLogger(logger)
if err != nil {
return nil, errors.Wrap(err, "httpServer.AddLogger")
}
err = httpServer.LoggerIgnorePaths(ignoredPaths...)
if err != nil {
return nil, errors.Wrap(err, "httpServer.LoggerIgnorePaths")
}
// Initialize permissions checker
perms, err := rbac.NewChecker(conn, httpServer)
if err != nil {
return nil, errors.Wrap(err, "rbac.NewChecker")
}
err = addRoutes(httpServer, &fs, cfg, conn, auth, store, discordAPI, perms)
if err != nil {
return nil, errors.Wrap(err, "addRoutes")
}
err = addMiddleware(httpServer, auth, cfg, perms, discordAPI, store, conn)
if err != nil {
return nil, errors.Wrap(err, "addMiddleware")
}
return httpServer, nil
}