diff --git a/internal/handlers/login.go b/internal/handlers/login.go index 93bd6c8..d76fcb6 100644 --- a/internal/handlers/login.go +++ b/internal/handlers/login.go @@ -16,6 +16,13 @@ import ( func Login(server *hws.Server, cfg *config.Config, st *store.Store, discordAPI *discord.APIClient) http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { + // TODO: check DB is connected + // check discord API is working + if r.Method == "POST" { + // if either fail, notify the client that login is unavailable right now + // otherwise proceed redirect to GET method + } + // if either fail and method is GET, show service not available page cookies.SetPageFrom(w, r, cfg.HWSAuth.TrustedHost) attempts, exceeded, track := st.TrackRedirect(r, "/login", 5) diff --git a/internal/handlers/notifications.go b/internal/handlers/notifications.go new file mode 100644 index 0000000..047ff62 --- /dev/null +++ b/internal/handlers/notifications.go @@ -0,0 +1,50 @@ +package handlers + +import ( + "net/http" + + "git.haelnorr.com/h/golib/hws" + "git.haelnorr.com/h/golib/notify" + "github.com/pkg/errors" +) + +func notifyClient( + s *hws.Server, + r *http.Request, + level notify.Level, + title, message, details string, + action any, +) error { + subCookie, err := r.Cookie("ws_sub_id") + if err != nil { + return errors.Wrap(err, "r.Cookie") + } + subID := notify.Target(subCookie.Value) + nt := notify.Notification{ + Target: subID, + Title: title, + Message: message, + Details: details, + Action: action, + Level: level, + } + s.NotifySub(nt) + return nil +} + +func notifyInternalServiceError(s *hws.Server, r *http.Request, msg string, err error) error { + return notifyClient(s, r, notify.LevelError, "Internal Service Error", msg, + SerializeErrorDetails(http.StatusInternalServerError, err), nil) +} + +func notifyWarn(s *hws.Server, r *http.Request, title, msg string, action any) error { + return notifyClient(s, r, notify.LevelWarn, title, msg, "", action) +} + +func notifyInfo(s *hws.Server, r *http.Request, title, msg string, action any) error { + return notifyClient(s, r, notify.LevelInfo, title, msg, "", action) +} + +func notifySuccess(s *hws.Server, r *http.Request, title, msg string, action any) error { + return notifyClient(s, r, notify.LevelSuccess, title, msg, "", action) +} diff --git a/internal/handlers/register.go b/internal/handlers/register.go index 25ded6d..352d17f 100644 --- a/internal/handlers/register.go +++ b/internal/handlers/register.go @@ -18,7 +18,7 @@ import ( ) func Register( - server *hws.Server, + s *hws.Server, auth *hwsauth.Authenticator[*db.User, bun.Tx], conn *bun.DB, cfg *config.Config, @@ -42,7 +42,7 @@ func Register( store.ClearRedirectTrack(r, "/register") throwError( - server, + s, w, r, http.StatusBadRequest, @@ -69,7 +69,7 @@ func Register( defer cancel() tx, err := conn.BeginTx(ctx, nil) if err != nil { - throwInternalServiceError(server, w, r, "Database transaction failed", err) + throwInternalServiceError(s, w, r, "Database transaction failed", err) return } defer tx.Rollback() @@ -83,7 +83,10 @@ func Register( username := r.FormValue("username") user, err := registerUser(ctx, tx, username, details) if err != nil { - throwInternalServiceError(server, w, r, "Registration failed", err) + err = notifyInternalServiceError(s, r, "Registration failed", err) + if err != nil { + throwInternalServiceError(s, w, r, "Registration failed", err) + } return } tx.Commit() @@ -92,7 +95,7 @@ func Register( } else { err = auth.Login(w, r, user, true) if err != nil { - throwInternalServiceError(server, w, r, "Login failed", err) + throwInternalServiceError(s, w, r, "Login failed", err) return } pageFrom := cookies.CheckPageFrom(w, r) diff --git a/internal/handlers/test.go b/internal/handlers/test.go index d26c094..c05714e 100644 --- a/internal/handlers/test.go +++ b/internal/handlers/test.go @@ -6,13 +6,12 @@ import ( "github.com/pkg/errors" "git.haelnorr.com/h/golib/hws" - "git.haelnorr.com/h/golib/notify" "git.haelnorr.com/h/oslstats/internal/view/page" ) // Handles responses to the / path. Also serves a 404 Page for paths that // don't have explicit handlers -func NotifyTester(server *hws.Server) http.Handler { +func NotifyTester(s *hws.Server) http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { testErr := errors.New("This is a stack trace. No really i swear. Just pretend ok? Thanks") @@ -26,31 +25,24 @@ func NotifyTester(server *hws.Server) http.Handler { page.Test().Render(r.Context(), w) } else { r.ParseForm() - target := r.Form.Get("target") + // target := r.Form.Get("target") title := r.Form.Get("title") - level := map[string]notify.Level{ - "info": notify.LevelInfo, - "success": notify.LevelSuccess, - "warn": notify.LevelWarn, - "error": notify.LevelError, - }[r.Form.Get("type")] + level := r.Form.Get("type") message := r.Form.Get("message") - nt := notify.Notification{ - Target: notify.Target(target), - Title: title, - Message: message, - Level: level, - } - // For error level, serialize error details with code - if level == notify.LevelError { - nt.Details = SerializeErrorDetails(500, testErr) + var err error + switch level { + case "success": + err = notifySuccess(s, r, title, message, nil) + case "info": + err = notifyInfo(s, r, title, message, nil) + case "warn": + err = notifyWarn(s, r, title, message, nil) + case "error": + err = notifyInternalServiceError(s, r, message, testErr) } - - if target == "all" { - server.NotifyAll(nt) - } else { - server.NotifySub(nt) + if err != nil { + throwInternalServiceError(s, w, r, "Error notifying client", err) } } }, diff --git a/internal/view/component/popup/errorModalWS.templ b/internal/view/component/popup/errorModalWS.templ index ce24fc9..1bce01d 100644 --- a/internal/view/component/popup/errorModalWS.templ +++ b/internal/view/component/popup/errorModalWS.templ @@ -40,7 +40,7 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int) @@ -62,4 +62,3 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int) } - diff --git a/internal/view/page/test.templ b/internal/view/page/test.templ index c2a9739..972e671 100644 --- a/internal/view/page/test.templ +++ b/internal/view/page/test.templ @@ -36,7 +36,6 @@ templ Test() { Target