big ole refactor
This commit is contained in:
81
internal/server/setup.go
Normal file
81
internal/server/setup.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user