we now got websockets baby

This commit is contained in:
2026-01-26 00:29:57 +11:00
parent 959ca96b68
commit eccab815ae
23 changed files with 621 additions and 312 deletions

View File

@@ -35,6 +35,12 @@ func setupHttpServer(
ignoredPaths := []string{
"/static/css/output.css",
"/static/favicon.ico",
"/static/js/popups.js",
"/static/js/theme.js",
"/static/vendored/htmx@2.0.8.min.js",
"/static/vendored/htmx-ext-ws.min.js",
"/static/vendored/alpinejs@3.15.4.min.js",
"/ws/notifications",
}
auth, err := setupAuth(cfg.HWSAuth, logger, bun, httpServer, ignoredPaths)
@@ -62,7 +68,7 @@ func setupHttpServer(
return nil, errors.Wrap(err, "addRoutes")
}
err = addMiddleware(httpServer, auth, cfg.Flags)
err = addMiddleware(httpServer, auth, cfg)
if err != nil {
return nil, errors.Wrap(err, "addMiddleware")
}

View File

@@ -3,6 +3,7 @@ package main
import (
"context"
"net/http"
"strconv"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
@@ -17,12 +18,12 @@ import (
func addMiddleware(
server *hws.Server,
auth *hwsauth.Authenticator[*db.User, bun.Tx],
flags *config.Flags,
cfg *config.Config,
) error {
err := server.AddMiddleware(
auth.Authenticate(),
htmxLog(flags.HTMXLog),
devMode(cfg),
)
if err != nil {
return errors.Wrap(err, "server.AddMiddleware")
@@ -30,12 +31,20 @@ func addMiddleware(
return nil
}
func htmxLog(htmxlog bool) hws.Middleware {
func devMode(cfg *config.Config) hws.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), contexts.HTMXLogKey, htmxlog)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
if cfg.Flags.DevMode {
devInfo := contexts.DevInfo{
WebsocketBase: "ws://" + cfg.HWS.Host + ":" + strconv.FormatUint(cfg.HWS.Port, 10),
HTMXLog: true,
}
ctx := context.WithValue(r.Context(), contexts.DevModeKey, devInfo)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
return
}
next.ServeHTTP(w, r)
},
)
}

View File

@@ -16,7 +16,7 @@ import (
)
func addRoutes(
server *hws.Server,
s *hws.Server,
staticFS *http.FileSystem,
cfg *config.Config,
conn *bun.DB,
@@ -25,41 +25,42 @@ func addRoutes(
discordAPI *discord.APIClient,
) error {
// Create the routes
routes := []hws.Route{
pageroutes := []hws.Route{
{
Path: "/static/",
Method: hws.MethodGET,
Handler: http.StripPrefix("/static/", handlers.StaticFS(staticFS, server)),
Handler: http.StripPrefix("/static/", handlers.StaticFS(staticFS, s)),
},
{
Path: "/",
Method: hws.MethodGET,
Handler: handlers.Index(server),
Handler: handlers.Index(s),
},
{
Path: "/login",
Method: hws.MethodGET,
Handler: auth.LogoutReq(handlers.Login(server, cfg, store, discordAPI)),
Handler: auth.LogoutReq(handlers.Login(s, cfg, store, discordAPI)),
},
{
Path: "/auth/callback",
Method: hws.MethodGET,
Handler: auth.LogoutReq(handlers.Callback(server, auth, conn, cfg, store, discordAPI)),
Handler: auth.LogoutReq(handlers.Callback(s, auth, conn, cfg, store, discordAPI)),
},
{
Path: "/register",
Methods: []hws.Method{hws.MethodGET, hws.MethodPOST},
Handler: auth.LogoutReq(handlers.Register(server, auth, conn, cfg, store)),
Handler: auth.LogoutReq(handlers.Register(s, auth, conn, cfg, store)),
},
{
Path: "/logout",
Methods: []hws.Method{hws.MethodGET, hws.MethodPOST},
Handler: auth.LoginReq(handlers.Logout(server, auth, conn, discordAPI)),
Handler: auth.LoginReq(handlers.Logout(s, auth, conn, discordAPI)),
},
{
Path: "/test",
Path: "/notification-tester",
Methods: []hws.Method{hws.MethodGET, hws.MethodPOST},
Handler: handlers.Test(server),
Handler: handlers.NotifyTester(s),
// TODO: add login protection
},
}
@@ -67,12 +68,23 @@ func addRoutes(
{
Path: "/htmx/isusernameunique",
Method: hws.MethodPOST,
Handler: handlers.IsUsernameUnique(server, conn, cfg, store),
Handler: handlers.IsUsernameUnique(s, conn, cfg, store),
},
}
wsRoutes := []hws.Route{
{
Path: "/ws/notifications",
Method: hws.MethodGET,
Handler: handlers.NotificationWS(s, cfg),
},
}
routes := append(pageroutes, htmxRoutes...)
routes = append(routes, wsRoutes...)
// Register the routes with the server
err := server.AddRoutes(append(routes, htmxRoutes...)...)
err := s.AddRoutes(routes...)
if err != nil {
return errors.Wrap(err, "server.AddRoutes")
}

View File

@@ -73,8 +73,9 @@ func run(ctx context.Context, w io.Writer, cfg *config.Config) error {
wg.Go(func() {
<-ctx.Done()
shutdownCtx := context.Background()
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 60*time.Second)
defer cancel()
logger.Info().Msg("Shut down requested, waiting 60 seconds...")
err := httpServer.Shutdown(shutdownCtx)
if err != nil {
logger.Error().Err(err).Msg("Graceful shutdown failed")