Skip to content

Commit

Permalink
feat: Generate OpenAPI command (sourcenetwork#2235)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves sourcenetwork#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
  • Loading branch information
nasdf committed Jan 23, 2024
1 parent d8ca1ce commit ac9785e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
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

0 comments on commit ac9785e

Please sign in to comment.