Skip to content

Commit

Permalink
chore: 更新 mux 至 v5
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Nov 25, 2021
1 parent 69d4ab1 commit 2eacf40
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
go: ['1.17.x']
go: ['1.17.x', '1.16.x']

steps:

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/issue9/errwrap v0.2.1
github.com/issue9/jsonrpc v0.12.1
github.com/issue9/localeutil v0.8.1
github.com/issue9/mux/v3 v3.1.1
github.com/issue9/mux/v5 v5.1.2
github.com/issue9/qheader v0.5.5
github.com/issue9/rands v1.2.0
github.com/issue9/sliceutil v0.7.1
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/issue9/assert v1.4.0/go.mod h1:Yktk83hAVl1SPSYtd9kjhBizuiBIqUQyj+D5SE2yjVY=
github.com/issue9/assert v1.4.1 h1:gUtOpMTeaE4JTe9kACma5foOHBvVt1p5XTFrULDwdXI=
github.com/issue9/assert v1.4.1/go.mod h1:Yktk83hAVl1SPSYtd9kjhBizuiBIqUQyj+D5SE2yjVY=
github.com/issue9/assert/v2 v2.0.0 h1:vN7fr70g5ND6zM39tPZk/E4WCyjGMqApmFbujSTmEo0=
github.com/issue9/assert/v2 v2.0.0/go.mod h1:rKr1eVGzXUhAo2af1thiKAhIA8uiSK9Wyn7mcZ4BzAg=
github.com/issue9/autoinc v1.0.8 h1:Vpi4ElzJOxyNKqw9wIsvNOSoh1xL9CNOp9vVQzouztI=
github.com/issue9/autoinc v1.0.8/go.mod h1:5LsFXszqd49znHXyPng+wFt6TYom5jCZoYNinFoKGgc=
github.com/issue9/cmdopt v0.7.1 h1:Jle2Be9ykHPU/1p3uTGfK/QQVb2zoE+8bG5mCHpH4As=
github.com/issue9/cmdopt v0.7.1/go.mod h1:2s73seejsiImh4JGBSJrYoWjK5rCHT+eYntYIUjlzro=
github.com/issue9/errwrap v0.2.0/go.mod h1:yudSKP/VtwvbW8hD9riNxJd8sAAzgJUgjllibgHqdJg=
github.com/issue9/errwrap v0.2.1 h1:hrzBGbOCRs8acitpWSg54xNl2oaLGA/WZEf71bKaiEU=
github.com/issue9/errwrap v0.2.1/go.mod h1:hjjkt9S/owgy48er68ACFfA6YPDO/xax18QE3lsguVQ=
github.com/issue9/jsonrpc v0.12.1 h1:HApT4tU8icb1iRStCidMvQfZvKI4MHZYipsAkPNsDQU=
github.com/issue9/jsonrpc v0.12.1/go.mod h1:uRoxqflqgG/+R9B2Axwsc9OG85TwmdSlJSZwjULwVBc=
github.com/issue9/localeutil v0.8.1 h1:8ngATedH5SEyZjS37KOjuxlNbPWnIpYhRGE35RDsT6k=
github.com/issue9/localeutil v0.8.1/go.mod h1:611rGRiQKJeEoUSsd5c7PvOuiGkR7a+4C7Ce0NURcu0=
github.com/issue9/mux/v3 v3.1.1 h1:itj2p66Cgn2PggnO804CB4cKRrk8XHSgIPUTXlDBSHA=
github.com/issue9/mux/v3 v3.1.1/go.mod h1:yN6Texx/I//wXVNsUWaSuqS+f3vRmf7lR3mysiuIn8Q=
github.com/issue9/mux/v5 v5.1.2 h1:QRE/xMmqHmbIgk1ZF+ZP9GuiiDjWvwVRGLYFC2xwfGE=
github.com/issue9/mux/v5 v5.1.2/go.mod h1:inBNr5pOw2UcCFONZFXffBmKdNF0i40283LzLnfQuyw=
github.com/issue9/qheader v0.5.5 h1:6LxVoqnxIthEuoZn2rFBGxOdoxN9gh+n1GI1TiGQe4Q=
github.com/issue9/qheader v0.5.5/go.mod h1:oZVXJYWaO2ha6yFlYPrdkTpEpg8InAIRo65fGytoWRo=
github.com/issue9/rands v1.2.0 h1:gXxfhibkFTIhg9M57jsH9BcuCe7rpNMEpuTAzSGr7bk=
github.com/issue9/rands v1.2.0/go.mod h1:Bayx3LQFKasAHNTGVN3APtfeoNATAdNwWUN3b4X8Blw=
github.com/issue9/sliceutil v0.6.0/go.mod h1:FUxZQC4oEw6V46afC+JOdCjOv84BA6WBNQRYGS9V4Dg=
github.com/issue9/sliceutil v0.7.1 h1:lnzYryF50+mxqvnsPZRZ1on+gxx0Khx5t5G7q0Cf7gU=
github.com/issue9/sliceutil v0.7.1/go.mod h1:8e+l2bD67pBUtV8cVnGChE/Ltlq8czcDLR1pOGCC2tE=
github.com/issue9/source v0.1.2 h1:Y70bsJOy9ks5MjY9O/3UnYsEu0a6lkzVm2AHQsqOl9c=
Expand Down
43 changes: 14 additions & 29 deletions internal/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
"strings"
"time"

"github.com/issue9/mux/v3"
"github.com/issue9/mux/v5"
"github.com/issue9/qheader"
"github.com/issue9/sliceutil"
"github.com/issue9/version"

"github.com/caixw/apidoc/v7/core"
Expand All @@ -27,8 +26,8 @@ import (
type mock struct {
msgHandler *core.MessageHandler
doc *ast.APIDoc
mux *mux.Mux
router http.Handler
router *mux.Router
h http.Handler
servers map[string]string
indent string
gen *GenOptions
Expand All @@ -50,7 +49,7 @@ func New(msg *core.MessageHandler, d *ast.APIDoc, indent, imageURL string, serve
return nil, locale.NewError(locale.VersionInCompatible)
}

mu := mux.New(false, false, true, nil, nil)
mu := mux.NewRouter("apidoc mock server")
router := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h.Set("Access-Control-Allow-Origin", "*")
Expand All @@ -62,8 +61,8 @@ func New(msg *core.MessageHandler, d *ast.APIDoc, indent, imageURL string, serve
m := &mock{
msgHandler: msg,
doc: d,
mux: mu,
router: router,
router: mu,
h: router,
indent: indent,
servers: servers,
gen: gen,
Expand All @@ -73,12 +72,10 @@ func New(msg *core.MessageHandler, d *ast.APIDoc, indent, imageURL string, serve
if imageURL[0] != '/' || imageURL[len(imageURL)-1] == '/' {
panic("参数 imageURL 必须以 / 开头且不能以 / 结尾")
}
m.mux.GetFunc(imageURL+"/{path}", m.getImage)
m.router.GetFunc(imageURL+"/{path}", m.getImage)
}

if err := m.parse(); err != nil {
return nil, err
}
m.parse()

return m, nil
}
Expand All @@ -103,17 +100,14 @@ func Load(h *core.MessageHandler, path core.URI, indent, imageURL string, server
return New(h, d, indent, imageURL, servers, gen)
}

func (m *mock) parse() error {
func (m *mock) parse() {
for _, api := range m.doc.APIs {
handler := m.buildAPI(api)
method := api.Method.V()
path := api.Path.Path.V()

if len(api.Servers) == 0 {
err := m.mux.Handle(path, handler, method)
if err != nil {
return err
}
m.router.Handle(path, handler, method)
continue
}

Expand All @@ -122,27 +116,18 @@ func (m *mock) parse() error {
if !found {
prefix = "/" + srv.V()
}
err := m.mux.Prefix(prefix).Handle(path, handler, method)
if err != nil {
return err
}
m.router.Prefix(prefix).Handle(path, handler, method)
}
}

routers := m.mux.All(true, true)
for path, methods := range routers[0].Routes {
routers := m.router.Routes()
for path, methods := range routers {
m.msgHandler.Locale(core.Info, locale.LoadAPI, "["+strings.Join(methods, ",")+"]", path)
}

return nil
}

func (m *mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.router.ServeHTTP(w, r)
}

func hasServer(srvs []*ast.ServerValue, key string) bool {
return sliceutil.Count(srvs, func(i int) bool { return srvs[i].V() == key }) > 0
m.h.ServeHTTP(w, r)
}

func (m *mock) getImage(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 2eacf40

Please sign in to comment.