forked from martensson/nixy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixy.go
158 lines (143 loc) · 3.48 KB
/
nixy.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"sync"
"github.com/BurntSushi/toml"
"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/peterbourgon/g2s"
)
type App struct {
Tasks []string
Labels map[string]string
Env map[string]string
Hosts []string
}
type Config struct {
sync.RWMutex
Xproxy string
Port string `json:"-"`
Marathon []string `json:"-"`
User string `json:"-"`
Pass string `json:"-"`
Nginx_config string `json:"-"`
Nginx_template string `json:"-"`
Nginx_cmd string `json:"-"`
Statsd StatsdConfig
Apps map[string]App
}
type StatsdConfig struct {
Addr string
Namespace string
SampleRate int `toml:"sample_rate"`
}
// Global variables
var VERSION string //added by goxc
var config Config
var statsd g2s.Statter
var endpoint string
var sick int
var logger = logrus.New()
// buffer of two, because we dont really need more.
var eventqueue = make(chan bool, 2)
// Global http transport for connection reuse
var tr = &http.Transport{}
func nixy_reload(w http.ResponseWriter, r *http.Request) {
logger.WithFields(logrus.Fields{
"client": r.RemoteAddr,
}).Info("marathon reload triggered")
select {
case eventqueue <- 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 nixy_health(w http.ResponseWriter, r *http.Request) {
health := make(map[string]string)
err := checkTmpl()
if err != nil {
w.WriteHeader(500)
health["Template"] = err.Error()
} else {
health["Template"] = "OK"
}
err = checkConf()
if err != nil {
w.WriteHeader(500)
health["Config"] = err.Error()
} else {
health["Config"] = "OK"
}
if sick == len(config.Marathon) {
w.WriteHeader(500)
health["Endpoints"] = "All endpoints are down"
} else {
health["Endpoints"] = strconv.Itoa(sick) + " SICK"
}
w.Header().Add("Content-Type", "application/json; charset=utf-8")
b, _ := json.MarshalIndent(health, "", " ")
w.Write(b)
return
}
func nixy_config(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json; charset=utf-8")
b, _ := json.MarshalIndent(config, "", " ")
w.Write(b)
return
}
func nixy_version(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "nixy "+VERSION)
return
}
func main() {
configtoml := flag.String("f", "nixy.toml", "Path to config. (default nixy.toml)")
version := flag.Bool("v", false, "prints current nixy version")
flag.Parse()
if *version {
fmt.Println(VERSION)
os.Exit(0)
}
file, err := ioutil.ReadFile(*configtoml)
if err != nil {
logger.WithFields(logrus.Fields{
"error": err.Error(),
}).Fatal("problem opening toml config")
}
err = toml.Unmarshal(file, &config)
if err != nil {
logger.WithFields(logrus.Fields{
"error": err.Error(),
}).Fatal("problem parsing config")
}
statsd, _ = setupStatsd()
mux := mux.NewRouter()
mux.HandleFunc("/", nixy_version)
mux.HandleFunc("/v1/reload", nixy_reload)
mux.HandleFunc("/v1/config", nixy_config)
mux.HandleFunc("/v1/health", nixy_health)
s := &http.Server{
Addr: ":" + config.Port,
Handler: mux,
}
endpoint = config.Marathon[0] // lets start with the first node.
endpointHealth()
eventStream()
eventWorker()
logger.Info("starting nixy on :" + config.Port)
err = s.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}