-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
84 lines (71 loc) · 1.84 KB
/
config.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"log"
"net/http"
"strconv"
"strings"
)
type contextKey int
const (
authRoleContext contextKey = iota
remoteLoggerContext
requestNumberContext
)
type options map[string]string
func parseOptString(params string) options {
ret := map[string]string{}
for _, s := range strings.Split(params, ",") {
if eqIdx := strings.Index(s, "="); eqIdx == -1 {
if strings.HasPrefix(s, "no-") {
ret[s[3:]] = "false"
} else {
ret[s] = "true"
}
} else {
ret[s[:eqIdx]] = s[eqIdx+1:]
}
}
return ret
}
func (opts options) IsTrue(option string, defaultValue bool) bool {
if vStr, have := opts[option]; have {
if v, err := strconv.ParseBool(vStr); err == nil {
return v
} else {
logf(nil, logLevelWarning, "cannot parse boolean from %#v: %s", vStr, err)
}
}
return defaultValue
}
func (opts options) IsSet(option string) bool {
if _, have := opts[option]; have {
return true
}
return false
}
func parseCurlyParams(handlerParams string) (map[string]string, string) {
connectParams := make(map[string]string)
if strings.HasPrefix(handlerParams, "{") {
ebIndex := strings.Index(handlerParams, "}")
if ebIndex < 0 {
log.Fatal("Invalid parameter syntax, missing '}'")
}
for _, s := range strings.Split(handlerParams[1:ebIndex], ",") {
kv := strings.SplitN(s, "=", 2)
connectParams[kv[0]] = kv[1]
}
handlerParams = handlerParams[ebIndex+1:]
}
return connectParams, handlerParams
}
type serverConfig struct {
logger serverLogger
certFile string
}
type protocoHandlerCreator func(urlPath, params string, cfg *serverConfig) (http.Handler, error)
var protocolHandlers = map[string]protocoHandlerCreator{}
var customHttpSchemas = make(map[string]func() http.RoundTripper)
func addProtocolHandler(proto string, createFunc protocoHandlerCreator) error {
protocolHandlers[proto] = createFunc
return nil
}