added new core.ServeEvent fields
This commit is contained in:
parent
9d8df8d05d
commit
d3711b0503
10
CHANGELOG.md
10
CHANGELOG.md
|
@ -8,6 +8,16 @@
|
||||||
|
|
||||||
- `Hook.Add()` and `Hook.PreAdd` now returns a unique string identifier that could be used to remove the registered hook handler via `Hook.Remove(handlerId)`.
|
- `Hook.Add()` and `Hook.PreAdd` now returns a unique string identifier that could be used to remove the registered hook handler via `Hook.Remove(handlerId)`.
|
||||||
|
|
||||||
|
- Added new fields to `core.ServeEvent`:
|
||||||
|
```
|
||||||
|
type ServeEvent struct {
|
||||||
|
App App
|
||||||
|
Router *echo.Echo
|
||||||
|
// new fields
|
||||||
|
Server *http.Server // allows adjusting the HTTP server config (global timeouts, TLS options, etc.)
|
||||||
|
CertManager *autocert.Manager // allows adjusting the autocert options (cache dir, host policy, etc.)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## v0.16.4-WIP
|
## v0.16.4-WIP
|
||||||
|
|
||||||
|
|
15
apis/base.go
15
apis/base.go
|
@ -47,6 +47,7 @@ func InitApi(app core.App) (*echo.Echo, error) {
|
||||||
e.Use(middleware.Recover())
|
e.Use(middleware.Recover())
|
||||||
e.Use(middleware.Secure())
|
e.Use(middleware.Secure())
|
||||||
e.Use(LoadAuthContext(app))
|
e.Use(LoadAuthContext(app))
|
||||||
|
e.Use(eagerRequestDataCache(app))
|
||||||
|
|
||||||
// custom error handler
|
// custom error handler
|
||||||
e.HTTPErrorHandler = func(c echo.Context, err error) {
|
e.HTTPErrorHandler = func(c echo.Context, err error) {
|
||||||
|
@ -116,20 +117,6 @@ func InitApi(app core.App) (*echo.Echo, error) {
|
||||||
bindHealthApi(app, api)
|
bindHealthApi(app, api)
|
||||||
bindBackupApi(app, api)
|
bindBackupApi(app, api)
|
||||||
|
|
||||||
// trigger the custom BeforeServe hook for the created api router
|
|
||||||
// allowing users to further adjust its options or register new routes
|
|
||||||
serveEvent := &core.ServeEvent{
|
|
||||||
App: app,
|
|
||||||
Router: e,
|
|
||||||
}
|
|
||||||
if err := app.OnBeforeServe().Trigger(serveEvent); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// note: it is after the OnBeforeServe hook to ensure that the implicit
|
|
||||||
// cache is after any user custom defined middlewares
|
|
||||||
e.Use(eagerRequestDataCache(app))
|
|
||||||
|
|
||||||
// catch all any route
|
// catch all any route
|
||||||
api.Any("/*", func(c echo.Context) error {
|
api.Any("/*", func(c echo.Context) error {
|
||||||
return echo.ErrNotFound
|
return echo.ErrNotFound
|
||||||
|
|
|
@ -27,7 +27,6 @@ type ServeOptions struct {
|
||||||
HttpAddr string
|
HttpAddr string
|
||||||
HttpsAddr string
|
HttpsAddr string
|
||||||
AllowedOrigins []string // optional list of CORS origins (default to "*")
|
AllowedOrigins []string // optional list of CORS origins (default to "*")
|
||||||
BeforeServeFunc func(server *http.Server) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve starts a new app web server.
|
// Serve starts a new app web server.
|
||||||
|
@ -75,7 +74,7 @@ func Serve(app core.App, options *ServeOptions) error {
|
||||||
|
|
||||||
mainHost, _, _ := net.SplitHostPort(mainAddr)
|
mainHost, _, _ := net.SplitHostPort(mainAddr)
|
||||||
|
|
||||||
certManager := autocert.Manager{
|
certManager := &autocert.Manager{
|
||||||
Prompt: autocert.AcceptTOS,
|
Prompt: autocert.AcceptTOS,
|
||||||
Cache: autocert.DirCache(filepath.Join(app.DataDir(), ".autocert_cache")),
|
Cache: autocert.DirCache(filepath.Join(app.DataDir(), ".autocert_cache")),
|
||||||
HostPolicy: autocert.HostWhitelist(mainHost, "www."+mainHost),
|
HostPolicy: autocert.HostWhitelist(mainHost, "www."+mainHost),
|
||||||
|
@ -93,10 +92,14 @@ func Serve(app core.App, options *ServeOptions) error {
|
||||||
Addr: mainAddr,
|
Addr: mainAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.BeforeServeFunc != nil {
|
serveEvent := &core.ServeEvent{
|
||||||
if err := options.BeforeServeFunc(serverConfig); err != nil {
|
App: app,
|
||||||
return err
|
Router: router,
|
||||||
}
|
Server: serverConfig,
|
||||||
|
CertManager: certManager,
|
||||||
|
}
|
||||||
|
if err := app.OnBeforeServe().Trigger(serveEvent); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.ShowStartBanner {
|
if options.ShowStartBanner {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/echo/v5"
|
"github.com/labstack/echo/v5"
|
||||||
"github.com/pocketbase/pocketbase/daos"
|
"github.com/pocketbase/pocketbase/daos"
|
||||||
"github.com/pocketbase/pocketbase/models"
|
"github.com/pocketbase/pocketbase/models"
|
||||||
|
@ -12,6 +14,7 @@ import (
|
||||||
"github.com/pocketbase/pocketbase/tools/mailer"
|
"github.com/pocketbase/pocketbase/tools/mailer"
|
||||||
"github.com/pocketbase/pocketbase/tools/search"
|
"github.com/pocketbase/pocketbase/tools/search"
|
||||||
"github.com/pocketbase/pocketbase/tools/subscriptions"
|
"github.com/pocketbase/pocketbase/tools/subscriptions"
|
||||||
|
"golang.org/x/crypto/acme/autocert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -70,8 +73,10 @@ type TerminateEvent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServeEvent struct {
|
type ServeEvent struct {
|
||||||
App App
|
App App
|
||||||
Router *echo.Echo
|
Router *echo.Echo
|
||||||
|
Server *http.Server
|
||||||
|
CertManager *autocert.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApiErrorEvent struct {
|
type ApiErrorEvent struct {
|
||||||
|
|
Loading…
Reference in New Issue