-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtile_proxy.go
81 lines (70 loc) · 1.71 KB
/
tile_proxy.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
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
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
}
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()
}
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()
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()
}
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.m.Lock()
if d, ok := s.Services[serviceId]; ok {
s.m.Unlock()
d.ServeHTTP(w, r)
} else {
s.m.Unlock()
w.WriteHeader(404)
}
}
func NewTileProxy(globals *setting.GlobalsSetting, proxys []*setting.ProxyService, fac setting.CacheFactory) *TileProxy {
proxy := &TileProxy{globals: globals}
proxy.Reload(proxys, fac)
return proxy
}