forked from martensson/vaban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaban.go
81 lines (73 loc) · 1.93 KB
/
vaban.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
/* Vaban - The Simple Varnish Ban REST Api. <benjamin@martensson.io> */
package main
import (
"flag"
"log"
"net/http"
"os"
"github.com/codegangsta/negroni"
"github.com/julienschmidt/httprouter"
"github.com/pilu/xrequestid"
"github.com/thoas/stats"
"github.com/unrolled/render"
"gopkg.in/yaml.v3"
)
type Message struct {
Msg string
}
type Messages map[string]Message
type Service struct {
Hosts []string
Secret string
}
type Services map[string]Service
var services Services
var r = render.New(render.Options{
IndentJSON: true,
})
func initialize() *negroni.Negroni {
vabanstats := stats.New()
n := negroni.New(
negroni.NewRecovery(),
NewLogger(),
xrequestid.New(8),
)
router := httprouter.New()
router.GET("/", func(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
stats := vabanstats.Data()
err := r.JSON(w, http.StatusOK, stats)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write([]byte(err.Error()))
if err != nil {
log.Println(err)
}
}
})
router.GET("/v1/services", GetServices)
router.GET("/v1/service/:service", GetService)
router.GET("/v1/service/:service/ping", GetPing)
router.GET("/v1/service/:service/health", GetHealth)
router.GET("/v1/service/:service/health/:backend", GetHealth)
router.POST("/v1/service/:service/health/:backend", PostHealth)
router.POST("/v1/service/:service/ban", PostBan)
// add router and clear mux.context values at the end of request life-times
n.UseHandler(router)
return n
}
func main() {
port := flag.String("p", "4000", "Listen on this port. (default 4000)")
config := flag.String("f", "config.yml", "Path to config. (default config.yml)")
flag.Parse()
file, err := os.ReadFile(*config)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(file, &services)
if err != nil {
log.Fatal("Problem parsing config: ", err)
}
n := initialize()
log.Println("Starting vaban on :" + *port)
n.Run(":" + *port)
}