-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy.go
68 lines (56 loc) · 1.24 KB
/
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
package proxy
import (
"fmt"
log "github.com/Sirupsen/logrus"
"net/http"
"os"
"regexp"
)
type ProxyConfig struct {
Master string
Slaves []string
AwsConfig *AWSConfig
LogLocation string
Version string
}
func init() {
log.SetFormatter(&log.JSONFormatter{})
}
type Proxy struct {
updater *Updater
reader *Reader
config *ProxyConfig
}
func NewProxy(proxyConfig *ProxyConfig) *Proxy {
updater := NewUpdater(proxyConfig.Master)
reader := NewReader(proxyConfig.Slaves)
if proxyConfig.LogLocation == "stdout" {
log.SetOutput(os.Stdout)
} else {
f, err := os.OpenFile(proxyConfig.LogLocation, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
log.SetOutput(f)
}
return &Proxy{
updater: updater,
reader: reader,
config: proxyConfig,
}
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Println(req.URL.Path)
fmt.Println(req.URL.RawQuery)
re := regexp.MustCompile("\\/solr\\/(.*)\\/update$")
matches := re.FindStringSubmatch(req.URL.Path)
req.Close = true
if len(matches) > 0 {
p.updater.ServeHTTP(w, req, p.config.AwsConfig, matches[1])
} else {
p.reader.ServeHTTP(w, req)
}
}
func writeLog(message string, params ...string) {
log.Printf(message, params)
}