-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
51 lines (37 loc) · 913 Bytes
/
handler.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
package moqt
import (
"strings"
"sync"
)
type HandlerFunc func(ServerSession)
var NotFoundFunc HandlerFunc = func(ServerSession) {}
var DefaultHandler ServeMux = NewServeMux()
func NewServeMux() ServeMux {
return ServeMux{
handlerFuncs: make(map[string]HandlerFunc),
}
}
type ServeMux struct {
mu sync.Mutex
handlerFuncs map[string]HandlerFunc
}
func (h *ServeMux) HandlerFunc(pattern string, op HandlerFunc) {
h.mu.Lock()
defer h.mu.Unlock()
if strings.HasPrefix(pattern, "/") {
panic("invalid path: path should start with \"/\"")
}
h.handlerFuncs[pattern] = op
}
func (mux *ServeMux) findHandlerFunc(pattern string) HandlerFunc {
mux.mu.Lock()
defer mux.mu.Unlock()
handlerFunc, ok := mux.handlerFuncs[pattern]
if !ok {
return NotFoundFunc
}
return handlerFunc
}
func HandleFunc(pattern string, op func(ServerSession)) {
DefaultHandler.HandlerFunc(pattern, op)
}