Skip to content

Commit

Permalink
Support shutdown of bootstrap server
Browse files Browse the repository at this point in the history
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
welteki committed Dec 8, 2023
1 parent 88d664d commit 4bfbae6
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package bootstrap

import (
"context"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -31,7 +33,7 @@ func Router() *mux.Router {
}

// Serve load your handlers into the correct OpenFaaS route spec. This function is blocking.
func Serve(handlers *types.FaaSHandlers, config *types.FaaSConfig) {
func Serve(ctx context.Context, handlers *types.FaaSHandlers, config *types.FaaSConfig) {

if config.EnableBasicAuth {
reader := auth.ReadBasicAuthFromDisk{
Expand Down Expand Up @@ -118,5 +120,16 @@ func Serve(handlers *types.FaaSHandlers, config *types.FaaSConfig) {
Handler: r,
}

log.Fatal(s.ListenAndServe())
// Start server in a goroutine
go func() {
if err := s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()

// Shutdown server when context is done.
<-ctx.Done()
if err := s.Shutdown(context.Background()); err != nil {
log.Printf("Failed to shut down provider gracefully: %s", err)
}
}

0 comments on commit 4bfbae6

Please sign in to comment.