# HG changeset patch # User Florent Aide <florent.aide@gmail.com> # Date 1632415159 -7200 # Thu Sep 23 18:39:19 2021 +0200 # Node ID 5dc9b01039ffcda7996da415b7a6e3e5485d5b21 # Parent 9f520041a3cc14320375bbaad4aaaa9f9185ccdb fix ConfigureAPI signature according to cmd/serve.go template diff --git a/templates/server/cmdCustomInfo.gotmpl b/templates/server/cmdCustomInfo.gotmpl --- a/templates/server/cmdCustomInfo.gotmpl +++ b/templates/server/cmdCustomInfo.gotmpl @@ -4,5 +4,5 @@ type CustomInfo struct { // Custom global flags can be added here. For example: - // BaseURL string `long:"base-url" env:"BASE_URL" ini-name:"base-url" description:"The public facing base URL of the API. Used to forge URLs"` + BaseURL string `long:"base-url" env:"BASE_URL" ini-name:"base-url" description:"The public facing base URL of the API. Used to forge URLs"` } diff --git a/templates/server/cmdServeConfig.gotmpl b/templates/server/cmdServeConfig.gotmpl --- a/templates/server/cmdServeConfig.gotmpl +++ b/templates/server/cmdServeConfig.gotmpl @@ -7,6 +7,7 @@ } func setupServeConfig(config *restapi.Config) error { + config.BaseURL = InfoOptions.BaseURL // This is where the api config can be customized at will return nil } diff --git a/templates/server/configureapi.gotmpl b/templates/server/configureapi.gotmpl --- a/templates/server/configureapi.gotmpl +++ b/templates/server/configureapi.gotmpl @@ -16,6 +16,7 @@ "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/security" + "orus.io/orus-io/go-orusapi" {{ imports .DefaultImports }} {{ imports .Imports }} @@ -26,6 +27,7 @@ Log zerolog.Logger DB *sqlx.DB + BaseURL string // Here you can add anything the ConfigureAPI function will need to setup // the API } @@ -70,10 +72,20 @@ func (capi *ConfiguredAPI) ServerShutdown() { } -func ConfigureAPI(api *{{.Package}}.{{ pascalize .Name }}API, config Config) (*ConfiguredAPI, error) { +func ConfigureAPI(api *{{.Package}}.{{ pascalize .Name }}API, server *orusapi.Server, config Config) (*ConfiguredAPI, error) { // configure the api here api.ServeError = errors.ServeError + if config.BaseURL == "" { + scheme := "https" + if len(server.EnabledListeners) != 0 { + scheme = server.EnabledListeners[0] + } + config.BaseURL = fmt.Sprintf("%s://%s:%d", + scheme, server.Host, server.Port, + ) + } + // Set your custom logger if needed. Default one is log.Printf // Expected interface func(string, ...interface{}) // @@ -148,6 +160,10 @@ }) } {{- end }} + if server.Prometheus { + api.PrometheusInstrumentHandlers() + } + api.LoggingInstrumentHandlers() return &ConfiguredAPI{ api: api,