forked from bluenviron/gohlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuxer_server.go
46 lines (37 loc) · 877 Bytes
/
muxer_server.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
package gohlslib
import (
"net/http"
"path/filepath"
"sync"
)
type muxerServer struct {
mutex sync.RWMutex
pathHandlers map[string]http.HandlerFunc
}
func (s *muxerServer) initialize() {
s.pathHandlers = make(map[string]http.HandlerFunc)
}
func (s *muxerServer) registerPath(path string, cb http.HandlerFunc) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.pathHandlers[path] = cb
}
func (s *muxerServer) unregisterPath(path string) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.pathHandlers, path)
}
func (s *muxerServer) getPathHandler(path string) http.HandlerFunc {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.pathHandlers[path]
}
func (s *muxerServer) handle(w http.ResponseWriter, r *http.Request) {
path := filepath.Base(r.URL.Path)
s.mutex.RLock()
handler, ok := s.pathHandlers[path]
s.mutex.RUnlock()
if ok {
handler(w, r)
}
}