Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Generate OpenAPI command #2235

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cmd/genopenapi/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
40 changes: 24 additions & 16 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand All @@ -74,14 +60,36 @@ 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(
middleware.RequestLogger(&logFormatter{}),
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())
})
Expand Down
Loading