Skip to content
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
1 change: 1 addition & 0 deletions .version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.1.1
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
vet:
go vet ./...

run-example:
go run internal/examples/*.go

goimports:
goimports -w -local github.com/pubgo/opendoc .
5 changes: 4 additions & 1 deletion docs/docs.go → docs/_docs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package templates
package docs

// https://spec.openapis.org/oas/latest.html
// https://swagger.io/specification/
Expand All @@ -13,3 +13,6 @@ package templates
// https://github.com/swaggest/jsonschema-go
// https://github.com/swaggest/openapi-go
// https://github.com/santhosh-tekuri/jsonschema

// https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
// https://rapidocweb.com/api.html#att-general
27 changes: 8 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
module github.com/pubgo/opendoc

go 1.20
go 1.23

require (
github.com/fatih/structtag v1.2.0
github.com/getkin/kin-openapi v0.124.0
github.com/goccy/go-json v0.10.2
github.com/getkin/kin-openapi v0.131.0
github.com/invopop/yaml v0.3.1
github.com/pubgo/funk v0.5.41
github.com/samber/lo v1.52.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/alecthomas/repr v0.2.0 // indirect
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.4 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // 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
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/phuslu/goid v1.0.0 // indirect
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.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
golang.org/x/text v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
69 changes: 20 additions & 49 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,76 +1,47 @@
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/getkin/kin-openapi v0.124.0 h1:VSFNMB9C9rTKBnQ/fpyDU8ytMTr4dWI9QovSKj9kz/M=
github.com/getkin/kin-openapi v0.124.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
github.com/getkin/kin-openapi v0.131.0 h1:NO2UeHnFKRYhZ8wg6Nyh5Cq7dHk4suQQr72a4pMrDxE=
github.com/getkin/kin-openapi v0.131.0/go.mod h1:3OlG51PCYNsPByuiMB0t4fjnNlIDnaEDsjiKUV8nL58=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
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/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw=
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
github.com/phuslu/goid v1.0.0 h1:Cgcvd/R54UO1fCtyt+iKXAi+yZQ/KWlAm6MmZNizCLM=
github.com/phuslu/goid v1.0.0/go.mod h1:txc2fUIdrdnn+v9Vq+QpiPQ3dnrXEchjoVDgic+r+L0=
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.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=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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.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.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/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=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
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/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21 changes: 11 additions & 10 deletions internal/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package main
import (
"fmt"
"net/http"
"os"

"github.com/samber/lo"

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

type TestQueryReqAAA struct {
Expand All @@ -19,10 +21,8 @@ type TestQueryReqAAA struct {
}

func main() {
defer recovery.Exit()

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

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

app := http.NewServeMux()
doc.InitRouter(app)
fmt.Println("http://localhost:8080/debug/apidocs")
assert.Exit(http.ListenAndServe("localhost:8080", app))
templates.InitRouter(app, doc, templates.DefaultCfg())

fmt.Println("http://localhost:8082/debug/apidocs")
lo.Must0(http.ListenAndServe("localhost:8082", app))
}
4 changes: 2 additions & 2 deletions internal/examples/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ info:
name: Apache License 2.0
url: https://github.com/pubgo/opendoc/blob/master/LICENSE
termsOfService: https://github.com/pubgo
title: 'this service web title '
version: v0.0.1-dev-99
title: ""
version: ""
openapi: 3.0.0
paths:
/api/v1/articles:
Expand Down
19 changes: 15 additions & 4 deletions opendoc/operation.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package opendoc

import (
"log"
"net/http"
"path/filepath"
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/pubgo/funk/assert"
"github.com/samber/lo"

"github.com/pubgo/opendoc/security"
)
Expand Down Expand Up @@ -34,6 +35,11 @@ func (op *Operation) AddSecurity(security ...security.Security) *Operation {
return op
}

func (op *Operation) AddTags(tags ...string) *Operation {
op.tags = append(op.tags, tags...)
return op
}

func (op *Operation) SetExclude(exclude bool) *Operation {
op.exclude = exclude
return op
Expand Down Expand Up @@ -73,7 +79,9 @@ func (op *Operation) SetSummary(summary string) *Operation {
}

func (op *Operation) SetPath(path string) *Operation {
assert.If(path == "", "path should not be null")
if path == "" {
log.Panic("path should not be null")
}

path = strings.TrimSpace(path)
path = strings.Trim(path, "/")
Expand All @@ -82,7 +90,10 @@ func (op *Operation) SetPath(path string) *Operation {
}

func (op *Operation) SetOperation(operationID string) *Operation {
assert.If(operationID == "", "operationID should not be nil")
if operationID == "" {
log.Panic("operationID should not be nil")
}

op.operationID = operationID
return op
}
Expand Down Expand Up @@ -110,7 +121,7 @@ func (op *Operation) Openapi(item *openapi3.PathItem) {
}

operation := &openapi3.Operation{
Tags: op.tags,
Tags: lo.Uniq(op.tags),
OperationID: op.operationID,
Summary: op.summary,
Description: op.description,
Expand Down
5 changes: 5 additions & 0 deletions opendoc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (s *Service) SetName(name string) *Service {
return s
}

func (s *Service) AddTags(tags ...string) *Service {
s.tags = append(s.tags, tags...)
return s
}

func (s *Service) AddContentType(contentType ...string) *Service {
s.contentType = append(s.contentType, contentType...)
return s
Expand Down
44 changes: 8 additions & 36 deletions opendoc/swagger.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package opendoc

import (
"fmt"
"net/http"
"log"
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/invopop/yaml"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/version"

"github.com/pubgo/opendoc/templates"
)

type Swagger struct {
rootPath string

Config *Config
rootPath string
Title string
Description string
Version string
TermsOfService string
Expand All @@ -27,7 +21,10 @@ type Swagger struct {
}

func (s *Swagger) SetRootPath(path string) {
assert.If(path == "", "path should not be null")
if path == "" {
log.Panic("path should not be null")
}

s.rootPath = "/" + strings.Trim(strings.TrimSpace(path), "/")
}

Expand All @@ -46,16 +43,11 @@ func (s *Swagger) WithService() *Service {
}

func (s *Swagger) buildSwagger() *openapi3.T {
if s.Config == nil {
s.Config = DefaultCfg()
}

t := &openapi3.T{
OpenAPI: "3.0.0",
Servers: s.Servers,
Components: &components,
Info: &openapi3.Info{
Title: s.Config.Title,
Description: s.Description,
TermsOfService: s.TermsOfService,
Contact: s.Contact,
Expand All @@ -79,20 +71,6 @@ func (s *Swagger) buildSwagger() *openapi3.T {
return 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 {
bytes := assert.Must1(s.MarshalYAML())
return func(writer http.ResponseWriter, request *http.Request) {
assert.Must1(writer.Write(bytes))
}
}

func (s *Swagger) MarshalJSON() ([]byte, error) {
return s.buildSwagger().MarshalJSON()
}
Expand All @@ -107,15 +85,9 @@ func (s *Swagger) MarshalYAML() ([]byte, error) {
}

func New(handles ...func(swag *Swagger)) *Swagger {
swagger := &Swagger{
Config: DefaultCfg(),
Description: fmt.Sprintf("project:%s version:%s commit:%s", version.Project(), version.Version(), version.CommitID()),
Version: version.Version(),
}

swagger := &Swagger{}
for i := range handles {
handles[i](swagger)
}

return swagger
}
Loading