Skip to content

Commit

Permalink
Merge pull request #1 from pubgo/dev
Browse files Browse the repository at this point in the history
update and refactor
  • Loading branch information
kooksee authored Apr 27, 2024
2 parents 0886dc9 + 14f9c37 commit 47c1a02
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 135 deletions.
4 changes: 4 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ package templates
// https://github.com/long2ice/swagin
// https://github.com/long2ice/fibers
// https://github.com/getkin/kin-openapi
// https://github.com/danielgtaylor/huma
// https://github.com/swaggest/jsonschema-go
// https://github.com/swaggest/openapi-go
// https://github.com/santhosh-tekuri/jsonschema
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/getkin/kin-openapi v0.123.0
github.com/goccy/go-json v0.10.0
github.com/invopop/yaml v0.2.0
github.com/pubgo/funk v0.5.30
github.com/pubgo/funk v0.5.41
github.com/stretchr/testify v1.8.4
k8s.io/kube-openapi v0.0.0-20221123214604-86e75ddd809a
)
Expand All @@ -17,7 +17,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/k0kubun/pp/v3 v3.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -29,8 +29,10 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/zerolog v1.29.0 // indirect
golang.org/x/exp v0.0.0-20221114191408-850992195362 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 13 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
Expand Down Expand Up @@ -46,8 +44,8 @@ github.com/phuslu/goid v1.0.0/go.mod h1:txc2fUIdrdnn+v9Vq+QpiPQ3dnrXEchjoVDgic+r
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pubgo/funk v0.5.30 h1:uo5RblUBR9Opl9j0yzbQoACXNoegtrcvydfLQIdWgu4=
github.com/pubgo/funk v0.5.30/go.mod h1:Z5Wp7OoxjmSlWH+6waFSbao5MSD942LVhkBSlUu4s4s=
github.com/pubgo/funk v0.5.41 h1:vcDD6SWsz6g9CoD/whMIDgcfnGBfvCoZa6OTJ9iB9Tk=
github.com/pubgo/funk v0.5.41/go.mod h1:gKCw72+MK7xPiUGY1Z/bdGJMrSfVi87r0x/7d1GtKU4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand All @@ -58,21 +56,20 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
golang.org/x/exp v0.0.0-20221114191408-850992195362 h1:NoHlPRbyl1VFI6FjwHtPQCN7wAMXI6cKcqrmXhOOfBQ=
golang.org/x/exp v0.0.0-20221114191408-850992195362/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c h1:QgY/XxIAIeccR+Ca/rDdKubLIU9rcJ3xfy1DC/Wd2Oo=
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c/go.mod h1:CGI5F/G+E5bKwmfYo09AXuVN4dD894kIKUFmVbP2/Fo=
google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
17 changes: 10 additions & 7 deletions internal/examples/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"fmt"
"net/http"

"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/opendoc/opendoc"
"github.com/pubgo/opendoc/security"
"os"
)

type TestQueryReqAAA struct {
Expand All @@ -19,7 +21,7 @@ type TestQueryReqAAA struct {
func main() {
defer recovery.Exit()

var doc = opendoc.New(func(swag *opendoc.Swagger) {
doc := opendoc.New(func(swag *opendoc.Swagger) {
swag.Config.Title = "this service web title "
swag.Description = "this is description"
swag.License = &opendoc.License{
Expand Down Expand Up @@ -63,10 +65,11 @@ func main() {
})
})

data := assert.Must1(doc.MarshalYAML())
assert.Exit(os.WriteFile("openapi.yaml", data, 0644))
// data := assert.Must1(doc.MarshalYAML())
// assert.Exit(os.WriteFile("openapi.yaml", data, 0644))

//var app = fiber.New()
//doc.InitRouter(app)
//assert.Exit(app.Listen("localhost:8080"))
app := http.NewServeMux()
doc.InitRouter(app)
fmt.Println("http://localhost:8080/debug/apidocs")
assert.Exit(http.ListenAndServe("localhost:8080", app))
}
20 changes: 20 additions & 0 deletions internal/examples/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ paths:
- req
type: object
description: OK
Test:
content:
application/json:
schema:
properties:
name:
default: test
description: name of model validate:required
nullable: true
type: string
name1:
default: test
description: name1 of model validate:required
nullable: true
type: string
required:
- name
- name1
type: object
description: Test
default:
content:
application/json:
Expand Down
10 changes: 6 additions & 4 deletions opendoc/aaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

type License = openapi3.License
type Contact = openapi3.Contact
type Servers = openapi3.Servers
type Server = openapi3.Server
type (
License = openapi3.License
Contact = openapi3.Contact
Servers = openapi3.Servers
Server = openapi3.Server
)

// NamedEnum returns the enumerated acceptable values with according string names.
type NamedEnum interface {
Expand Down
25 changes: 15 additions & 10 deletions opendoc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package opendoc

import "github.com/pubgo/funk/version"

// https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
// https://rapidocweb.com/api.html#att-general

type Config struct {
Title string `yaml:"title"`
OpenapiRouter string `yaml:"path"`
OpenapiRedocRouter string `yaml:"redoc-path"`
OpenapiUrl string `yaml:"openapi-path"`
OpenapiOpt map[string]interface{} `yaml:"options"`
Title string `yaml:"title"`
OpenapiRouter string `yaml:"path"`
OpenapiRedocRouter string `yaml:"redoc-path"`
OpenapiRApiDocRouter string `yaml:"rapidoc-path"`
OpenapiUrl string `yaml:"openapi-path"`
OpenapiOpt map[string]interface{} `yaml:"options"`
}

func DefaultCfg() *Config {
return &Config{
Title: version.Project() + " openapi docs",
OpenapiRouter: "/debug/docs",
OpenapiRedocRouter: "/debug/redocs",
OpenapiUrl: "/debug/docs/openapi.yaml",
OpenapiOpt: make(map[string]interface{}),
Title: version.Project() + " openapi docs",
OpenapiRouter: "/debug/docs",
OpenapiRedocRouter: "/debug/redocs",
OpenapiRApiDocRouter: "/debug/apidocs",
OpenapiUrl: "/debug/docs/openapi.yaml",
OpenapiOpt: make(map[string]interface{}),
}
}
2 changes: 1 addition & 1 deletion opendoc/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (op *Operation) SetOperation(operationID string) *Operation {
return op
}

func (op *Operation) SetModel(req interface{}, rsp interface{}) *Operation {
func (op *Operation) SetModel(req, rsp interface{}) *Operation {
checkModelType(req)
op.request = req

Expand Down
2 changes: 1 addition & 1 deletion opendoc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *Service) PostOf(cb func(op *Operation)) *Service {
}

func (s *Service) Openapi() map[string]*openapi3.PathItem {
var routes = make(map[string]*openapi3.PathItem)
routes := make(map[string]*openapi3.PathItem)
for i := range s.operations {
op := s.operations[i]
if routes[op.path] == nil {
Expand Down
9 changes: 5 additions & 4 deletions opendoc/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func (s *Swagger) SetRootPath(path string) {
}

func (s *Swagger) ServiceOf(name string, cb func(srv *Service)) {
var srv = newService(name)
srv := newService(name)
srv.prefix = s.rootPath
s.Routers = append(s.Routers, srv)
cb(srv)
}

func (s *Swagger) WithService() *Service {
var srv = new(Service)
srv := new(Service)
srv.prefix = s.rootPath
s.Routers = append(s.Routers, srv)
return srv
Expand All @@ -50,7 +50,7 @@ func (s *Swagger) buildSwagger() *openapi3.T {
s.Config = DefaultCfg()
}

var t = &openapi3.T{
t := &openapi3.T{
OpenAPI: "3.0.0",
Servers: s.Servers,
Components: &components,
Expand Down Expand Up @@ -82,11 +82,12 @@ func (s *Swagger) buildSwagger() *openapi3.T {
func (s *Swagger) InitRouter(r *http.ServeMux) {
r.Handle(s.Config.OpenapiRouter, templates.SwaggerHandler(s.Config.Title, s.Config.OpenapiUrl))
r.Handle(s.Config.OpenapiRedocRouter, templates.ReDocHandler(s.Config.Title, s.Config.OpenapiUrl))
r.Handle(s.Config.OpenapiRApiDocRouter, templates.RApiDocHandler(s.Config.OpenapiUrl))
r.Handle(s.Config.OpenapiUrl, s.OpenapiDataHandler())
}

func (s *Swagger) OpenapiDataHandler() http.HandlerFunc {
var bytes = assert.Must1(s.MarshalYAML())
bytes := assert.Must1(s.MarshalYAML())
return func(writer http.ResponseWriter, request *http.Request) {
assert.Must1(writer.Write(bytes))
}
Expand Down
6 changes: 3 additions & 3 deletions opendoc/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ type testQueryReq struct {
}

func TestGenSchema(t *testing.T) {
var ref, s = genSchema(testQueryReq{})
ref, s := genSchema(testQueryReq{})
assert.NotNil(t, s)
assert.Equal(t, "#/components/schemas/com.github.pubgo.opendoc.testQueryReq", ref)

var data, err = json.Marshal(s)
data, err := json.Marshal(s)
assert.NoError(t, err)
assert.Equal(t,
`{"properties":{"name":{"default":"test","description":"name of model","nullable":true,"type":"string"},"rsp":{"$ref":"#/components/schemas/com.github.pubgo.opendoc.testQueryRsp"}},"required":["name","rsp"],"type":"object"}`,
string(data),
)

var p = genParameters(testQueryReq{})
p := genParameters(testQueryReq{})
data, err = json.Marshal(p)
assert.NoError(t, err)
assert.Equal(t,
Expand Down
Loading

0 comments on commit 47c1a02

Please sign in to comment.