-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
59 lines (55 loc) · 1.72 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"flag"
"log"
"net/http"
"github.com/gorilla/mux"
)
var (
port = flag.String("port", "8080", "port")
)
/*
curl -I 'localhost:8080/api/'
curl -I 'localhost:8080/api/v1/'
curl -I 'localhost:8080/api/v1/status'
curl -I 'localhost:8080/api/v2/'
curl -I 'localhost:8080/api/v2/status'
curl -I 'localhost:8080/api/v1/status' -H "x-auth-token: admin"
curl -I 'localhost:8080/api/v1/status' -H "x-auth-token: notadmin"
*/
func main() {
flag.Parse()
var router = mux.NewRouter()
var api = router.PathPrefix("/api").Subrouter()
// var api = router.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
// return r.Header.Get("x-auth-token") == "admin"
// }).PathPrefix("/api").Subrouter()
api.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
api.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("x-auth-token") != "admin" {
w.WriteHeader(http.StatusUnauthorized)
return
}
log.Println(r.RequestURI)
next.ServeHTTP(w, r)
})
})
var api1 = api.PathPrefix("/v1").Subrouter()
api1.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
api1.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
})
var api2 = api.PathPrefix("/v2").Subrouter()
api2.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
})
api2.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
http.ListenAndServe(":"+*port, router)
}