-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddama.go
120 lines (97 loc) · 3.82 KB
/
addama.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
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
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright (C) 2003-2011 Institute for Systems Biology
Seattle, Washington, USA.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"fmt"
"github.com/codeforsystemsbiology/httplib.go"
"github.com/dlintw/goconf"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type AddamaConnection struct {
target string
connectionFile string
serviceHost string
serviceName string
uri string
label string
apikey string
}
func NewAddamaProxy(addamaConn AddamaConnection) *AddamaProxy {
target := addamaConn.target
connectionFile := addamaConn.connectionFile
serviceHost := addamaConn.serviceHost
serviceName := addamaConn.serviceName
uri := addamaConn.uri
label := addamaConn.label
apikey := addamaConn.apikey
registrar := NewRegistrar(connectionFile)
serviceUri := "/addama/services/golem/" + serviceName
service := fmt.Sprintf(" { uri: '%v', url: '%v', label: '%v' } ", serviceUri, serviceHost, label)
header := registrar.Register("/addama/registry/services/"+serviceName, "service", service)
registrykey := header.Get("x-addama-registry-key")
logger.Printf("registrykey:%v", registrykey)
mapping := fmt.Sprintf(" { uri: '%v', label: '%v', service: '%v' } ", uri, label, serviceUri)
registrar.Register("/addama/registry/mappings"+uri, "mapping", mapping)
targetUrl, _ := url.Parse(target)
return &AddamaProxy{target: targetUrl, registrykey: registrykey, apikey: apikey, baseuri: uri}
}
func NewRegistrar(connectionFilePath string) *Registrar {
connectionFile, _ := goconf.ReadConfigFile(connectionFilePath)
host, _ := connectionFile.GetString("Connection", "host")
apikey, _ := connectionFile.GetString("Connection", "apikey")
logger.Debug("NewRegistrar(%v):%v,%v", connectionFilePath, host, apikey)
return &Registrar{host: "https://" + host, apikey: apikey}
}
type Registrar struct {
host string
apikey string
}
func (this *Registrar) Register(uri string, registrationType string, registration string) http.Header {
logger.Debug("Register(%v%v, %v, %v)", this.host, uri, registrationType, registration)
requestBuilder := httplib.Post(this.host + uri)
requestBuilder.Header("x-addama-apikey", this.apikey)
requestBuilder.Param(registrationType, registration)
resp, err := requestBuilder.AsResponse()
if err != nil {
panic(err)
}
if resp.StatusCode != http.StatusOK {
panic(fmt.Sprintf("unable to properly register:%v", resp))
}
logger.Debug("Register(%v%v): %d", this.host, uri, resp.StatusCode)
return resp.Header
}
type AddamaProxy struct {
target *url.URL
registrykey string
apikey string
baseuri string
}
func (this *AddamaProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger.Printf("AddamaProxy.ServeHTTP(%v %v)", r.Method, r.URL.Path)
if this.registrykey != r.Header.Get("x-addama-registry-key") {
http.Error(w, "Registry key does not match", http.StatusForbidden)
return
}
uri := strings.Replace(r.URL.Path, this.baseuri, "", -1)
preq, _ := http.NewRequest(r.Method, uri, r.Body)
preq.Header.Set("x-golem-apikey", this.apikey)
proxy := httputil.NewSingleHostReverseProxy(this.target)
go proxy.ServeHTTP(w, preq)
}