-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
119 lines (110 loc) · 2.43 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"net/http"
)
// Route structure
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
// Route slice
type Routes []Route
// createRouter creates router with several routes and sets up the logger
func createRouter(dbs *mgo.Session) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
routes := generateRoutes(dbs)
for _, route := range routes {
handler := Logger(route.HandlerFunc, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
return router
}
// generateRoutes defines route names, methods, paths and handlers
func generateRoutes(dbs *mgo.Session) Routes {
return Routes{
// Index route
Route{
"Index page",
"GET",
"/",
serveIndex,
},
// User login check route
Route{
"Check user logged in",
"GET",
"/user/login/check",
checkUserLogged,
},
// JWT user login route
Route{
"JWT login",
"POST",
"/user/login",
loginUser(dbs),
},
// User creation route
Route{
"Add user",
"POST",
"/users/add",
addUser(dbs),
},
// Route to get all dictionary words
Route{
"Get all dictionary words",
"GET",
"/words",
getWords(dbs),
},
// Route to get all dictionary words including a specific string
Route{
"Get all dictionary words including a specific string",
"POST",
"/words/incl",
getWordsIncl(dbs),
},
// Route to get a specific word (with relative info) from the dictionary
Route{
"Get a specific word (with relative info) from the dictionary",
"GET",
"/word/{word:[a-z]+}",
getWordInfo(dbs),
},
// Route to get a specific word comments
Route{
"Get a specific word comments",
"GET",
"/word/{word:[a-z]+}/comments",
getWordComments(dbs),
},
// Route to add a new word to the dictionary
Route{
"Add a new word to the dictionary",
"POST",
"/words/add",
addWord(dbs),
},
// Route to add a new comment for a specific word of the dictionary
Route{
"Add a new comment for a specific word of the dictionary",
"POST",
"/comments/add",
addComment(dbs),
},
}
}
// serveIndex defines the handler of the index route
func serveIndex(w http.ResponseWriter, r *http.Request) {
Log.Notice("Serve Index")
http.ServeFile(w, r, "static/index.html")
}