-
Notifications
You must be signed in to change notification settings - Fork 22
/
envresolver.go
55 lines (44 loc) · 1.29 KB
/
envresolver.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
package main
import (
"errors"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type EnvResolver struct {
config *Config
watcher *watcher
services map[string]*ServiceCluster
dest2ProxyCache map[string]http.Handler
}
func NewEnvResolver(c *Config) *EnvResolver {
services := make(map[string]*ServiceCluster)
w, _ := NewEtcdWatcher(c, nil, services)
return &EnvResolver{c, w, services, make(map[string]http.Handler)}
}
func (r *EnvResolver) resolve(domain string) (http.Handler, error) {
serviceName := strings.Split(domain, ".")[0]
serviceTree := r.services[serviceName]
if serviceTree != nil {
if service, err := serviceTree.Next(); err != nil {
uri := fmt.Sprintf("http://%s:%d/", service.location.Host, service.location.Port)
return r.getOrCreateProxyFor(uri), nil
}
}
return nil, errors.New("Unable to resolve")
}
func (r *EnvResolver) init() {
r.watcher.loadAndWatch(r.config.servicePrefix, r.watcher.registerService)
}
func (r *EnvResolver) redirectToStatusPage(domainName string) string {
return ""
}
func (r *EnvResolver) getOrCreateProxyFor(uri string) http.Handler {
if _, ok := r.dest2ProxyCache[uri]; !ok {
dest, _ := url.Parse(uri)
r.dest2ProxyCache[uri] = httputil.NewSingleHostReverseProxy(dest)
}
return r.dest2ProxyCache[uri]
}