From ac9785e4d2b520716c2afeb49c438006ad4d1c9e Mon Sep 17 00:00:00 2001 From: Keenan Nemetz Date: Tue, 23 Jan 2024 11:29:47 -0800 Subject: [PATCH] feat: Generate OpenAPI command (#2235) ## Relevant issue(s) Resolves #2234 ## Description This PR adds a command to output the current OpenAPI specification ## Tasks - [x] I made sure the code is well commented, particularly hard-to-understand areas. - [x] I made sure the repository-held documentation is changed accordingly. - [x] I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in [tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)). - [x] I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ... ## How has this been tested? `go run ./cmd/genopenapi` Specify the platform(s) on which this was tested: - MacOS --- cmd/genopenapi/main.go | 33 +++++++++++++++++++++++++++++++++ http/handler.go | 40 ++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 cmd/genopenapi/main.go diff --git a/cmd/genopenapi/main.go b/cmd/genopenapi/main.go new file mode 100644 index 0000000000..ed655eb932 --- /dev/null +++ b/cmd/genopenapi/main.go @@ -0,0 +1,33 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +/* +genopenapi is a tool to generate and print an OpenAPI specification. +*/ +package main + +import ( + "fmt" + "os" + + "github.com/sourcenetwork/defradb/http" +) + +func main() { + router, err := http.NewApiRouter() + if err != nil { + panic(err) + } + json, err := router.OpenAPI().MarshalJSON() + if err != nil { + panic(err) + } + fmt.Fprint(os.Stdout, string(json)) +} diff --git a/http/handler.go b/http/handler.go index 1df8987964..328ea8fab9 100644 --- a/http/handler.go +++ b/http/handler.go @@ -29,15 +29,7 @@ var Version string = "v0" // playgroundHandler is set when building with the playground build tag var playgroundHandler http.Handler = http.HandlerFunc(http.NotFound) -type Handler struct { - db client.DB - mux *chi.Mux - txs *sync.Map -} - -func NewHandler(db client.DB, opts ServerOptions) (*Handler, error) { - txs := &sync.Map{} - +func NewApiRouter() (*Router, error) { tx_handler := &txHandler{} store_handler := &storeHandler{} collection_handler := &collectionHandler{} @@ -50,12 +42,6 @@ func NewHandler(db client.DB, opts ServerOptions) (*Handler, error) { return nil, err } - router.AddMiddleware( - ApiMiddleware(db, txs, opts), - TransactionMiddleware, - StoreMiddleware, - ) - tx_handler.bindRoutes(router) store_handler.bindRoutes(router) p2p_handler.bindRoutes(router) @@ -74,6 +60,21 @@ func NewHandler(db client.DB, opts ServerOptions) (*Handler, error) { if err := router.Validate(context.Background()); err != nil { return nil, err } + return router, nil +} + +type Handler struct { + db client.DB + mux *chi.Mux + txs *sync.Map +} + +func NewHandler(db client.DB, opts ServerOptions) (*Handler, error) { + router, err := NewApiRouter() + if err != nil { + return nil, err + } + txs := &sync.Map{} mux := chi.NewMux() mux.Use( @@ -81,7 +82,14 @@ func NewHandler(db client.DB, opts ServerOptions) (*Handler, error) { middleware.Recoverer, CorsMiddleware(opts), ) - mux.Mount("/api/"+Version, router) + mux.Route("/api/"+Version, func(r chi.Router) { + r.Use( + ApiMiddleware(db, txs, opts), + TransactionMiddleware, + StoreMiddleware, + ) + r.Handle("/*", router) + }) mux.Get("/openapi.json", func(rw http.ResponseWriter, req *http.Request) { responseJSON(rw, http.StatusOK, router.OpenAPI()) })