league #1

Merged
h merged 41 commits from league into master 2026-02-15 19:59:31 +11:00
6 changed files with 83 additions and 33 deletions
Showing only changes of commit fb701bf205 - Show all commits

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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)
}
}
},

View File

@@ -40,7 +40,7 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int)
<button
onclick={ templ.JSFuncCall("copyToClipboard", fmt.Sprintf("error-modal-ws-details-%v", id), "copyButton") }
id="copyButton"
class="mt-2 bg-mauve text-crust px-3 py-1 rounded text-xs hover:bg-mauve/75 transition"
class="mt-2 bg-mauve text-crust px-3 py-1 rounded text-xs hover:bg-mauve/75 transition hover:cursor-pointer"
title="Copy to clipboard"
>
Copy
@@ -52,7 +52,7 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int)
<div class="mt-6">
<button
onclick="document.getElementById('error-modal-ws-container').innerHTML = ''"
class="inline-block rounded-lg bg-mauve px-5 py-3 text-sm text-crust transition hover:bg-mauve/75"
class="inline-block rounded-lg bg-mauve px-5 py-3 text-sm text-crust transition hover:bg-mauve/75 hover:cursor-pointer"
>
Close Modal
</button>
@@ -62,4 +62,3 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int)
<script src="/static/js/copytoclipboard.js"></script>
</div>
}

View File

@@ -36,7 +36,6 @@ templ Test() {
Target
</label>
<input
required
type="text"
name="target"
id="target"
@@ -73,7 +72,7 @@ templ Test() {
<!-- Submit Button -->
<button
type="submit"
class="mt-2 bg-mauve text-crust font-semibold px-6 py-3 rounded-lg hover:bg-mauve/75 transition"
class="mt-2 bg-mauve text-crust font-semibold px-6 py-3 rounded-lg hover:bg-mauve/75 transition hover:cursor-pointer"
>
Send Notification
</button>