-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtile_proxy.go
More file actions
109 lines (90 loc) · 2.27 KB
/
tile_proxy.go
File metadata and controls
109 lines (90 loc) · 2.27 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tileproxy
import (
"net/http"
"regexp"
"sync"
"github.com/flywave/go-tileproxy/setting"
)
type TileProxy struct {
m sync.RWMutex
Services map[string]*Service
globals *setting.GlobalsSetting
serviceReqRegex *regexp.Regexp
serviceCache map[string]*Service
serviceCacheMu sync.RWMutex
}
func (t *TileProxy) UpdateService(id string, d *setting.ProxyService, fac setting.CacheFactory) {
t.m.Lock()
t.Services[id] = NewService(d, t.globals, fac)
t.m.Unlock()
t.serviceCacheMu.Lock()
t.serviceCache[id] = t.Services[id]
t.serviceCacheMu.Unlock()
}
func (t *TileProxy) RemoveService(id string) {
var d *Service
t.m.Lock()
if d_, ok := t.Services[id]; ok {
d = d_
}
delete(t.Services, id)
t.m.Unlock()
t.serviceCacheMu.Lock()
delete(t.serviceCache, id)
t.serviceCacheMu.Unlock()
d.Clean()
}
func (t *TileProxy) Reload(proxy []*setting.ProxyService, fac setting.CacheFactory) {
t.m.Lock()
t.Services = make(map[string]*Service)
for i := range proxy {
t.Services[proxy[i].Id] = NewService(proxy[i], t.globals, fac)
}
t.m.Unlock()
t.serviceCacheMu.Lock()
t.serviceCache = make(map[string]*Service)
t.serviceCacheMu.Unlock()
}
func (t *TileProxy) parseServiceId(r *http.Request) string {
match := t.serviceReqRegex.FindStringSubmatch(r.URL.Path)
groupNames := t.serviceReqRegex.SubexpNames()
result := make(map[string]string)
for i, name := range groupNames {
if name != "" && match[i] != "" {
result[name] = match[i]
}
}
if len(match) == 0 {
return ""
}
return result["service"]
}
func (s *TileProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
serviceId := s.parseServiceId(r)
if serviceId == "" {
w.WriteHeader(404)
return
}
s.serviceCacheMu.RLock()
d, ok := s.serviceCache[serviceId]
s.serviceCacheMu.RUnlock()
if !ok {
s.m.RLock()
d, ok = s.Services[serviceId]
s.m.RUnlock()
if ok {
s.serviceCacheMu.Lock()
s.serviceCache[serviceId] = d
s.serviceCacheMu.Unlock()
} else {
w.WriteHeader(404)
return
}
}
d.ServeHTTP(w, r)
}
func NewTileProxy(globals *setting.GlobalsSetting, proxys []*setting.ProxyService, fac setting.CacheFactory) *TileProxy {
proxy := &TileProxy{globals: globals, serviceCache: make(map[string]*Service)}
proxy.Reload(proxys, fac)
return proxy
}