Last active
October 10, 2020 11:15
-
-
Save podhmo/630a1047d11c0bf923d641d115b30475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module m | |
go 1.15 | |
require ( | |
github.com/go-chi/chi v4.1.2+incompatible // indirect | |
github.com/podhmo/reflect-openapi v0.0.3 // indirect | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | |
github.com/getkin/kin-openapi v0.22.1 h1:ODA1olTp175o//NfHko/uCAAhwUSfm5P4+K52XvTg4w= | |
github.com/getkin/kin-openapi v0.22.1/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw= | |
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= | |
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= | |
github.com/go-chi/chi v1.0.0 h1:s/kv1cTXfivYjdKJdyUzNGyAWZ/2t7duW1gKn5ivu+c= | |
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= | |
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= | |
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs= | |
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | |
github.com/podhmo/reflect-openapi v0.0.3 h1:s9vZGRivsHTO10L1J7GV/ezleLlSixVETJDl2vx9W34= | |
github.com/podhmo/reflect-openapi v0.0.3/go.mod h1:oTr6vMRd2eZVbdPxkr85wu90k6nBegq10WRy2IQzG+Y= | |
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | |
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | |
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | |
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | |
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= | |
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
"github.com/go-chi/chi" | |
"github.com/go-chi/chi/middleware" | |
reflectopenapi "github.com/podhmo/reflect-openapi" | |
) | |
type Setup struct { | |
Router chi.Router | |
Manager *reflectopenapi.Manager | |
} | |
func (s *Setup) AddEndpoint(method, path string, interactor interface{}, handle http.HandlerFunc) { | |
s.Router.Method(method, path, handle) | |
op := s.Manager.Visitor.VisitFunc(interactor) | |
s.Manager.Doc.AddOperation(path, method, op) | |
} | |
func (s *Setup) SetupRoutes() { | |
s.AddEndpoint( | |
"GET", "/", | |
func() string { return "" }, // この関数からpathを導出 | |
func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("welcome")) | |
}, | |
) | |
} | |
func main() { | |
r := chi.NewRouter() | |
r.Use(middleware.Logger) | |
c := reflectopenapi.Config{} | |
doc, err := c.BuildDoc(context.Background(), func(m *reflectopenapi.Manager) { | |
s := &Setup{Manager: m, Router: r} | |
s.SetupRoutes() | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if ok, _ := strconv.ParseBool(os.Getenv("DOCGEN")); ok { | |
enc := json.NewEncoder(os.Stdout) | |
enc.SetIndent("", " ") | |
enc.Encode(doc) | |
return | |
} | |
http.ListenAndServe(":3000", r) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"components": {}, | |
"info": { | |
"description": "-", | |
"title": "Sample API", | |
"version": "0.0.0" | |
}, | |
"openapi": "3.0.0", | |
"paths": { | |
"/": { | |
"get": { | |
"operationId": "main.(*Setup).SetupRoutes.func1", | |
"responses": { | |
"200": { | |
"content": { | |
"application/json": { | |
"schema": { | |
"type": "string" | |
} | |
} | |
}, | |
"description": "" | |
}, | |
"default": { | |
"description": "" | |
} | |
} | |
} | |
} | |
}, | |
"servers": [ | |
{ | |
"url": "http://localhost:8888", | |
"description": "local development server" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment