-
Notifications
You must be signed in to change notification settings - Fork 2
/
moxy.go
141 lines (131 loc) · 3.06 KB
/
moxy.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"github.com/BurntSushi/toml"
"github.com/mailgun/oxy/forward"
"github.com/mailgun/oxy/roundrobin"
"github.com/peterbourgon/g2s"
"github.com/thoas/stats"
)
type App struct {
Tasks []string
Fwd *forward.Forwarder `json:"-"`
Lb *roundrobin.RoundRobin `json:"-"`
}
type Apps struct {
sync.RWMutex
Apps map[string]App
}
var apps Apps
type Config struct {
Port string
Xproxy string
Marathon string
Statsd string
Prefix string
TLS bool
Cert string
Key string
}
var config Config
var statsd g2s.Statter
func moxy_proxy(w http.ResponseWriter, r *http.Request) {
app := strings.Split(r.Host, ".")[0]
if config.Xproxy != "" {
w.Header().Add("X-Proxy", config.Xproxy)
}
if s, ok := apps.Apps[app]; ok {
if config.Statsd != "" {
go func(app string) {
statsd.Counter(1.0, config.Prefix+app, 1)
}(app)
}
// let us forward this request to a running container
s.Lb.ServeHTTP(w, r)
return
}
w.WriteHeader(503)
fmt.Fprintln(w, "503 Service Unavailable\n\nNo server is available to handle this request.")
}
func moxy_callback(w http.ResponseWriter, r *http.Request) {
log.Println("callback received from Marathon")
if config.Xproxy != "" {
w.Header().Add("X-Proxy", config.Xproxy)
}
select {
case callbackqueue <- true: // Add reload to our queue channel, unless it is full of course.
w.WriteHeader(202)
fmt.Fprintln(w, "queued")
return
default:
w.WriteHeader(202)
fmt.Fprintln(w, "queue is full")
return
}
}
func moxy_apps(w http.ResponseWriter, r *http.Request) {
if config.Xproxy != "" {
w.Header().Add("X-Proxy", config.Xproxy)
}
b, _ := json.MarshalIndent(apps, "", " ")
w.Write(b)
return
}
func main() {
configtoml := flag.String("f", "moxy.toml", "Path to config. (default moxy.toml)")
flag.Parse()
file, err := ioutil.ReadFile(*configtoml)
if err != nil {
log.Fatal(err)
}
err = toml.Unmarshal(file, &config)
if err != nil {
log.Fatal("Problem parsing config: ", err)
}
if config.Statsd != "" {
statsd, _ = g2s.Dial("udp", config.Statsd)
}
moxystats := stats.New()
mux := http.NewServeMux()
mux.HandleFunc("/moxy_callback", moxy_callback)
mux.HandleFunc("/moxy_apps", moxy_apps)
mux.HandleFunc("/moxy_stats", func(w http.ResponseWriter, req *http.Request) {
if config.Xproxy != "" {
w.Header().Add("X-Proxy", config.Xproxy)
}
stats := moxystats.Data()
b, _ := json.MarshalIndent(stats, "", " ")
w.Write(b)
return
})
mux.HandleFunc("/", moxy_proxy)
// In case we want to log req/resp.
//trace, _ := trace.New(redirect, os.Stdout)
handler := moxystats.Handler(mux)
s := &http.Server{
Addr: ":" + config.Port,
Handler: handler,
}
callbackworker()
callbackqueue <- true
if config.TLS {
log.Println("Starting moxy tls on :" + config.Port)
err := s.ListenAndServeTLS(config.Cert, config.Key)
if err != nil {
log.Fatal(err)
}
} else {
log.Println("Starting moxy on :" + config.Port)
err := s.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
}