Skip to content

Commit 829867e

Browse files
committed
Implement OpenAPI spec serving and embed JSON data [deploy-test]
1 parent 83e2383 commit 829867e

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

api/spec.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package apispec
2+
3+
import _ "embed"
4+
5+
//go:embed openapi.json
6+
var openAPIJSON []byte
7+
8+
func OpenAPIJSON() []byte {
9+
return append([]byte(nil), openAPIJSON...)
10+
}

pkg/api_client/routers.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package api_client
22

33
import (
4-
"path/filepath"
5-
"runtime"
4+
"net/http"
65

6+
apispec "github.com/developer-overheid-nl/don-api-register/api"
77
"github.com/developer-overheid-nl/don-api-register/pkg/api_client/handler"
88
"github.com/developer-overheid-nl/don-api-register/pkg/api_client/helpers/problem"
99
"github.com/gin-contrib/cors"
@@ -258,7 +258,8 @@ func NewRouter(apiVersion string, controller *handler.APIsAPIController) *fizz.F
258258
)
259259

260260
// 6) OpenAPI documentatie
261-
g.StaticFile("/v1/openapi.json", openAPISpecPath())
261+
g.GET("/v1/openapi.json", serveOpenAPISpec)
262+
g.HEAD("/v1/openapi.json", serveOpenAPISpec)
262263

263264
return f
264265
}
@@ -280,10 +281,7 @@ func APIVersionMiddleware(version string) gin.HandlerFunc {
280281
}
281282
}
282283

283-
func openAPISpecPath() string {
284-
_, file, _, ok := runtime.Caller(0)
285-
if !ok {
286-
return "./api/openapi.json"
287-
}
288-
return filepath.Clean(filepath.Join(filepath.Dir(file), "..", "..", "api", "openapi.json"))
284+
func serveOpenAPISpec(c *gin.Context) {
285+
data := apispec.OpenAPIJSON()
286+
c.Data(http.StatusOK, "application/json; charset=utf-8", data)
289287
}

0 commit comments

Comments
 (0)