-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.go
45 lines (39 loc) · 1.21 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"net/http"
"time"
"github.com/edjumacator/chi-prometheus"
"github.com/go-chi/chi"
"github.com/prometheus/client_golang/prometheus"
)
func main() {
n := chi.NewRouter()
m := chiprometheus.NewPatternMiddleware("test_service")
n.Use(m)
n.Handle("/metrics", prometheus.Handler())
n.Get("/ok", func(w http.ResponseWriter, r *http.Request) {
sleep := rand.Intn(4999) + 1
time.Sleep(time.Duration(sleep) * time.Millisecond)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "slept %d milliseconds\n", sleep)
})
n.Get("/users/{firstName}", func(w http.ResponseWriter, r *http.Request) {
sleep := rand.Intn(4999) + 1
time.Sleep(time.Duration(sleep) * time.Millisecond)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "slept %d milliseconds\n", sleep)
})
n.Get("/users/{id}/contacts", func(w http.ResponseWriter, r *http.Request) {
sleep := rand.Intn(4999) + 1
time.Sleep(time.Duration(sleep) * time.Millisecond)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "slept %d milliseconds\n", sleep)
})
n.Get("/notfound", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "not found")
})
http.ListenAndServe(":3000", n)
}