-
Notifications
You must be signed in to change notification settings - Fork 5
/
routes.go
60 lines (52 loc) · 1.75 KB
/
routes.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
package server
import (
//"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
// Route struct
type Route struct {
Name string // Route Name
Method string // POST or GET
Pattern string // URL
HandlerFunc http.HandlerFunc // Handler function
}
// Routes list
type Routes []Route
var (
xAuthorization = http.CanonicalHeaderKey("Authorization")
)
func authenticationHandler(fn func(w http.ResponseWriter, r *http.Request, uid int)) http.HandlerFunc {
//, username string, APIkey string
return func(w http.ResponseWriter, r *http.Request) {
api_key := r.Header.Get(xAuthorization)
if len(api_key) == 0 {
return
}
uid, err := golog.Authenticate(api_key)
if err != nil {
log.Print(err)
return
}
fn(w, r, uid)
}
}
var routes = Routes{
Route{"Events", "GET", "/api/events/{date:[0-9-]+}", authenticationHandler(Events)},
Route{"RecentWindows", "GET", "/api/events/recent", authenticationHandler(RecentWindows)},
Route{"KeyEvents", "GET", "/api/events/key/{date:[0-9-]+}", authenticationHandler(KeyEvents)},
Route{"WinEvents", "GET", "/api/events/win/{date:[0-9-]+}", authenticationHandler(WinEvents)},
Route{"AddNote", "POST", "/api/notes", authenticationHandler(AddNote)},
Route{"AddBlog", "POST", "/api/blog", authenticationHandler(AddBlog)},
Route{"DataUpload", "POST", "/logs", DataUpload},
Route{"ExportList", "GET", "/export_list.json", authenticationHandler(ExportList)},
}
// RegisterRoutes operates over `Routes` and registers all of them
func RegisterRoutes(router *mux.Router) *mux.Router {
for _, route := range routes {
router.Methods(route.Method).Path(route.Pattern).Name(route.Name).Handler(route.HandlerFunc)
}
router.PathPrefix("/").Handler(http.FileServer(assetFS()))
return router
}