refactored view package

This commit is contained in:
2026-02-09 19:30:47 +11:00
parent fa3b8e3982
commit 0b3301f921
47 changed files with 653 additions and 490 deletions

View File

@@ -6,7 +6,7 @@ import (
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/view/page"
adminview "git.haelnorr.com/h/oslstats/internal/view/adminview"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
@@ -24,6 +24,6 @@ func AdminDashboard(s *hws.Server, conn *bun.DB) http.Handler {
}); !ok {
return
}
renderSafely(page.AdminDashboard(users), s, r, w)
renderSafely(adminview.DashboardPage(users), s, r, w)
})
}

View File

@@ -6,7 +6,7 @@ import (
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/view/component/admin"
adminview "git.haelnorr.com/h/oslstats/internal/view/adminview"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
@@ -29,6 +29,6 @@ func AdminUsersList(s *hws.Server, conn *bun.DB) http.Handler {
}); !ok {
return
}
renderSafely(admin.UserList(users), s, r, w)
renderSafely(adminview.UserList(users), s, r, w)
})
}

View File

@@ -15,6 +15,7 @@ import (
"git.haelnorr.com/h/oslstats/internal/discord"
"git.haelnorr.com/h/oslstats/internal/store"
"git.haelnorr.com/h/oslstats/internal/throw"
"git.haelnorr.com/h/oslstats/internal/validation"
"git.haelnorr.com/h/oslstats/pkg/oauth"
)
@@ -36,15 +37,23 @@ func Callback(
throw.BadRequest(s, w, r, "Too many redirects. Please try logging in again.", err)
return
}
state := r.URL.Query().Get("state")
code := r.URL.Query().Get("code")
if state == "" && code == "" {
http.Redirect(w, r, "/", http.StatusBadRequest)
getter := validation.NewQueryGetter(r)
state := getter.String("state").Required().Value
code := getter.String("code").Required().Value
if !getter.Validate() {
store.ClearRedirectTrack(r, "/callback")
apiErr := getter.String("error").Value
errDesc := getter.String("error_description").Value
if apiErr == "access_denied" {
throw.Unauthorized(s, w, r, "OAuth login failed or cancelled", errors.New(errDesc))
return
}
throw.BadRequest(s, w, r, "OAuth login failed", errors.New("state or code parameters missing"))
return
}
data, err := verifyState(cfg.OAuth, w, r, state)
if err != nil {
store.ClearRedirectTrack(r, "/callback")
if vsErr, ok := err.(*verifyStateError); ok {
if vsErr.IsCookieError() {
throw.Unauthorized(s, w, r, "OAuth session not found or expired", err)

View File

@@ -5,7 +5,7 @@ import (
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/notify"
"git.haelnorr.com/h/oslstats/internal/view/page"
baseview "git.haelnorr.com/h/oslstats/internal/view/baseview"
)
func ErrorPage(hwsError hws.HWSError) (hws.ErrorPage, error) {
@@ -27,7 +27,7 @@ func ErrorPage(hwsError hws.HWSError) (hws.ErrorPage, error) {
// Render appropriate template
if details != "" {
return page.ErrorWithDetails(
return baseview.ErrorPageWithDetails(
hwsError.StatusCode,
http.StatusText(hwsError.StatusCode),
message,
@@ -35,7 +35,7 @@ func ErrorPage(hwsError hws.HWSError) (hws.ErrorPage, error) {
), nil
}
return page.Error(
return baseview.ErrorPage(
hwsError.StatusCode,
http.StatusText(hwsError.StatusCode),
message,

View File

@@ -34,3 +34,12 @@ func renderSafely(page templ.Component, s *hws.Server, r *http.Request, w http.R
throw.InternalServiceError(s, w, r, "Failed to render page", errors.Wrap(err, "page."))
}
}
func logError(s *hws.Server, msg string, err error) {
s.LogError(hws.HWSError{
Message: msg,
Error: err,
Level: hws.ErrorERROR,
StatusCode: http.StatusInternalServerError,
})
}

View File

@@ -3,10 +3,9 @@ package handlers
import (
"net/http"
"git.haelnorr.com/h/oslstats/internal/throw"
"git.haelnorr.com/h/oslstats/internal/view/page"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/throw"
homeview "git.haelnorr.com/h/oslstats/internal/view/homeview"
)
// Index handles responses to the / path. Also serves a 404 Page for paths that
@@ -17,7 +16,7 @@ func Index(s *hws.Server) http.Handler {
if r.URL.Path != "/" {
throw.NotFound(s, w, r, r.URL.Path)
}
renderSafely(page.Index(), s, r, w)
renderSafely(homeview.IndexPage(), s, r, w)
},
)
}

View File

@@ -9,7 +9,7 @@ import (
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/notify"
"git.haelnorr.com/h/oslstats/internal/validation"
"git.haelnorr.com/h/oslstats/internal/view/page"
seasonsview "git.haelnorr.com/h/oslstats/internal/view/seasonsview"
"git.haelnorr.com/h/timefmt"
"github.com/pkg/errors"
"github.com/uptrace/bun"
@@ -21,7 +21,7 @@ func NewSeason(
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
renderSafely(page.NewSeason(), s, r, w)
renderSafely(seasonsview.NewPage(), s, r, w)
return
}
})

View File

@@ -11,7 +11,7 @@ import (
"git.haelnorr.com/h/oslstats/internal/config"
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/throw"
"git.haelnorr.com/h/oslstats/internal/view/component/popup"
"git.haelnorr.com/h/oslstats/internal/view/popup"
"github.com/coder/websocket"
"github.com/pkg/errors"
@@ -28,36 +28,24 @@ func NotificationWS(
}
nc, err := setupClient(s, w, r)
if err != nil {
s.LogError(hws.HWSError{
Message: "Failed to get notification client",
Error: err,
Level: hws.ErrorERROR,
StatusCode: http.StatusInternalServerError,
})
logError(s, "Failed to get notification client", errors.Wrap(err, "setupClient"))
return
}
ws, err := websocket.Accept(w, r, &websocket.AcceptOptions{
OriginPatterns: []string{cfg.HWSAuth.TrustedHost},
})
if err != nil {
s.LogError(hws.HWSError{
Message: "Failed to open websocket",
Error: err,
Level: hws.ErrorERROR,
StatusCode: http.StatusInternalServerError,
})
logError(s, "Failed to open websocket", errors.Wrap(err, "websocket.Accept"))
return
}
defer ws.CloseNow()
ctx := ws.CloseRead(r.Context())
err = notifyLoop(ctx, nc, ws)
if err != nil {
s.LogError(hws.HWSError{
Message: "Notification error",
Error: err,
Level: hws.ErrorERROR,
StatusCode: http.StatusInternalServerError,
})
logError(s, "Notification error", errors.Wrap(err, "notifyLoop"))
}
err = ws.CloseNow()
if err != nil {
logError(s, "Error closing websocket", errors.Wrap(err, "ws.CloseNow"))
}
},
)

View File

@@ -7,7 +7,7 @@ import (
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/notify"
"git.haelnorr.com/h/oslstats/internal/view/page"
testview "git.haelnorr.com/h/oslstats/internal/view/testview"
)
// NotifyTester handles responses to the / path. Also serves a 404 Page for paths that
@@ -17,7 +17,7 @@ func NotifyTester(s *hws.Server) http.Handler {
func(w http.ResponseWriter, r *http.Request) {
testErr := errors.New("This is a stack trace. No really i swear. Just pretend ok? Thanks")
if r.Method == "GET" {
renderSafely(page.Test(), s, r, w)
renderSafely(testview.NotificationTestPage(), s, r, w)
} else {
_ = r.ParseForm()
// target := r.Form.Get("target")

View File

@@ -14,7 +14,7 @@ import (
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/store"
"git.haelnorr.com/h/oslstats/internal/throw"
"git.haelnorr.com/h/oslstats/internal/view/page"
authview "git.haelnorr.com/h/oslstats/internal/view/authview"
)
func Register(
@@ -49,7 +49,7 @@ func Register(
store.ClearRedirectTrack(r, "/register")
if r.Method == "GET" {
renderSafely(page.Register(details.DiscordUser.Username), s, r, w)
renderSafely(authview.RegisterPage(details.DiscordUser.Username), s, r, w)
return
}
username := r.FormValue("username")

View File

@@ -7,7 +7,7 @@ import (
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/throw"
"git.haelnorr.com/h/oslstats/internal/view/page"
seasonsview "git.haelnorr.com/h/oslstats/internal/view/seasonsview"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
@@ -33,6 +33,6 @@ func SeasonPage(
throw.NotFound(s, w, r, r.URL.Path)
return
}
renderSafely(page.SeasonPage(season), s, r, w)
renderSafely(seasonsview.DetailPage(season), s, r, w)
})
}

View File

@@ -6,7 +6,7 @@ import (
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/oslstats/internal/db"
"git.haelnorr.com/h/oslstats/internal/view/page"
seasonsview "git.haelnorr.com/h/oslstats/internal/view/seasonsview"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
@@ -31,7 +31,7 @@ func SeasonsPage(
}); !ok {
return
}
renderSafely(page.SeasonsPage(seasons), s, r, w)
renderSafely(seasonsview.ListPage(seasons), s, r, w)
})
}
@@ -55,6 +55,6 @@ func SeasonsList(
}); !ok {
return
}
renderSafely(page.SeasonsList(seasons), s, r, w)
renderSafely(seasonsview.SeasonsList(seasons), s, r, w)
})
}