-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_router.go
More file actions
52 lines (41 loc) · 1.12 KB
/
static_router.go
File metadata and controls
52 lines (41 loc) · 1.12 KB
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
package pgmux
import (
"context"
"sync"
)
// StaticRouter implements Router with a static mapping of users to backends
type StaticRouter struct {
mappings map[string]*BackendConfig
mu sync.RWMutex
}
// NewStaticRouter creates a new StaticRouter with the provided mappings
func NewStaticRouter(mappings map[string]*BackendConfig) *StaticRouter {
return &StaticRouter{
mappings: mappings,
}
}
// Route returns the backend configuration for the given username
func (r *StaticRouter) Route(ctx context.Context, username string) (*BackendConfig, error) {
r.mu.RLock()
defer r.mu.RUnlock()
config, exists := r.mappings[username]
if !exists {
return nil, ErrUserNotFound
}
return config, nil
}
// AddMapping adds or updates a user mapping
func (r *StaticRouter) AddMapping(username string, config *BackendConfig) {
r.mu.Lock()
defer r.mu.Unlock()
if r.mappings == nil {
r.mappings = make(map[string]*BackendConfig)
}
r.mappings[username] = config
}
// RemoveMapping removes a user mapping
func (r *StaticRouter) RemoveMapping(username string) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.mappings, username)
}