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
package oauthcallback
import (
"context"
"net/http"
"strings"
"sync"
)
type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request) error
type Registry struct {
mu sync.RWMutex
handlers map[string]Handler
}
func NewRegistry() *Registry {
return &Registry{handlers: map[string]Handler{}}
}
func (r *Registry) Register(name string, handler Handler) {
name = strings.TrimSpace(name)
if r == nil || name == "" || handler == nil {
return
}
r.mu.Lock()
r.handlers[name] = handler
r.mu.Unlock()
}
func (r *Registry) Lookup(name string) (Handler, bool) {
if r == nil {
return nil, false
}
r.mu.RLock()
handler, ok := r.handlers[strings.TrimSpace(name)]
r.mu.RUnlock()
return handler, ok
}