added new core.ServeEvent fields

This commit is contained in:
Gani Georgiev
2023-05-29 16:57:50 +03:00
parent 9d8df8d05d
commit d3711b0503
4 changed files with 27 additions and 22 deletions
+1 -14
View File
@@ -47,6 +47,7 @@ func InitApi(app core.App) (*echo.Echo, error) {
e.Use(middleware.Recover())
e.Use(middleware.Secure())
e.Use(LoadAuthContext(app))
e.Use(eagerRequestDataCache(app))
// custom error handler
e.HTTPErrorHandler = func(c echo.Context, err error) {
@@ -116,20 +117,6 @@ func InitApi(app core.App) (*echo.Echo, error) {
bindHealthApi(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
api.Any("/*", func(c echo.Context) error {
return echo.ErrNotFound
+9 -6
View File
@@ -27,7 +27,6 @@ type ServeOptions struct {
HttpAddr string
HttpsAddr string
AllowedOrigins []string // optional list of CORS origins (default to "*")
BeforeServeFunc func(server *http.Server) error
}
// Serve starts a new app web server.
@@ -75,7 +74,7 @@ func Serve(app core.App, options *ServeOptions) error {
mainHost, _, _ := net.SplitHostPort(mainAddr)
certManager := autocert.Manager{
certManager := &autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(filepath.Join(app.DataDir(), ".autocert_cache")),
HostPolicy: autocert.HostWhitelist(mainHost, "www."+mainHost),
@@ -93,10 +92,14 @@ func Serve(app core.App, options *ServeOptions) error {
Addr: mainAddr,
}
if options.BeforeServeFunc != nil {
if err := options.BeforeServeFunc(serverConfig); err != nil {
return err
}
serveEvent := &core.ServeEvent{
App: app,
Router: router,
Server: serverConfig,
CertManager: certManager,
}
if err := app.OnBeforeServe().Trigger(serveEvent); err != nil {
return err
}
if options.ShowStartBanner {