1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-25 07:34:10 +02:00

[#3233] added optional ServeEvent.Listener field

This commit is contained in:
Gani Georgiev
2025-06-29 15:41:55 +03:00
parent 0e12169546
commit 6443f2f159
3 changed files with 23 additions and 11 deletions

View File

@@ -5,6 +5,8 @@
- Added the triggered rate rimit rule in the error log `details`. - Added the triggered rate rimit rule in the error log `details`.
- Added optional `ServeEvent.Listener` field to initialize a custom network listener (e.g. `unix`) instead of the default `tcp` ([#3233](https://github.com/pocketbase/pocketbase/discussions/3233)).
- Other minor improvements (wrapped the backup restore in a transaction as an extra precaution, updated npm deps, regenerated JSVM docs with the recent tygoja changes, etc.). - Other minor improvements (wrapped the backup restore in a transaction as an extra precaution, updated npm deps, regenerated JSVM docs with the recent tygoja changes, etc.).

View File

@@ -153,13 +153,6 @@ func Serve(app core.App, config ServeConfig) error {
ErrorLog: log.New(&serverErrorLogWriter{app: app}, "", 0), ErrorLog: log.New(&serverErrorLogWriter{app: app}, "", 0),
} }
serveEvent := new(core.ServeEvent)
serveEvent.App = app
serveEvent.Router = pbRouter
serveEvent.Server = server
serveEvent.CertManager = certManager
serveEvent.InstallerFunc = DefaultInstallerFunc
var listener net.Listener var listener net.Listener
// graceful shutdown // graceful shutdown
@@ -207,6 +200,13 @@ func Serve(app core.App, config ServeConfig) error {
var baseURL string var baseURL string
serveEvent := new(core.ServeEvent)
serveEvent.App = app
serveEvent.Router = pbRouter
serveEvent.Server = server
serveEvent.CertManager = certManager
serveEvent.InstallerFunc = DefaultInstallerFunc
// trigger the OnServe hook and start the tcp listener // trigger the OnServe hook and start the tcp listener
serveHookErr := app.OnServe().Trigger(serveEvent, func(e *core.ServeEvent) error { serveHookErr := app.OnServe().Trigger(serveEvent, func(e *core.ServeEvent) error {
handler, err := e.Router.BuildMux() handler, err := e.Router.BuildMux()
@@ -237,10 +237,14 @@ func Serve(app core.App, config ServeConfig) error {
} }
} }
if e.Listener == nil {
listener, err = net.Listen("tcp", addr) listener, err = net.Listen("tcp", addr)
if err != nil { if err != nil {
return err return err
} }
} else {
listener = e.Listener
}
if e.InstallerFunc != nil { if e.InstallerFunc != nil {
app := e.App app := e.App
@@ -260,7 +264,7 @@ func Serve(app core.App, config ServeConfig) error {
if listener == nil { if listener == nil {
//nolint:staticcheck //nolint:staticcheck
return errors.New("The OnServe finalizer wasn't invoked. Did you forget to call the ServeEvent.Next() method?") return errors.New("The OnServe listener was not initialized. Did you forget to call the ServeEvent.Next() method?")
} }
if config.ShowStartBanner { if config.ShowStartBanner {

View File

@@ -2,6 +2,7 @@ package core
import ( import (
"context" "context"
"net"
"net/http" "net/http"
"time" "time"
@@ -104,6 +105,11 @@ type ServeEvent struct {
Server *http.Server Server *http.Server
CertManager *autocert.Manager CertManager *autocert.Manager
// Listener allow specifying a custom network listener.
//
// Leave it nil to use the default net.Listen("tcp", e.Server.Addr).
Listener net.Listener
// InstallerFunc is the "installer" function that is called after // InstallerFunc is the "installer" function that is called after
// successful server tcp bind but only if there is no explicit // successful server tcp bind but only if there is no explicit
// superuser record created yet. // superuser record created yet.