-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
59 lines (44 loc) · 1.44 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 (
"fmt"
"flag"
"net/http"
"time"
"git-go-websiteskeleton/app/common"
"git-go-websiteskeleton/app/home"
"git-go-websiteskeleton/app/user"
"github.com/golang/glog"
"github.com/gorilla/mux"
)
func main() {
flag.Parse()
defer glog.Flush()
router := mux.NewRouter()
http.Handle("/", httpInterceptor(router))
router.HandleFunc("/", home.GetHomePage).Methods("GET")
router.HandleFunc("/user{_:/?}", user.GetHomePage).Methods("GET")
router.HandleFunc("/user/view/{id:[0-9]+}", user.GetViewPage).Methods("GET")
router.HandleFunc("/user/{id:[0-9]+}", user.GetViewPage).Methods("GET")
fileServer := http.StripPrefix("/static/", http.FileServer(http.Dir("static")))
http.Handle("/static/", fileServer)
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println(err)
}
}
func httpInterceptor(router http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
startTime := time.Now()
router.ServeHTTP(w, req)
finishTime := time.Now()
elapsedTime := finishTime.Sub(startTime)
switch req.Method {
case "GET":
// We may not always want to StatusOK, but for the sake of
// this example we will
common.LogAccess(w, req, elapsedTime)
case "POST":
// here we might use http.StatusCreated
}
})
}