normalized builtin middlewares to return hook.Handler

This commit is contained in:
Gani Georgiev 2024-10-14 18:17:31 +03:00
parent 47d5ea3ce2
commit f9ee710cdd
3 changed files with 153 additions and 144 deletions

View File

@ -19,6 +19,7 @@ import (
"strings"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/hook"
)
const (
@ -124,7 +125,7 @@ var DefaultCORSConfig = CORSConfig{
}
// CORSWithConfig returns a CORS middleware with config.
func CORSWithConfig(config CORSConfig) func(e *core.RequestEvent) error {
func CORSWithConfig(config CORSConfig) *hook.Handler[*core.RequestEvent] {
// Defaults
if len(config.AllowOrigins) == 0 {
config.AllowOrigins = DefaultCORSConfig.AllowOrigins
@ -151,7 +152,10 @@ func CORSWithConfig(config CORSConfig) func(e *core.RequestEvent) error {
maxAge = strconv.Itoa(config.MaxAge)
}
return func(e *core.RequestEvent) error {
return &hook.Handler[*core.RequestEvent]{
Id: DefaultCorsMiddlewareId,
Priority: DefaultCorsMiddlewarePriority,
Func: func(e *core.RequestEvent) error {
req := e.Request
res := e.Response
origin := req.Header.Get("Origin")
@ -253,6 +257,7 @@ func CORSWithConfig(config CORSConfig) func(e *core.RequestEvent) error {
}
return e.NoContent(http.StatusNoContent)
},
}
}

View File

@ -18,6 +18,7 @@ import (
"sync"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/hook"
"github.com/pocketbase/pocketbase/tools/router"
)
@ -25,6 +26,10 @@ const (
gzipScheme = "gzip"
)
const (
DefaultGzipMiddlewareId = "pbGzip"
)
// GzipConfig defines the config for Gzip middleware.
type GzipConfig struct {
// Gzip compression level.
@ -46,12 +51,12 @@ type GzipConfig struct {
}
// Gzip returns a middleware which compresses HTTP response using Gzip compression scheme.
func Gzip() func(*core.RequestEvent) error {
func Gzip() *hook.Handler[*core.RequestEvent] {
return GzipWithConfig(GzipConfig{})
}
// GzipWithConfig returns a middleware which compresses HTTP response using gzip compression scheme.
func GzipWithConfig(config GzipConfig) func(*core.RequestEvent) error {
func GzipWithConfig(config GzipConfig) *hook.Handler[*core.RequestEvent] {
if config.Level < -2 || config.Level > 9 { // these are consts: gzip.HuffmanOnly and gzip.BestCompression
panic(errors.New("invalid gzip level"))
}
@ -79,7 +84,9 @@ func GzipWithConfig(config GzipConfig) func(*core.RequestEvent) error {
},
}
return func(e *core.RequestEvent) error {
return &hook.Handler[*core.RequestEvent]{
Id: DefaultGzipMiddlewareId,
Func: func(e *core.RequestEvent) error {
e.Response.Header().Add("Vary", "Accept-Encoding")
if strings.Contains(e.Request.Header.Get("Accept-Encoding"), gzipScheme) {
w, ok := pool.Get().(*gzip.Writer)
@ -127,6 +134,7 @@ func GzipWithConfig(config GzipConfig) func(*core.RequestEvent) error {
}
return e.Next()
},
}
}

View File

@ -83,21 +83,17 @@ func Serve(app core.App, config ServeConfig) error {
return err
}
pbRouter.Bind(&hook.Handler[*core.RequestEvent]{
Id: DefaultCorsMiddlewareId,
Func: CORSWithConfig(CORSConfig{
pbRouter.Bind(CORSWithConfig(CORSConfig{
AllowOrigins: config.AllowedOrigins,
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}),
Priority: DefaultCorsMiddlewarePriority,
})
}))
pbRouter.BindFunc(installerRedirect(app, config.DashboardPath))
pbRouter.GET(config.DashboardPath, Static(ui.DistDirFS, false)).
BindFunc(dashboardRemoveInstallerParam()).
BindFunc(dashboardCacheControl()).
BindFunc(Gzip())
Bind(Gzip())
// start http server
// ---