This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.go
92 lines (78 loc) · 2.3 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
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
// Copyright 2016 Fraunhofer Institute for Applied Information Technology FIT
package main
import (
"fmt"
"log"
"net/http"
"runtime/debug"
"time"
"github.com/codegangsta/negroni"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/linksmart/historical-datastore/common"
"github.com/rs/cors"
)
type router struct {
*mux.Router
alice.Chain
}
func newRouter() *router {
r := router{Router: mux.NewRouter().
StrictSlash(false).
SkipClean(true), //Enable complex urls in senml name. like "/data/http://example.com/temperatureSensor"
}
// default handler(s)
r.handle(http.MethodGet, "/health", healthHandler)
// middleware chain for handler, used when calling chained()
r.Chain = alice.New(
context.ClearHandler,
loggingHandler,
recoverHandler,
cors.AllowAll().Handler,
)
return &r
}
func (r *router) appendChain(handler alice.Constructor) {
r.Chain = r.Chain.Append(handler)
}
// chained chains the middleware and returns the final handler
func (r *router) chained() http.Handler {
return r.Then(r)
}
func (r *router) handle(m, p string, f http.HandlerFunc) {
// handle with and without slash (no redirects)
r.Methods(m).Path(p).HandlerFunc(f)
r.Methods(m).Path(fmt.Sprintf("%s/", p)).HandlerFunc(f)
}
func loggingHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
nw := negroni.NewResponseWriter(w)
if common.Debug {
log.Printf("\"%s %s\" %d\n", r.Method, r.URL.String(), r.ContentLength)
}
next.ServeHTTP(nw, r)
log.Printf("\"%s %s %s\" %d %d %v\n", r.Method, r.URL.String(), r.Proto, nw.Status(), nw.Size(), time.Since(t1))
}
return http.HandlerFunc(fn)
}
func recoverHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
log.Printf("PANIC: %v\n%v", r, string(debug.Stack()))
http.Error(w, http.StatusText(500), 500)
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", common.DefaultMIMEType)
fmt.Fprintf(w, "{\"status\":\"OK\"}")
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Historical Datastore %s - Welcome!\n", common.APIVersion)
}