From bbdf596e6e0b8d8df653bf98144d309646ff1751 Mon Sep 17 00:00:00 2001 From: Taras Kushnir Date: Wed, 17 Dec 2025 14:05:50 +0100 Subject: [PATCH] Remove refactoring leftover --- pkg/portal/utils.go | 76 --------------------------------------------- 1 file changed, 76 deletions(-) diff --git a/pkg/portal/utils.go b/pkg/portal/utils.go index d7f3cb1b..5df5f176 100644 --- a/pkg/portal/utils.go +++ b/pkg/portal/utils.go @@ -5,13 +5,11 @@ import ( "log/slog" randv2 "math/rand/v2" "net/http" - "strings" "github.com/PrivateCaptcha/PrivateCaptcha/pkg/common" "github.com/PrivateCaptcha/PrivateCaptcha/pkg/db" dbgen "github.com/PrivateCaptcha/PrivateCaptcha/pkg/db/generated" "github.com/PrivateCaptcha/PrivateCaptcha/pkg/session" - "github.com/justinas/alice" ) // NOTE: this will eventually be replaced by proper OTP @@ -19,80 +17,6 @@ func twoFactorCode() int { return randv2.IntN(900000) + 100000 } -type RouteAndHandler struct { - pattern string - chain alice.Chain - handler http.Handler -} - -// RouteGenerator's point is to passthrough the path correctly to the std.Handler() of slok/go-http-metrics -// the whole magic can break if for some reason Go will not evaluate result of Route() before calling Alice's Then() -// when calling router.Handle() in setupWithPrefix() -type RouteGenerator struct { - Prefix string - Path string - routes []*RouteAndHandler -} - -func (rg *RouteGenerator) Route(method string, parts ...string) string { - rg.Path = strings.Join(parts, "/") - result := method + " " + rg.Prefix + rg.Path - return result -} - -func (rg *RouteGenerator) Get(parts ...string) string { - return rg.Route(http.MethodGet, parts...) -} - -func (rg *RouteGenerator) Post(parts ...string) string { - return rg.Route(http.MethodPost, parts...) -} - -func (rg *RouteGenerator) Put(parts ...string) string { - return rg.Route(http.MethodPut, parts...) -} - -func (rg *RouteGenerator) Delete(parts ...string) string { - return rg.Route(http.MethodDelete, parts...) -} - -func (rg *RouteGenerator) LastPath() string { - result := rg.Path - // side-effect: this will cause go http metrics handler to use handlerID based on request Path - rg.Path = "" - return result -} - -func (rg *RouteGenerator) Handler(pattern string) (*RouteAndHandler, bool) { - for _, route := range rg.routes { - if route.pattern == pattern { - return route, true - } - } - - return nil, false -} - -func (rg *RouteGenerator) Handle(pattern string, chain alice.Chain, handler http.Handler) { - if route, ok := rg.Handler(pattern); ok { - route.chain = chain - route.handler = handler - return - } - - rg.routes = append(rg.routes, &RouteAndHandler{ - pattern: pattern, - chain: chain, - handler: handler, - }) -} - -func (rg *RouteGenerator) Register(router *http.ServeMux) { - for _, route := range rg.routes { - router.Handle(route.pattern, route.chain.Then(route.handler)) - } -} - func (s *Server) Org(user *dbgen.User, r *http.Request) (*dbgen.Organization, error) { ctx := r.Context()