@@ -14,7 +14,6 @@ import (
14
14
"github.com/go-stuff/mongostore"
15
15
"github.com/golang/protobuf/ptypes/timestamp"
16
16
"github.com/gorilla/mux"
17
- "github.com/gorilla/sessions"
18
17
"go.mongodb.org/mongo-driver/mongo"
19
18
"google.golang.org/grpc"
20
19
@@ -46,7 +45,17 @@ func Init(mongoclient *mongo.Client, mongostore *mongostore.MongoStore, apiclien
46
45
47
46
router = initRouter ()
48
47
49
- CompileRoutes ()
48
+ // seed roles
49
+ err = roleSeed ()
50
+ if err != nil {
51
+ log .Fatal (err )
52
+ }
53
+
54
+ // seed routes
55
+ err = routeSeed ()
56
+ if err != nil {
57
+ log .Fatal (err )
58
+ }
50
59
51
60
return router
52
61
}
@@ -57,8 +66,14 @@ func initTemplates() error {
57
66
// initialize the content files templates map
58
67
templates = make (map [string ]* template.Template )
59
68
69
+ // build templates with auth and content
70
+ err := initTemplatesWithAuthAndContent ()
71
+ if err != nil {
72
+ return err
73
+ }
74
+
60
75
// build templates with content
61
- err : = initTemplatesWithContent ()
76
+ err = initTemplatesWithContent ()
62
77
if err != nil {
63
78
return err
64
79
}
@@ -69,16 +84,35 @@ func initTemplates() error {
69
84
return err
70
85
}
71
86
72
- // // build noauth template
73
- // err = initTemplateNoAuth()
74
- // if err != nil {
75
- // return err
76
- // }
87
+ return nil
88
+ }
89
+
90
+ func initTemplatesWithAuthAndContent () error {
91
+ log .Println ("INFO > controllers/controllers.go > initTemplatesWithAuthAndContent()" )
92
+
93
+ layout = template .New ("mainAuthContent.html" )
94
+
95
+ layout .Funcs (timestampFM ())
96
+ layout .Funcs (permissionFM (nil ))
97
+
98
+ // check the validity of login.html by parsing
99
+ _ , err := layout .ParseFiles (
100
+ "./templates/layout/mainAuthContent.html" ,
101
+ "./templates/layout/head.html" ,
102
+ "./templates/layout/header.html" ,
103
+ "./templates/layout/footer.html" ,
104
+ "./templates/layout/script.html" ,
105
+ )
106
+ if err != nil {
107
+ return err
108
+ }
109
+
110
+ // recurse content files templates and build separate templates for each of them
111
+ filepath .Walk ("./templates/mainAuthContent" , walkTemplatesPath )
77
112
78
113
return nil
79
114
}
80
115
81
- // <html> head, header, content, footer </html
82
116
func initTemplatesWithContent () error {
83
117
log .Println ("INFO > controllers/controllers.go > initTemplatesWithContent()" )
84
118
@@ -88,21 +122,24 @@ func initTemplatesWithContent() error {
88
122
layout .Funcs (permissionFM (nil ))
89
123
90
124
// check the validity of login.html by parsing
91
- layout .ParseFiles (
125
+ _ , err := layout .ParseFiles (
92
126
"./templates/layout/mainContent.html" ,
93
127
"./templates/layout/head.html" ,
94
128
"./templates/layout/header.html" ,
95
- "./templates/layout/bypass .html" ,
129
+ "./templates/layout/logout .html" ,
96
130
"./templates/layout/footer.html" ,
131
+ "./templates/layout/script.html" ,
97
132
)
133
+ if err != nil {
134
+ return err
135
+ }
98
136
99
137
// recurse content files templates and build separate templates for each of them
100
138
filepath .Walk ("./templates/mainContent" , walkTemplatesPath )
101
139
102
140
return nil
103
141
}
104
142
105
- // <html> head, header, menu, content, footer </html
106
143
func initTemplatesWithNavAndContent () error {
107
144
log .Println ("INFO > controllers/controllers.go > initTemplatesWithNavAndContent()" )
108
145
//var err error
@@ -113,53 +150,25 @@ func initTemplatesWithNavAndContent() error {
113
150
layout .Funcs (permissionFM (nil ))
114
151
115
152
// check the validity of the files that make up layout.html by parsing
116
- layout .ParseFiles (
153
+ _ , err := layout .ParseFiles (
117
154
"./templates/layout/mainNavContent.html" ,
118
155
"./templates/layout/head.html" ,
119
156
"./templates/layout/header.html" ,
120
- "./templates/layout/bypass.html" ,
121
- "./templates/layout/logout.html" ,
122
157
"./templates/layout/nav.html" ,
158
+ "./templates/layout/logout.html" ,
123
159
"./templates/layout/footer.html" ,
160
+ "./templates/layout/script.html" ,
124
161
)
125
- // if err != nil {
126
- // return err
127
- // }
162
+ if err != nil {
163
+ return err
164
+ }
128
165
129
166
// recurse content files templates and build separate templates for each of them
130
167
filepath .Walk ("./templates/mainMenuContent" , walkTemplatesPath )
131
168
132
169
return nil
133
170
}
134
171
135
- // // <html> head, header, menu, content, footer </html
136
- // func initTemplateNoAuth() error {
137
- // log.Println("INFO > controllers/controllers.go > initTemplateNoAuth()")
138
- // //var err error
139
-
140
- // layout = template.New("mainContent.html")
141
-
142
- // layout.Funcs(timestampFM())
143
- // layout.Funcs(permissionFM(nil))
144
-
145
- // // check the validity of the files that make up layout.html by parsing
146
- // layout.ParseFiles(
147
- // "./templates/layout/mainContent.html",
148
- // "./templates/layout/head.html",
149
- // "./templates/layout/header.html",
150
- // "./templates/layout/bypass.html",
151
- // "./templates/layout/footer.html",
152
- // )
153
- // // if err != nil {
154
- // // return err
155
- // // }
156
-
157
- // // recurse content files templates and build separate templates for each of them
158
- // filepath.Walk("./templates/mainNoAuth", walkTemplatesPath)
159
-
160
- // return nil
161
- // }
162
-
163
172
// recurse a directory and build templates
164
173
func walkTemplatesPath (path string , fileInfo os.FileInfo , err error ) error {
165
174
@@ -203,16 +212,6 @@ func walkTemplatesPath(path string, fileInfo os.FileInfo, err error) error {
203
212
func render (w http.ResponseWriter , r * http.Request , tmpl string , data interface {}) {
204
213
log .Printf ("INFO > controllers/controllers.go > render(): %s" , tmpl )
205
214
206
- // var tpl bytes.Buffer
207
- // e := templates[tmpl].Execute(&tpl, data)
208
- // if e != nil {
209
- // log.Println(tmpl)
210
-
211
- // }
212
- // log.Println(e)
213
- // log.Printf("\ntmpl: %v\n", templates[tmpl])
214
- // log.Printf("\nbytes: %v\n", tpl.String())
215
-
216
215
// Set the content type.
217
216
w .Header ().Set ("Content-Type" , "text/html" )
218
217
@@ -223,7 +222,6 @@ func render(w http.ResponseWriter, r *http.Request, tmpl string, data interface{
223
222
err := templates [tmpl ].Execute (w , data )
224
223
if err != nil {
225
224
log .Printf ("ERROR > controllers.go > render(): %v" , err )
226
- //fmt.Println(err)
227
225
}
228
226
}
229
227
@@ -233,27 +231,30 @@ func initRouter() *mux.Router {
233
231
router := mux .NewRouter ()
234
232
235
233
// System Routes
236
- router .HandleFunc ("/session/list" , sessionListHandler ).Methods ("GET" )
234
+ router .HandleFunc ("/audit/list100" , auditList100Handler ).Methods ("GET" )
235
+
236
+ router .HandleFunc ("/login" , loginHandler ).Methods ("GET" , "POST" )
237
+ router .HandleFunc ("/logout" , loginHandler ).Methods ("GET" )
238
+
239
+ router .HandleFunc ("/noauth" , noauthHandler ).Methods ("GET" )
237
240
238
241
router .HandleFunc ("/role/list" , roleListHandler ).Methods ("GET" )
239
242
router .HandleFunc ("/role/create" , roleCreateHandler ).Methods ("GET" , "POST" )
240
243
router .HandleFunc ("/role/read/{id}" , roleReadHandler ).Methods ("GET" )
241
244
router .HandleFunc ("/role/update/{id}" , roleUpdateHandler ).Methods ("GET" , "POST" )
242
- router .HandleFunc ("/role/delete/{id}" , roleDeleteHandler ).Methods ("GET " )
245
+ router .HandleFunc ("/role/delete/{id}" , roleDeleteHandler ).Methods ("POST " )
243
246
244
247
router .HandleFunc ("/route/list" , routeListHandler ).Methods ("GET" , "POST" )
245
248
249
+ router .HandleFunc ("/session/list" , sessionListHandler ).Methods ("GET" )
250
+
246
251
router .HandleFunc ("/user/list" , userListHandler ).Methods ("GET" )
252
+ router .HandleFunc ("/user/read/{id}" , userReadHandler ).Methods ("GET" )
247
253
router .HandleFunc ("/user/update/{id}" , userUpdateHandler ).Methods ("GET" , "POST" )
248
254
router .HandleFunc ("/user/delete/{id}" , userDeleteHandler ).Methods ("GET" )
249
255
250
- router .HandleFunc ("/login" , loginHandler ).Methods ("GET" , "POST" )
251
- router .HandleFunc ("/logout" , loginHandler ).Methods ("GET" )
252
-
253
- router .HandleFunc ("/noauth" , noauthHandler ).Methods ("GET" )
254
-
255
256
// App Routes
256
- router .HandleFunc ("/" , rootHandler ).Methods ("GET" , "POST" )
257
+ router .HandleFunc ("/" , homeHandler ).Methods ("GET" , "POST" )
257
258
router .HandleFunc ("/home" , homeHandler ).Methods ("GET" )
258
259
259
260
router .HandleFunc ("/server/list" , serverListHandler ).Methods ("GET" )
@@ -265,27 +266,6 @@ func initRouter() *mux.Router {
265
266
return router
266
267
}
267
268
268
- // func initPermissions() error {
269
- // // call api to get a slice of permissions
270
- // ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
271
- // defer cancel()
272
- // svc := api.NewPermissionServiceClient(apiClient)
273
- // req := new(api.PermissionSliceReq)
274
- // slice, err := svc.Slice(ctx, req)
275
- // if err != nil {
276
- // log.Printf("controllers/controllers.go > ERROR > svc.Slice(): %s\n", err.Error())
277
- // return err
278
- // }
279
-
280
- // permissions = make(map[string]string)
281
-
282
- // for _, permission := range slice.Permissions {
283
- // permissions[permission.RoleID] = permission.Route
284
- // }
285
-
286
- // return nil
287
- // }
288
-
289
269
// format timestamps
290
270
func timestampFM () template.FuncMap {
291
271
return template.FuncMap {
@@ -320,9 +300,6 @@ func permissionFM(r *http.Request) template.FuncMap {
320
300
return false
321
301
}
322
302
323
- //currentRoute := mux.CurrentRoute(r)
324
- //pathTemplate, _ := currentRoute.GetPathTemplate()
325
-
326
303
ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
327
304
defer cancel ()
328
305
@@ -350,8 +327,24 @@ func permissionFM(r *http.Request) template.FuncMap {
350
327
}
351
328
352
329
// addNotification adds a notification message to session.Values
353
- func addNotification (session * sessions.Session , notification string ) {
330
+ func addNotification (w http.ResponseWriter , r * http.Request , notification string ) {
331
+ // get session
332
+ session , err := store .Get (r , "session" )
333
+ if err != nil {
334
+ log .Printf ("ERROR > controllers/controllers.go > addNotification() > store.Get(): %s\n " , err .Error ())
335
+ http .Error (w , err .Error (), http .StatusInternalServerError )
336
+ return
337
+ }
338
+
354
339
session .Values ["notification" ] = notification
340
+
341
+ // save session
342
+ err = session .Save (r , w )
343
+ if err != nil {
344
+ log .Printf ("ERROR > controllers/controllers.go > addNotification() > session.Save(): %s\n " , err .Error ())
345
+ http .Error (w , err .Error (), http .StatusInternalServerError )
346
+ return
347
+ }
355
348
}
356
349
357
350
// getNotification returns a notification from session.Values if
0 commit comments