Remove refactoring leftover

This commit is contained in:
Taras Kushnir
2025-12-17 14:05:50 +01:00
parent 653b91f86e
commit bbdf596e6e

View File

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