Skip to content

Commit

Permalink
Merge pull request #109 from lazybytez/feature/webapi-swagger
Browse files Browse the repository at this point in the history
Ensure basepath is properly used
  • Loading branch information
pascal-zarrad authored Oct 16, 2022
2 parents 0504104 + 9af81b3 commit cb39592
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions internal/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func enrichMiddlewares(e *gin.Engine) {
// This way it is easier to access the swagger.
func addSwaggerRedirect(originalHandler gin.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasSuffix(strings.TrimSuffix(c.Request.RequestURI, "/"), RouteSwagger) {
c.Redirect(http.StatusMovedPermanently, RouteSwaggerIndex)
if strings.HasSuffix(strings.TrimSuffix(c.Request.RequestURI, "/"), buildRoutePath(RouteSwagger)) {
c.Redirect(http.StatusMovedPermanently, buildRoutePath(RouteSwaggerIndex))

return
}
Expand All @@ -92,17 +92,10 @@ func configureGeneralSwaggerMeta(basePath string) {
// initSwagger cares about initializing the Swagger
// that can be used to find information about the web api.
func initSwagger() {
configuredBasePath := Config.webApiBasePath
if strings.HasSuffix(configuredBasePath, "/") && strings.HasPrefix(RouteApiV1, "/") {
configuredBasePath = strings.TrimSuffix(configuredBasePath, "/")
}
// base path for us is configured base path + current api version.
basePath := fmt.Sprintf("%s%s", configuredBasePath, RouteApiV1)

configureGeneralSwaggerMeta(basePath)
configureGeneralSwaggerMeta(buildRoutePath(RouteApiV1))
swaggerHandler := ginSwagger.WrapHandler(swaggerFiles.Handler)

routeGroup := engine.Group(RouteSwagger)
routeGroup := engine.Group(buildRoutePath(RouteSwagger))
routeGroup.GET("/*any", addSwaggerRedirect(swaggerHandler))
}

Expand All @@ -121,7 +114,7 @@ func initWebApi() {
engine = gin.New()
enrichMiddlewares(engine)

v1ApiRouter = engine.Group(RouteApiV1)
v1ApiRouter = engine.Group(buildRoutePath(RouteApiV1))

httpServer = &http.Server{
Addr: ":8080",
Expand All @@ -147,6 +140,13 @@ func initWebApi() {
}
}

// buildRoutePath prepares the path of a route to be registered
func buildRoutePath(route string) string {
return fmt.Sprintf("%s/%s",
strings.TrimSuffix(Config.webApiBasePath, "/"),
strings.TrimPrefix(route, "/"))
}

// shutdownApiWebserver tries to gracefully shut down
// the api webserver run by the bot.
func shutdownApiWebserver() {
Expand All @@ -162,7 +162,7 @@ func shutdownApiWebserver() {
webApiLogger.Info("Gracefully shutting down api webserver...")

if err := httpServer.Shutdown(ctx); err != nil {
webApiLogger.Err(err, "Failed to gracefully shutdown the api webserv!")
webApiLogger.Err(err, "Failed to gracefully shutdown the api webserver!")

return
}
Expand Down

0 comments on commit cb39592

Please sign in to comment.