# HG changeset patch # User Florent Aide <florent.aide@gmail.com> # Date 1633986354 -7200 # Mon Oct 11 23:05:54 2021 +0200 # Node ID 73aff05670f0014d5881b79fb11be9dadd253c39 # Parent db5348b25dc17b8ec84d20f9b793b293e3a3a6e7 tmpl: add shutdown callbacks in generated config diff --git a/templates/server/configureapi.gotmpl b/templates/server/configureapi.gotmpl --- a/templates/server/configureapi.gotmpl +++ b/templates/server/configureapi.gotmpl @@ -22,12 +22,16 @@ {{ imports .Imports }} ) +// Callback is a simple function +type Callback func() + // Config holds the things required to configure the API type Config struct { Log zerolog.Logger DB *sqlx.DB BaseURL string + Shutdown []Callback // Here you can add anything the ConfigureAPI function will need to setup // the API } @@ -56,6 +60,8 @@ type ConfiguredAPI struct { api *{{.Package}}.{{ pascalize .Name }}API handler http.Handler + + config Config } func (capi *ConfiguredAPI) Name() string { @@ -70,6 +76,10 @@ } func (capi *ConfiguredAPI) ServerShutdown() { + lsize := len(capi.config.Shutdown) + for i := range capi.config.Shutdown { + capi.config.Shutdown[lsize-i-1]() + } } func ConfigureAPI(api *{{.Package}}.{{ pascalize .Name }}API, server *orusapi.Server, config Config) (*ConfiguredAPI, error) { @@ -168,6 +178,7 @@ return &ConfiguredAPI{ api: api, handler: setupGlobalMiddleware(api.Serve(setupMiddlewares)), + config: config, }, nil }