Skip to content

Commit

Permalink
return the http.Server instance to allow manual shutdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Jul 16, 2023
1 parent 64d7ab2 commit 6179864
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 6 additions & 8 deletions apis/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,15 @@ type ServeConfig struct {
AllowedOrigins []string
}

// @todo return the server instance to allow manual shutdowns
//
// Serve starts a new app web server.
func Serve(app core.App, config ServeConfig) error {
func Serve(app core.App, config ServeConfig) (*http.Server, error) {
if len(config.AllowedOrigins) == 0 {
config.AllowedOrigins = []string{"*"}
}

// ensure that the latest migrations are applied before starting the server
if err := runMigrations(app); err != nil {
return err
return nil, err
}

// reload app settings in case a new default value was set with a migration
Expand All @@ -60,7 +58,7 @@ func Serve(app core.App, config ServeConfig) error {

router, err := InitApi(app)
if err != nil {
return err
return nil, err
}

// configure cors
Expand Down Expand Up @@ -104,7 +102,7 @@ func Serve(app core.App, config ServeConfig) error {
CertManager: certManager,
}
if err := app.OnBeforeServe().Trigger(serveEvent); err != nil {
return err
return nil, err
}

if config.ShowStartBanner {
Expand Down Expand Up @@ -143,11 +141,11 @@ func Serve(app core.App, config ServeConfig) error {
go http.ListenAndServe(config.HttpAddr, certManager.HTTPHandler(nil))
}

return server.ListenAndServeTLS("", "")
return server, server.ListenAndServeTLS("", "")
}

// OR start HTTP server
return server.ListenAndServe()
return server, server.ListenAndServe()
}

type migrationsConnection struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewServeCommand(app core.App, showStartBanner bool) *cobra.Command {
Use: "serve",
Short: "Starts the web server (default to 127.0.0.1:8090)",
Run: func(command *cobra.Command, args []string) {
err := apis.Serve(app, apis.ServeConfig{
_, err := apis.Serve(app, apis.ServeConfig{
HttpAddr: httpAddr,
HttpsAddr: httpsAddr,
ShowStartBanner: showStartBanner,
Expand Down

0 comments on commit 6179864

Please sign in to comment.