Skip to content

Commit

Permalink
Add implementation to validates version specific handlers are registe…
Browse files Browse the repository at this point in the history
…red before starting server
  • Loading branch information
nagdahimanshu committed Feb 27, 2024
1 parent 81e3e31 commit 30b9e21
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/faultdetector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (app *App) Start() {
app.wg.Add(1)
go app.faultDetector.Start()

app.apiServer.AddHandler(app.faultDetector, app.config.Api.RegisterVersions, app.config.Api.BasePath)
app.apiServer.RegisterHandlersForVersion(app.faultDetector, app.config.Api.RegisterVersions, app.config.Api.BasePath)
app.wg.Add(1)
go app.apiServer.Start()

Expand Down
11 changes: 9 additions & 2 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/gin-gonic/gin"
)

var areVersionsHandlerRegistered bool = false

// HTTPServer embeds the http.Server along with the various other properties.
type HTTPServer struct {
server *http.Server
Expand All @@ -27,8 +29,8 @@ type HTTPServer struct {
errorChan chan error
}

// AddHandler is responsible to register route handlers.
func (w *HTTPServer) AddHandler(fd *faultdetector.FaultDetector, versions []string, basePath string) {
// RegisterHandlersForVersion is responsible to register API version specific route handlers.
func (w *HTTPServer) RegisterHandlersForVersion(fd *faultdetector.FaultDetector, versions []string, basePath string) {
baseGroup := w.router.Group(basePath)
for _, version := range versions {
group := baseGroup.Group(version)
Expand All @@ -42,12 +44,17 @@ func (w *HTTPServer) AddHandler(fd *faultdetector.FaultDetector, versions []stri
w.logger.Warningf("No routes and handlers defined for version %s. Please verify the API config.", version)
}
}
areVersionsHandlerRegistered = true
}

// Start starts the HTTP API server.
func (w *HTTPServer) Start() {
defer w.wg.Done()

if !areVersionsHandlerRegistered {
w.errorChan <- fmt.Errorf("API specific versions handler are not registered")
}

w.logger.Infof("Starting the HTTP server on %s.", w.server.Addr)
err := w.server.ListenAndServe()
if err != nil {
Expand Down

0 comments on commit 30b9e21

Please sign in to comment.