/
router.go
58 lines (49 loc) · 1.6 KB
/
router.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
package main
import (
"github.com/gorilla/mux"
"net/http"
"time"
"petwork.core/util"
"github.com/gorilla/context"
)
func Router() http.Handler {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", IndexHandler)
router.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
return context.ClearHandler(Interceptor(CorsSupport(router)))
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
util.Response{
Message: "Welcome to Vault-Tec" }.Writer(w).Status(108, http.StatusOK).Json()
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
http.Error(
w,
util.Response{ Code: http.StatusNotFound, Message: "Not Found" }.ToJSON(),
http.StatusNotFound)
}
func CorsSupport(h http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Credentials", "false")
w.Header().Set("Access-Control-Max-Age", "86400")
w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
} else {
h.ServeHTTP(w, r)
}
})
}
func Interceptor(h http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(w, r)
elapsed := time.Since(start)
util.Info.Println("URL:", r.URL, "-", "Elapsed Time:", elapsed.String())
})
}