server: Switch to custom mux
This commit is contained in:
parent
a1069f115e
commit
ab9998881b
13 changed files with 313 additions and 62 deletions
|
|
@ -30,8 +30,8 @@ var (
|
|||
|
||||
func (auth *Auth) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/auth/"},
|
||||
CSRF: true,
|
||||
Prefix: "/auth/",
|
||||
CSRF: true,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ var (
|
|||
|
||||
func (next *Next) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/next/"},
|
||||
Prefix: "/next/",
|
||||
Decorator: next.Dependencies.Auth.Require(auth.User),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ var (
|
|||
|
||||
func (panel *UserPanel) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/user/"},
|
||||
Prefix: "/user/",
|
||||
CSRF: true,
|
||||
Decorator: panel.Dependencies.Auth.Require(nil),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ var (
|
|||
|
||||
func (admin *Admin) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/admin/"},
|
||||
Prefix: "/admin/",
|
||||
CSRF: true,
|
||||
Decorator: admin.Dependencies.Auth.Require(auth.Admin),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,8 +29,9 @@ var (
|
|||
|
||||
func (*Home) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/"},
|
||||
CSRF: false,
|
||||
Prefix: "/",
|
||||
MatchAllDomains: true,
|
||||
CSRF: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,8 +32,10 @@ var legalTemplate = static.AssetsLegal.MustParseShared("legal.html", legalTempla
|
|||
|
||||
func (legal *Legal) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/legal/"},
|
||||
CSRF: false,
|
||||
Prefix: "/legal/",
|
||||
Exact: true,
|
||||
|
||||
CSRF: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,8 +33,9 @@ var (
|
|||
|
||||
func (*News) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/news/"},
|
||||
CSRF: false,
|
||||
Prefix: "/news/",
|
||||
Exact: true,
|
||||
CSRF: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -126,10 +127,6 @@ func (news *News) HandleRoute(ctx context.Context, path string) (http.Handler, e
|
|||
|
||||
return httpx.HTMLHandler[newsContext]{
|
||||
Handler: func(r *http.Request) (nc newsContext, err error) {
|
||||
if strings.TrimSuffix(r.URL.Path, "/") != strings.TrimSuffix(path, "/") {
|
||||
return nc, httpx.ErrNotFound
|
||||
}
|
||||
|
||||
news.Dependencies.Custom.Update(&nc, r)
|
||||
nc.Items, err = items, itemsErr
|
||||
|
||||
|
|
|
|||
|
|
@ -2,10 +2,14 @@ package control
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/cancel"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/mux"
|
||||
"github.com/gorilla/csrf"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
|
@ -15,8 +19,25 @@ import (
|
|||
//
|
||||
// Logging messages are directed to progress
|
||||
func (control *Control) Server(ctx context.Context, progress io.Writer) (http.Handler, error) {
|
||||
// create a new mux
|
||||
mux := http.NewServeMux()
|
||||
logger := zerolog.Ctx(ctx)
|
||||
|
||||
var mux mux.Mux[component.RouteContext]
|
||||
mux.Context = func(r *http.Request) component.RouteContext {
|
||||
slug, ok := control.Still.Config.SlugFromHost(r.Host)
|
||||
return component.RouteContext{
|
||||
DefaultDomain: slug == "" && ok,
|
||||
}
|
||||
}
|
||||
mux.Panic = func(panic any, w http.ResponseWriter, r *http.Request) {
|
||||
// log the panic
|
||||
logger.Error().
|
||||
Str("panic", fmt.Sprint(panic)).
|
||||
Str("path", r.URL.Path).
|
||||
Msg("panic serving handler")
|
||||
|
||||
// and send an internal server error
|
||||
httpx.TextInterceptor.Fallback.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// create a csrf protector
|
||||
csrfProtector := control.CSRF()
|
||||
|
|
@ -24,14 +45,35 @@ func (control *Control) Server(ctx context.Context, progress io.Writer) (http.Ha
|
|||
// iterate over all the handler
|
||||
for _, s := range control.Dependencies.Routeables {
|
||||
routes := s.Routes()
|
||||
zerolog.Ctx(ctx).Info().Str("component", s.Name()).Strs("paths", routes.Paths).Bool("csrf", routes.CSRF).Bool("decorator", routes.Decorator != nil).Msg("mounting route")
|
||||
zerolog.Ctx(ctx).Info().
|
||||
Str("Name", s.Name()).
|
||||
Str("Prefix", routes.Prefix).
|
||||
Strs("Aliases", routes.Aliases).
|
||||
Bool("Exact", routes.Exact).
|
||||
Bool("CSRF", routes.CSRF).
|
||||
Bool("Decorator", routes.Decorator != nil).
|
||||
Bool("MatchAllDomains", routes.MatchAllDomains).
|
||||
Msg("mounting route")
|
||||
|
||||
for _, path := range routes.Paths {
|
||||
handler, err := s.HandleRoute(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mux.Handle(path, routes.Decorate(handler, csrfProtector))
|
||||
// call the handler for the route
|
||||
handler, err := s.HandleRoute(ctx, routes.Prefix)
|
||||
if err != nil {
|
||||
zerolog.Ctx(ctx).Err(err).
|
||||
Str("Component", s.Name()).
|
||||
Str("Prefix", routes.Prefix).
|
||||
Msg("error mounting route")
|
||||
continue
|
||||
}
|
||||
|
||||
// decorate the handler
|
||||
handler = routes.Decorate(handler, csrfProtector)
|
||||
|
||||
// determine the predicate
|
||||
predicate := routes.Predicate(mux.ContextOf)
|
||||
|
||||
// and add all the prefixes
|
||||
for _, prefix := range append([]string{routes.Prefix}, routes.Aliases...) {
|
||||
mux.Add(prefix, predicate, routes.Exact, handler)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,8 +20,9 @@ var (
|
|||
|
||||
func (*Static) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/static/"},
|
||||
CSRF: false,
|
||||
Prefix: "/static/",
|
||||
|
||||
CSRF: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,8 +29,6 @@ type Resolver struct {
|
|||
|
||||
prefixes lazy.Lazy[map[string]string] // cached prefixes (from the server)
|
||||
RefreshInterval time.Duration
|
||||
|
||||
handler lazy.Lazy[wdresolve.ResolveHandler] // handler
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -40,8 +38,9 @@ var (
|
|||
|
||||
func (resolver *Resolver) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Paths: []string{"/go/", "/wisski/get/"},
|
||||
CSRF: false,
|
||||
Prefix: "/wisski/get/",
|
||||
Aliases: []string{"/go/"},
|
||||
CSRF: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -59,42 +58,42 @@ func (resolver *Resolver) HandleRoute(ctx context.Context, route string) (http.H
|
|||
|
||||
logger := zerolog.Ctx(ctx)
|
||||
|
||||
var p wdresolve.ResolveHandler
|
||||
var err error
|
||||
return resolver.handler.Get(func() (p wdresolve.ResolveHandler) {
|
||||
p.HandleIndex = func(context wdresolve.IndexContext, w http.ResponseWriter, r *http.Request) {
|
||||
ctx := resolverContext{
|
||||
IndexContext: context,
|
||||
}
|
||||
resolver.Dependencies.Custom.Update(&ctx, r)
|
||||
|
||||
httpx.WriteHTML(ctx, nil, resolverTemplate, "", w, r)
|
||||
p.HandleIndex = func(context wdresolve.IndexContext, w http.ResponseWriter, r *http.Request) {
|
||||
ctx := resolverContext{
|
||||
IndexContext: context,
|
||||
}
|
||||
p.TrustXForwardedProto = true
|
||||
resolver.Dependencies.Custom.Update(&ctx, r)
|
||||
|
||||
fallback := &resolvers.Regexp{
|
||||
Data: map[string]string{},
|
||||
}
|
||||
httpx.WriteHTML(ctx, nil, resolverTemplate, "", w, r)
|
||||
}
|
||||
p.TrustXForwardedProto = true
|
||||
|
||||
// handle the default domain name!
|
||||
domainName := resolver.Config.DefaultDomain
|
||||
if domainName != "" {
|
||||
fallback.Data[fmt.Sprintf("^https?://(.*)\\.%s", regexp.QuoteMeta(domainName))] = fmt.Sprintf("https://$1.%s", domainName)
|
||||
logger.Info().Str("name", domainName).Msg("registering default domain")
|
||||
}
|
||||
fallback := &resolvers.Regexp{
|
||||
Data: map[string]string{},
|
||||
}
|
||||
|
||||
// handle the extra domains!
|
||||
for _, domain := range resolver.Config.SelfExtraDomains {
|
||||
fallback.Data[fmt.Sprintf("^https?://(.*)\\.%s", regexp.QuoteMeta(domain))] = fmt.Sprintf("https://$1.%s", domainName)
|
||||
logger.Info().Str("name", domainName).Msg("registering legacy domain")
|
||||
}
|
||||
// handle the default domain name!
|
||||
domainName := resolver.Config.DefaultDomain
|
||||
if domainName != "" {
|
||||
fallback.Data[fmt.Sprintf("^https?://(.*)\\.%s", regexp.QuoteMeta(domainName))] = fmt.Sprintf("https://$1.%s", domainName)
|
||||
logger.Info().Str("name", domainName).Msg("registering default domain")
|
||||
}
|
||||
|
||||
// resolve the prefixes
|
||||
p.Resolver = resolvers.InOrder{
|
||||
resolver,
|
||||
fallback,
|
||||
}
|
||||
return p
|
||||
}), err
|
||||
// handle the extra domains!
|
||||
for _, domain := range resolver.Config.SelfExtraDomains {
|
||||
fallback.Data[fmt.Sprintf("^https?://(.*)\\.%s", regexp.QuoteMeta(domain))] = fmt.Sprintf("https://$1.%s", domainName)
|
||||
logger.Info().Str("name", domainName).Msg("registering legacy domain")
|
||||
}
|
||||
|
||||
// resolve the prefixes
|
||||
p.Resolver = resolvers.InOrder{
|
||||
resolver,
|
||||
fallback,
|
||||
}
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (resolver *Resolver) Target(uri string) string {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package component
|
|||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/mux"
|
||||
)
|
||||
|
||||
// Routeable is a component that is servable
|
||||
|
|
@ -18,9 +20,18 @@ type Routeable interface {
|
|||
|
||||
// Routes represents information about a single Routeable
|
||||
type Routes struct {
|
||||
// Paths are the paths handled by this routeable.
|
||||
// Each path is passed to HandleRoute() individually.
|
||||
Paths []string
|
||||
// Prefix is the prefix this pattern handles
|
||||
Prefix string
|
||||
|
||||
// MatchAllDomains indicates that all domains, even the non-default domain, should be matched
|
||||
MatchAllDomains bool
|
||||
|
||||
// Exact indicates that only the exact prefix, as opposed to any sub-paths, are matched.
|
||||
// Trailing '/'s are automatically trimmed, even with an exact match.
|
||||
Exact bool
|
||||
|
||||
// Aliases are the additional prefixes this route handles.
|
||||
Aliases []string
|
||||
|
||||
// CSRF indicates if this route should be protected by CSRF.
|
||||
// CSRF protection is applied prior to any custom decorator being called.
|
||||
|
|
@ -31,6 +42,22 @@ type Routes struct {
|
|||
Decorator func(http.Handler) http.Handler
|
||||
}
|
||||
|
||||
type RouteContext struct {
|
||||
DefaultDomain bool
|
||||
}
|
||||
|
||||
// Predicate returns the predicate corresponding to the given route
|
||||
func (routes Routes) Predicate(context func(*http.Request) RouteContext) mux.Predicate {
|
||||
if routes.MatchAllDomains {
|
||||
return nil
|
||||
}
|
||||
|
||||
// match only the default domain
|
||||
return func(r *http.Request) bool {
|
||||
return context(r).DefaultDomain
|
||||
}
|
||||
}
|
||||
|
||||
// Decorate decorates the provided handler with the options specified in this handler.
|
||||
func (routes Routes) Decorate(handler http.Handler, csrf func(http.Handler) http.Handler) http.Handler {
|
||||
if routes.CSRF && csrf != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue