diff --git a/routes/openapi/openapi.go b/routes/openapi/openapi.go new file mode 100644 index 0000000..7d6a12b --- /dev/null +++ b/routes/openapi/openapi.go @@ -0,0 +1,50 @@ +/** Healthz package, endpoints. +* +* @author Sonja Happ +* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC +* @license GNU General Public License (version 3) +* +* VILLASweb-backend-go +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*********************************************************************************/ +package openapi + +import ( + _ "git.rwth-aachen.de/acs/public/villas/web-backend-go/doc/api" // doc/api folder is used by Swag CLI, you have to import it + "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" + "github.com/gin-gonic/gin" + "github.com/swaggo/swag" +) + +func RegisterOpenAPIEndpoint(r *gin.RouterGroup) { + r.GET("", getOpenAPI) +} + +// getOpenAPI godoc +// @Summary Get OpenAPI 2.0 spec of API +// @ID getOpenAPI +// @Produce json +// @Tags openapi +// @Success 200 string string "A OpenAPI 2.0 specification of the API" +// @Router /openapi [get] +func getOpenAPI(c *gin.Context) { + doc, err := swag.ReadDoc() + if err != nil { + helper.InternalServerError(c, err.Error()) + } + + c.Header("Content-Type", "application/json") + c.String(200, doc) +} diff --git a/routes/register.go b/routes/register.go index 8d89860..7884397 100644 --- a/routes/register.go +++ b/routes/register.go @@ -42,14 +42,13 @@ import ( "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/healthz" infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/metrics" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/openapi" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/result" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/signal" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/widget" "github.com/gin-gonic/gin" - ginSwagger "github.com/swaggo/gin-swagger" - "github.com/swaggo/gin-swagger/swaggerFiles" "github.com/zpatrick/go-config" ) @@ -80,6 +79,7 @@ func RegisterEndpoints(router *gin.Engine, api *gin.RouterGroup) { healthz.RegisterHealthzEndpoint(api.Group("/healthz")) metrics.RegisterMetricsEndpoint(api.Group("/metrics")) + openapi.RegisterOpenAPIEndpoint(api.Group("/openapi")) // All endpoints (except for /healthz and /metrics) require authentication except when someone wants to // login (POST /authenticate) user.RegisterAuthenticate(api.Group("/authenticate"))