-
Notifications
You must be signed in to change notification settings - Fork 7
/
ctx.go
62 lines (47 loc) · 1.35 KB
/
ctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: MIT
// Package ctx 自定义路由
package ctx
import (
"net/http"
"github.com/issue9/mux/v7"
"github.com/issue9/mux/v7/group"
"github.com/issue9/mux/v7/types"
)
type (
CTX struct {
R *http.Request
W http.ResponseWriter
P types.Route
}
Router = mux.RouterOf[Handler]
Routers = group.GroupOf[Handler]
Handler interface {
Handle(*CTX)
}
HandlerFunc func(*CTX)
)
func (f HandlerFunc) Handle(c *CTX) { f(c) }
func call(w http.ResponseWriter, r *http.Request, ps types.Route, h Handler) {
h.Handle(&CTX{R: r, W: w, P: ps})
}
func optionsHandlerBuilder(p types.Node) Handler {
return HandlerFunc(func(ctx *CTX) {
ctx.W.Header().Set("allow", p.AllowHeader())
})
}
func methodNotAllowedBuilder(p types.Node) Handler {
return HandlerFunc(func(ctx *CTX) {
ctx.W.Header().Set("allow", p.AllowHeader())
ctx.W.WriteHeader(http.StatusMethodNotAllowed)
})
}
func notFound(ctx *CTX) {
ctx.W.WriteHeader(http.StatusNotFound)
}
func NewRouters(o ...mux.Option) *Routers {
return group.NewOf[Handler](call, HandlerFunc(notFound), methodNotAllowedBuilder, optionsHandlerBuilder, o...)
}
// NewRouter 声明适用于官方 http.Handler 接口的路由
func NewRouter(name string, o ...mux.Option) *Router {
return mux.NewRouterOf[Handler](name, call, HandlerFunc(notFound), methodNotAllowedBuilder, optionsHandlerBuilder, o...)
}