normalized builtin middlewares to return hook.Handler
This commit is contained in:
parent
47d5ea3ce2
commit
f9ee710cdd
|
@ -19,6 +19,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
|
"github.com/pocketbase/pocketbase/tools/hook"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -124,7 +125,7 @@ var DefaultCORSConfig = CORSConfig{
|
||||||
}
|
}
|
||||||
|
|
||||||
// CORSWithConfig returns a CORS middleware with config.
|
// 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
|
// Defaults
|
||||||
if len(config.AllowOrigins) == 0 {
|
if len(config.AllowOrigins) == 0 {
|
||||||
config.AllowOrigins = DefaultCORSConfig.AllowOrigins
|
config.AllowOrigins = DefaultCORSConfig.AllowOrigins
|
||||||
|
@ -151,7 +152,10 @@ func CORSWithConfig(config CORSConfig) func(e *core.RequestEvent) error {
|
||||||
maxAge = strconv.Itoa(config.MaxAge)
|
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
|
req := e.Request
|
||||||
res := e.Response
|
res := e.Response
|
||||||
origin := req.Header.Get("Origin")
|
origin := req.Header.Get("Origin")
|
||||||
|
@ -253,6 +257,7 @@ func CORSWithConfig(config CORSConfig) func(e *core.RequestEvent) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.NoContent(http.StatusNoContent)
|
return e.NoContent(http.StatusNoContent)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
|
"github.com/pocketbase/pocketbase/tools/hook"
|
||||||
"github.com/pocketbase/pocketbase/tools/router"
|
"github.com/pocketbase/pocketbase/tools/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,6 +26,10 @@ const (
|
||||||
gzipScheme = "gzip"
|
gzipScheme = "gzip"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultGzipMiddlewareId = "pbGzip"
|
||||||
|
)
|
||||||
|
|
||||||
// GzipConfig defines the config for Gzip middleware.
|
// GzipConfig defines the config for Gzip middleware.
|
||||||
type GzipConfig struct {
|
type GzipConfig struct {
|
||||||
// Gzip compression level.
|
// Gzip compression level.
|
||||||
|
@ -46,12 +51,12 @@ type GzipConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gzip returns a middleware which compresses HTTP response using Gzip compression scheme.
|
// 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{})
|
return GzipWithConfig(GzipConfig{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GzipWithConfig returns a middleware which compresses HTTP response using gzip compression scheme.
|
// 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
|
if config.Level < -2 || config.Level > 9 { // these are consts: gzip.HuffmanOnly and gzip.BestCompression
|
||||||
panic(errors.New("invalid gzip level"))
|
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")
|
e.Response.Header().Add("Vary", "Accept-Encoding")
|
||||||
if strings.Contains(e.Request.Header.Get("Accept-Encoding"), gzipScheme) {
|
if strings.Contains(e.Request.Header.Get("Accept-Encoding"), gzipScheme) {
|
||||||
w, ok := pool.Get().(*gzip.Writer)
|
w, ok := pool.Get().(*gzip.Writer)
|
||||||
|
@ -127,6 +134,7 @@ func GzipWithConfig(config GzipConfig) func(*core.RequestEvent) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.Next()
|
return e.Next()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,21 +83,17 @@ func Serve(app core.App, config ServeConfig) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
pbRouter.Bind(&hook.Handler[*core.RequestEvent]{
|
pbRouter.Bind(CORSWithConfig(CORSConfig{
|
||||||
Id: DefaultCorsMiddlewareId,
|
|
||||||
Func: CORSWithConfig(CORSConfig{
|
|
||||||
AllowOrigins: config.AllowedOrigins,
|
AllowOrigins: config.AllowedOrigins,
|
||||||
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
|
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
|
||||||
}),
|
}))
|
||||||
Priority: DefaultCorsMiddlewarePriority,
|
|
||||||
})
|
|
||||||
|
|
||||||
pbRouter.BindFunc(installerRedirect(app, config.DashboardPath))
|
pbRouter.BindFunc(installerRedirect(app, config.DashboardPath))
|
||||||
|
|
||||||
pbRouter.GET(config.DashboardPath, Static(ui.DistDirFS, false)).
|
pbRouter.GET(config.DashboardPath, Static(ui.DistDirFS, false)).
|
||||||
BindFunc(dashboardRemoveInstallerParam()).
|
BindFunc(dashboardRemoveInstallerParam()).
|
||||||
BindFunc(dashboardCacheControl()).
|
BindFunc(dashboardCacheControl()).
|
||||||
BindFunc(Gzip())
|
Bind(Gzip())
|
||||||
|
|
||||||
// start http server
|
// start http server
|
||||||
// ---
|
// ---
|
||||||
|
|
Loading…
Reference in New Issue