-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.go
56 lines (44 loc) · 1.06 KB
/
options.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
// SPDX-License-Identifier: MIT
// Package options 构建路由的参数
package options
import (
"net/http"
"github.com/issue9/mux/v7/internal/syntax"
)
type (
Option func(*Options)
Options struct {
Lock bool
CORS *CORS
Interceptors *syntax.Interceptors
URLDomain string
RecoverFunc RecoverFunc
Connections []ConnectionFunc
}
// OnConnection 每个新链接到来时对 http.ResponseWriter 和 *http.Request 的处理
ConnectionFunc func(http.ResponseWriter, *http.Request) (http.ResponseWriter, *http.Request)
RecoverFunc func(http.ResponseWriter, any)
)
func Build(o ...Option) (*Options, error) {
ret := &Options{Interceptors: syntax.NewInterceptors()}
for _, opt := range o {
opt(ret)
}
if err := ret.sanitize(); err != nil {
return nil, err
}
return ret, nil
}
func (o *Options) sanitize() error {
if o.CORS == nil {
o.CORS = &CORS{}
}
if err := o.CORS.sanitize(); err != nil {
return err
}
l := len(o.URLDomain)
if l != 0 && o.URLDomain[l-1] == '/' {
o.URLDomain = o.URLDomain[:l-1]
}
return nil
}