feat: add dependency shutdown and configurable service endpoints

This commit is contained in:
2026-09-24 18:33:24 +08:00
parent 8a348951eb
commit f0991d3ff2
51 changed files with 1433 additions and 73 deletions

View File

@@ -14,6 +14,7 @@ import (
"git.yuntuo.hk/k2safe/go-omni/config"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/lifecycle"
"git.yuntuo.hk/k2safe/go-omni/middleware"
"git.yuntuo.hk/k2safe/go-omni/response"
netrouter "git.yuntuo.hk/k2safe/go-omni/transport/nethttp"
@@ -183,7 +184,8 @@ func (a *App) NewServer(address string) *http.Server {
}
// RunContext serves until the context is canceled, then performs a graceful shutdown.
func (a *App) RunContext(ctx context.Context, address ...string) error {
func (a *App) RunContext(ctx context.Context, address ...string) (runErr error) {
defer func() { runErr = shutdownResult(ctx, runErr) }()
if ctx == nil {
ctx = context.Background()
}
@@ -205,7 +207,8 @@ func (a *App) RunContext(ctx context.Context, address ...string) error {
// RunWith starts a custom server runner while keeping App-owned cleanup in the
// same lifecycle. The runner receives the transport-neutral App handler and
// owns transport/TLS settings; built-in transport selection is bypassed.
func (a *App) RunWith(ctx context.Context, runner Runner, address ...string) error {
func (a *App) RunWith(ctx context.Context, runner Runner, address ...string) (runErr error) {
defer func() { runErr = shutdownResult(ctx, runErr) }()
if runner == nil {
return errors.Join(errors.New("go-omni: nil runner"), a.Close())
}
@@ -218,6 +221,13 @@ func (a *App) RunWith(ctx context.Context, runner Runner, address ...string) err
return errors.Join(runner.Run(ctx, a, a.listenAddress(address...)), a.Close())
}
func shutdownResult(ctx context.Context, err error) error {
if cause := lifecycle.Cause(ctx); cause != nil && !errors.Is(err, cause) {
return errors.Join(err, cause)
}
return err
}
func (a *App) listenAddress(address ...string) string {
listenOn := a.config.Address
if listenOn == "" {