Skip to content

Commit b6fd1be

Browse files
committed
chore: quick update feat/v2 at 2025-10-24 11:31:23
1 parent 470f193 commit b6fd1be

File tree

20 files changed

+148
-165
lines changed

20 files changed

+148
-165
lines changed

.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.1.1

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vet:
2+
go vet ./...
3+
4+
run-example:
5+
go run internal/examples/*.go

docs/docs.go renamed to docs/_docs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package templates
1+
package docs
22

33
// https://spec.openapis.org/oas/latest.html
44
// https://swagger.io/specification/
@@ -13,3 +13,6 @@ package templates
1313
// https://github.com/swaggest/jsonschema-go
1414
// https://github.com/swaggest/openapi-go
1515
// https://github.com/santhosh-tekuri/jsonschema
16+
17+
// https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
18+
// https://rapidocweb.com/api.html#att-general

go.mod

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
11
module github.com/pubgo/opendoc
22

3-
go 1.20
3+
go 1.23
44

55
require (
66
github.com/fatih/structtag v1.2.0
7-
github.com/getkin/kin-openapi v0.124.0
8-
github.com/goccy/go-json v0.10.2
7+
github.com/getkin/kin-openapi v0.131.0
98
github.com/invopop/yaml v0.3.1
10-
github.com/pubgo/funk v0.5.41
9+
github.com/samber/lo v1.52.0
1110
github.com/stretchr/testify v1.9.0
1211
)
1312

1413
require (
15-
github.com/alecthomas/repr v0.2.0 // indirect
1614
github.com/davecgh/go-spew v1.1.1 // indirect
17-
github.com/go-openapi/jsonpointer v0.20.2 // indirect
18-
github.com/go-openapi/swag v0.22.8 // indirect
19-
github.com/golang/protobuf v1.5.4 // indirect
15+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
16+
github.com/go-openapi/swag v0.23.0 // indirect
2017
github.com/josharian/intern v1.0.0 // indirect
21-
github.com/k0kubun/pp/v3 v3.2.0 // indirect
2218
github.com/mailru/easyjson v0.7.7 // indirect
23-
github.com/mattn/go-colorable v0.1.13 // indirect
24-
github.com/mattn/go-isatty v0.0.17 // indirect
2519
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
20+
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
21+
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
2622
github.com/perimeterx/marshmallow v1.1.5 // indirect
27-
github.com/phuslu/goid v1.0.0 // indirect
2823
github.com/pmezard/go-difflib v1.0.0 // indirect
29-
github.com/rs/zerolog v1.29.0 // indirect
30-
golang.org/x/exp v0.0.0-20221114191408-850992195362 // indirect
31-
golang.org/x/sys v0.18.0 // indirect
32-
golang.org/x/text v0.14.0 // indirect
33-
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirect
34-
google.golang.org/grpc v1.51.0 // indirect
35-
google.golang.org/protobuf v1.33.0 // indirect
24+
golang.org/x/text v0.22.0 // indirect
3625
gopkg.in/yaml.v3 v3.0.1 // indirect
3726
)

go.sum

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,47 @@
1-
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
2-
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
3-
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
41
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
52
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
63
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
74
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
8-
github.com/getkin/kin-openapi v0.124.0 h1:VSFNMB9C9rTKBnQ/fpyDU8ytMTr4dWI9QovSKj9kz/M=
9-
github.com/getkin/kin-openapi v0.124.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
10-
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
11-
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
12-
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
13-
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
5+
github.com/getkin/kin-openapi v0.131.0 h1:NO2UeHnFKRYhZ8wg6Nyh5Cq7dHk4suQQr72a4pMrDxE=
6+
github.com/getkin/kin-openapi v0.131.0/go.mod h1:3OlG51PCYNsPByuiMB0t4fjnNlIDnaEDsjiKUV8nL58=
7+
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
8+
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
9+
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
10+
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
1411
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
15-
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
16-
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
17-
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
18-
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
19-
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
20-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
12+
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
2113
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
2214
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
2315
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
2416
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
25-
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
26-
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
2717
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
18+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2819
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
20+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2921
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
3022
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
31-
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
32-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
33-
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
34-
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
35-
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
36-
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
37-
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
3823
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
3924
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
25+
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=
26+
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw=
27+
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
28+
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
4029
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
4130
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
42-
github.com/phuslu/goid v1.0.0 h1:Cgcvd/R54UO1fCtyt+iKXAi+yZQ/KWlAm6MmZNizCLM=
43-
github.com/phuslu/goid v1.0.0/go.mod h1:txc2fUIdrdnn+v9Vq+QpiPQ3dnrXEchjoVDgic+r+L0=
44-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4531
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4632
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
47-
github.com/pubgo/funk v0.5.41 h1:vcDD6SWsz6g9CoD/whMIDgcfnGBfvCoZa6OTJ9iB9Tk=
48-
github.com/pubgo/funk v0.5.41/go.mod h1:gKCw72+MK7xPiUGY1Z/bdGJMrSfVi87r0x/7d1GtKU4=
4933
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
50-
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
51-
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
52-
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
53-
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
34+
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
35+
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
36+
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
5437
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
5538
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
5639
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
57-
golang.org/x/exp v0.0.0-20221114191408-850992195362 h1:NoHlPRbyl1VFI6FjwHtPQCN7wAMXI6cKcqrmXhOOfBQ=
58-
golang.org/x/exp v0.0.0-20221114191408-850992195362/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
59-
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
60-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
61-
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
62-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63-
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
64-
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
65-
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
66-
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
67-
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c h1:QgY/XxIAIeccR+Ca/rDdKubLIU9rcJ3xfy1DC/Wd2Oo=
68-
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c/go.mod h1:CGI5F/G+E5bKwmfYo09AXuVN4dD894kIKUFmVbP2/Fo=
69-
google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U=
70-
google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww=
71-
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
72-
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
40+
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
41+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
42+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
7343
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7444
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
45+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
7546
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7647
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/examples/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"os"
67

7-
"github.com/pubgo/funk/assert"
8-
"github.com/pubgo/funk/recovery"
98
"github.com/pubgo/opendoc/opendoc"
109
"github.com/pubgo/opendoc/security"
10+
"github.com/pubgo/opendoc/templates"
11+
"github.com/samber/lo"
1112
)
1213

1314
type TestQueryReqAAA struct {
@@ -19,10 +20,8 @@ type TestQueryReqAAA struct {
1920
}
2021

2122
func main() {
22-
defer recovery.Exit()
23-
2423
doc := opendoc.New(func(swag *opendoc.Swagger) {
25-
swag.Config.Title = "this service web title "
24+
swag.Title = "this service web title "
2625
swag.Description = "this is description"
2726
swag.License = &opendoc.License{
2827
Name: "Apache License 2.0",
@@ -65,11 +64,12 @@ func main() {
6564
})
6665
})
6766

68-
// data := assert.Must1(doc.MarshalYAML())
69-
// assert.Exit(os.WriteFile("openapi.yaml", data, 0644))
67+
data := lo.Must1(doc.MarshalYAML())
68+
lo.Must0(os.WriteFile("internal/examples/openapi.yaml", data, 0644))
7069

7170
app := http.NewServeMux()
72-
doc.InitRouter(app)
73-
fmt.Println("http://localhost:8080/debug/apidocs")
74-
assert.Exit(http.ListenAndServe("localhost:8080", app))
71+
templates.InitRouter(app, doc, templates.DefaultCfg())
72+
73+
fmt.Println("http://localhost:8082/debug/apidocs")
74+
lo.Must0(http.ListenAndServe("localhost:8082", app))
7575
}

internal/examples/openapi.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ info:
9898
name: Apache License 2.0
9999
url: https://github.com/pubgo/opendoc/blob/master/LICENSE
100100
termsOfService: https://github.com/pubgo
101-
title: 'this service web title '
102-
version: v0.0.1-dev-99
101+
title: ""
102+
version: ""
103103
openapi: 3.0.0
104104
paths:
105105
/api/v1/articles:

opendoc/operation.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package opendoc
22

33
import (
4+
"log"
45
"net/http"
56
"path/filepath"
67
"strings"
78

89
"github.com/getkin/kin-openapi/openapi3"
9-
"github.com/pubgo/funk/assert"
10-
1110
"github.com/pubgo/opendoc/security"
11+
"github.com/samber/lo"
1212
)
1313

1414
type Operation struct {
@@ -34,6 +34,11 @@ func (op *Operation) AddSecurity(security ...security.Security) *Operation {
3434
return op
3535
}
3636

37+
func (op *Operation) AddTags(tags ...string) *Operation {
38+
op.tags = append(op.tags, tags...)
39+
return op
40+
}
41+
3742
func (op *Operation) SetExclude(exclude bool) *Operation {
3843
op.exclude = exclude
3944
return op
@@ -73,7 +78,9 @@ func (op *Operation) SetSummary(summary string) *Operation {
7378
}
7479

7580
func (op *Operation) SetPath(path string) *Operation {
76-
assert.If(path == "", "path should not be null")
81+
if path == "" {
82+
log.Panic("path should not be null")
83+
}
7784

7885
path = strings.TrimSpace(path)
7986
path = strings.Trim(path, "/")
@@ -82,7 +89,10 @@ func (op *Operation) SetPath(path string) *Operation {
8289
}
8390

8491
func (op *Operation) SetOperation(operationID string) *Operation {
85-
assert.If(operationID == "", "operationID should not be nil")
92+
if operationID == "" {
93+
log.Panic("operationID should not be nil")
94+
}
95+
8696
op.operationID = operationID
8797
return op
8898
}
@@ -110,7 +120,7 @@ func (op *Operation) Openapi(item *openapi3.PathItem) {
110120
}
111121

112122
operation := &openapi3.Operation{
113-
Tags: op.tags,
123+
Tags: lo.Uniq(op.tags),
114124
OperationID: op.operationID,
115125
Summary: op.summary,
116126
Description: op.description,

opendoc/service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func (s *Service) SetName(name string) *Service {
2828
return s
2929
}
3030

31+
func (s *Service) AddTags(tags ...string) *Service {
32+
s.tags = append(s.tags, tags...)
33+
return s
34+
}
35+
3136
func (s *Service) AddContentType(contentType ...string) *Service {
3237
s.contentType = append(s.contentType, contentType...)
3338
return s

opendoc/swagger.go

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
package opendoc
22

33
import (
4-
"fmt"
5-
"net/http"
4+
"log"
65
"strings"
76

87
"github.com/getkin/kin-openapi/openapi3"
98
"github.com/invopop/yaml"
10-
"github.com/pubgo/funk/assert"
11-
"github.com/pubgo/funk/version"
12-
13-
"github.com/pubgo/opendoc/templates"
149
)
1510

1611
type Swagger struct {
17-
rootPath string
18-
19-
Config *Config
12+
rootPath string
13+
Title string
2014
Description string
2115
Version string
2216
TermsOfService string
@@ -27,7 +21,10 @@ type Swagger struct {
2721
}
2822

2923
func (s *Swagger) SetRootPath(path string) {
30-
assert.If(path == "", "path should not be null")
24+
if path == "" {
25+
log.Panic("path should not be null")
26+
}
27+
3128
s.rootPath = "/" + strings.Trim(strings.TrimSpace(path), "/")
3229
}
3330

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

4845
func (s *Swagger) buildSwagger() *openapi3.T {
49-
if s.Config == nil {
50-
s.Config = DefaultCfg()
51-
}
52-
5346
t := &openapi3.T{
5447
OpenAPI: "3.0.0",
5548
Servers: s.Servers,
5649
Components: &components,
5750
Info: &openapi3.Info{
58-
Title: s.Config.Title,
5951
Description: s.Description,
6052
TermsOfService: s.TermsOfService,
6153
Contact: s.Contact,
@@ -79,20 +71,6 @@ func (s *Swagger) buildSwagger() *openapi3.T {
7971
return t
8072
}
8173

82-
func (s *Swagger) InitRouter(r *http.ServeMux) {
83-
r.Handle(s.Config.OpenapiRouter, templates.SwaggerHandler(s.Config.Title, s.Config.OpenapiUrl))
84-
r.Handle(s.Config.OpenapiRedocRouter, templates.ReDocHandler(s.Config.Title, s.Config.OpenapiUrl))
85-
r.Handle(s.Config.OpenapiRApiDocRouter, templates.RApiDocHandler(s.Config.OpenapiUrl))
86-
r.Handle(s.Config.OpenapiUrl, s.OpenapiDataHandler())
87-
}
88-
89-
func (s *Swagger) OpenapiDataHandler() http.HandlerFunc {
90-
bytes := assert.Must1(s.MarshalYAML())
91-
return func(writer http.ResponseWriter, request *http.Request) {
92-
assert.Must1(writer.Write(bytes))
93-
}
94-
}
95-
9674
func (s *Swagger) MarshalJSON() ([]byte, error) {
9775
return s.buildSwagger().MarshalJSON()
9876
}
@@ -107,15 +85,9 @@ func (s *Swagger) MarshalYAML() ([]byte, error) {
10785
}
10886

10987
func New(handles ...func(swag *Swagger)) *Swagger {
110-
swagger := &Swagger{
111-
Config: DefaultCfg(),
112-
Description: fmt.Sprintf("project:%s version:%s commit:%s", version.Project(), version.Version(), version.CommitID()),
113-
Version: version.Version(),
114-
}
115-
88+
swagger := &Swagger{}
11689
for i := range handles {
11790
handles[i](swagger)
11891
}
119-
12092
return swagger
12193
}

0 commit comments

Comments
 (0)