-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.go
130 lines (108 loc) · 3.65 KB
/
example.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
package main
import (
"encoding/json"
"net/http"
"os"
"time"
ranger_http "github.com/foodora/go-ranger/ranger_http"
ranger_logger "github.com/foodora/go-ranger/ranger_logger"
ranger_metrics "github.com/foodora/go-ranger/ranger_metrics"
"github.com/julienschmidt/httprouter"
)
var (
logger ranger_logger.LoggerInterface
logstash ranger_logger.Hook
slack ranger_logger.Hook
rangerMetrics ranger_http.MiddlewareInterface
requestLogger ranger_http.MiddlewareInterface
)
func init() {
// you can use all logrus hooks. we provide some useful with go-ranger
logstash = ranger_logger.NewLogstashHook(
"tcp",
"localhost:1234",
&ranger_logger.JSONFormatter{},
)
slack = ranger_logger.NewSlackHook(
"#my-channel",
"https://hooks.slack.com/services/T00/B00/absfmzyy",
"debug",
)
logger = ranger_logger.NewLogger(
os.Stdout,
ranger_logger.LoggerData{"environment": "development"},
&ranger_logger.JSONFormatter{},
"debug",
logstash,
slack,
)
rangerMetrics = ranger_metrics.NewNewRelic("Your App Name", "<your-key-goes-here>....................", logger)
}
func main() {
s := ranger_http.NewHTTPServer(logger).
// you can add as many middlewares as you want. they will be applied in the same order
// sampleMiddlewar -> anotherSampleMiddleware -> ranger_http.RequestLog
WithMiddleware(
rangerMetrics.Middleware,
sampleMiddleware,
anotherSampleMiddleware,
ranger_http.LoggerMiddleware,
).
// with this we provide a default http 404 and 500 error.
// see more on response_writer.go
WithDefaultErrorRoute().
// basic health check endpoints
// /health/check/lb and /health/check
// any instance of `func() ranger_http.HealthCheckService` sent as parameter of the configuration will be converted to JSON and printed
// if necessary, you also can add a prefix to the endpoints with WithPrefix("/prefix")
// ex: WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(someHealthCheck()).WithPrefix("/prefix"))
WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(etcdHealthCheck()).WithVersion("version.json"))
// add some endpoints. based on "github.com/julienschmidt/httprouter"
s.GET("/hello", helloEndpoint())
addr := ":8080"
// LoggerData is a map[string]interface{} struct
logger.Info("Listening to address:", ranger_logger.LoggerData{"addr": addr})
// decided to keep this startup process under control of the user, outside the go-ranger toolkit
if err := http.ListenAndServe(addr, s.Start()); err != nil {
logger.Error(err.Error(), nil)
}
}
// some endpoints for demonstration purpose
func sampleMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
logger.Info("middleware", nil)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func anotherSampleMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
logger.Info("another middleware!", nil)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// just an example
func helloEndpoint() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
json.NewEncoder(w).Encode("Hello world")
}
}
func etcdHealthCheck() func() ranger_http.HealthCheckService {
type etcdHealthCheck struct {
ResponseTime int `json:"response_time"`
}
return func() ranger_http.HealthCheckService {
//some logic here to get etcd response time
var crazyLogic int
crazyLogic = int(time.Now().Unix() % 10)
return ranger_http.HealthCheckService{
Name: "etcd",
Status: true,
Info: etcdHealthCheck{
ResponseTime: crazyLogic,
},
}
}
}