Commit 3f8e3c437433052f24b067ed433df8e83b98ec12
1 parent
34436d11e2
Exists in
master
and in
1 other branch
minor changes
Showing
3 changed files
with
197 additions
and
134 deletions
Show diff stats
auth.go
... | ... | @@ -135,39 +135,35 @@ func RefreshAuthToken(tok string) (TokenClaims, error) { |
135 | 135 | return CreateAuthToken(claims.Username, Role{claims.Role, claims.RoleID}) |
136 | 136 | } |
137 | 137 | |
138 | -// AuthCheck returns JWT claims and boolean result of a check if req contains any role from roles. | |
139 | -// It checks if role extracted from reqest's Authorization header (JWT claims) matches any of | |
140 | -// provided comma-separated roles in roles. If roles is empty string check is skipped, | |
141 | -// otherwise role is extracted from token claims and compared against roles. | |
142 | -// If roles is "*" the check is automatically validated. | |
143 | -func AuthCheck(req *http.Request, roles string) (*TokenClaims, bool) { | |
144 | - if roles == "" { | |
145 | - return nil, true | |
146 | - } | |
147 | - | |
138 | +func AuthCheck(req *http.Request, roles string) (*TokenClaims, error) { | |
148 | 139 | // validate token and check expiration date |
149 | 140 | claims, err := GetTokenClaims(req) |
150 | 141 | if err != nil { |
151 | - return claims, false | |
142 | + return claims, err | |
143 | + } | |
144 | + | |
145 | + if roles == "" { | |
146 | + return claims, nil | |
152 | 147 | } |
148 | + | |
153 | 149 | // check if token has expired |
154 | 150 | if claims.ExpiresAt < (time.Now()).Unix() { |
155 | - return claims, false | |
151 | + return claims, errors.New("token has expired") | |
156 | 152 | } |
157 | 153 | |
158 | 154 | if roles == "*" { |
159 | - return claims, true | |
155 | + return claims, nil | |
160 | 156 | } |
161 | 157 | |
162 | 158 | parts := strings.Split(roles, ",") |
163 | 159 | for i, _ := range parts { |
164 | 160 | r := strings.Trim(parts[i], " ") |
165 | 161 | if claims.Role == r { |
166 | - return claims, true | |
162 | + return claims, nil | |
167 | 163 | } |
168 | 164 | } |
169 | 165 | |
170 | - return claims, false | |
166 | + return claims, nil | |
171 | 167 | } |
172 | 168 | |
173 | 169 | // GetTokenClaims extracts JWT claims from Authorization header of req. | ... | ... |
list_config.go
... | ... | @@ -49,6 +49,8 @@ type ListActions struct { |
49 | 49 | Print bool `json:"print"` |
50 | 50 | Graph bool `json:"graph"` |
51 | 51 | LiveGraph bool `json:"liveGraph"` |
52 | + SaveFile bool `json:"saveFile"` | |
53 | + ShowFile bool `json:"showFile"` | |
52 | 54 | } |
53 | 55 | |
54 | 56 | type ListNavNode struct { |
... | ... | @@ -105,25 +107,24 @@ type ListConfig struct { |
105 | 107 | // GetListConfig returns list configuration for the provided object type for the front-end application |
106 | 108 | // or an error if it fails. |
107 | 109 | func GetListConfig(db *sql.DB, objType string) (ListConfig, error) { |
108 | - resp := newDefaultList(objType) | |
109 | - var err error | |
110 | - | |
111 | - err = setListParams(db, &resp, objType) | |
112 | - resp.Navigation, err = getListNavigation(db, objType) | |
113 | - resp.Actions, err = getListActions(db, objType) | |
114 | - resp.Filters, err = getListFilters(db, objType) | |
115 | - resp.Options, err = getListOptions(db, objType) | |
116 | - resp.Parent, err = getListParent(db, objType) | |
117 | - resp.Graphs, err = getListGraph(db, objType) | |
118 | - resp.Pivots, err = getListPivot(db, objType) | |
119 | - resp.Details, err = getListDetails(db, objType) | |
120 | - resp.LiveGraph, err = getListLiveGraph(db, objType) | |
110 | + list := NewListConfig(objType) | |
111 | + | |
112 | + err := list.setParams(db, objType) | |
113 | + err = list.SetNavigation(db, objType) | |
114 | + err = list.SetActions(db, objType) | |
115 | + err = list.SetFilters(db, objType) | |
116 | + err = list.SetOptions(db, objType) | |
117 | + err = list.SetParent(db, objType) | |
118 | + err = list.SetPivot(db, objType) | |
119 | + err = list.SetGraph(db, objType) | |
120 | + err = list.SetDetails(db, objType) | |
121 | + err = list.SetLiveGraph(db, objType) | |
121 | 122 | |
122 | 123 | if err != nil { |
123 | - return ListConfig{}, err | |
124 | + return list, err | |
124 | 125 | } |
125 | 126 | |
126 | - return resp, nil | |
127 | + return list, nil | |
127 | 128 | } |
128 | 129 | |
129 | 130 | // GetListConfigObjectIDField takes in database connection and an object type and it returns the |
... | ... | @@ -152,7 +153,7 @@ func GetListConfigObjectIDField(db *sql.DB, otype string) string { |
152 | 153 | } |
153 | 154 | |
154 | 155 | // newDefaultList returns default configuration for the provided object type. |
155 | -func newDefaultList(objType string) ListConfig { | |
156 | +func NewListConfig(objType string) ListConfig { | |
156 | 157 | list := ListConfig{ |
157 | 158 | ObjectType: objType, |
158 | 159 | Title: objType, |
... | ... | @@ -182,9 +183,12 @@ func newDefaultList(objType string) ListConfig { |
182 | 183 | } |
183 | 184 | |
184 | 185 | // setListParams sets the default parameters of the provided configuration list for the provided object type. |
185 | -func setListParams(db *sql.DB, list *ListConfig, objType string) error { | |
186 | +func (list *ListConfig) setParams(db *sql.DB, objType string) error { | |
186 | 187 | rows, err := db.Query(`SELECT |
187 | - OBJECT_TYPE, TITLE, LAZY_LOAD, INLINE_EDIT | |
188 | + OBJECT_TYPE, | |
189 | + TITLE, | |
190 | + LAZY_LOAD, | |
191 | + INLINE_EDIT | |
188 | 192 | FROM LIST_CONFIG |
189 | 193 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
190 | 194 | if err != nil { |
... | ... | @@ -211,17 +215,22 @@ func setListParams(db *sql.DB, list *ListConfig, objType string) error { |
211 | 215 | return nil |
212 | 216 | } |
213 | 217 | |
214 | -// getListNavigation returns list navigation node slice for the provided objectType. | |
215 | -func getListNavigation(db *sql.DB, listObjType string) ([]ListNavNode, error) { | |
216 | - resp := make([]ListNavNode, 0) | |
218 | +// ListNavigation returns list navigation node slice for the provided objectType. | |
219 | +func (list *ListConfig) SetNavigation(db *sql.DB, listObjType string) error { | |
220 | + list.Navigation = make([]ListNavNode, 0) | |
217 | 221 | rows, err := db.Query(`SELECT |
218 | - a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD | |
222 | + a.OBJECT_TYPE, | |
223 | + a.PARENT_OBJECT_TYPE, | |
224 | + a.LABEL, | |
225 | + a.ICON, | |
226 | + a.PARENT_FILTER_FIELD, | |
227 | + b.PARENT_ID_FIELD | |
219 | 228 | FROM LIST_CONFIG_NAVIGATION b |
220 | 229 | JOIN LIST_CONFIG_CHILD a ON b.PARENT_CHILD_ID = a.PARENT_CHILD_ID |
221 | 230 | WHERE b.LIST_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", listObjType) + |
222 | 231 | ` ORDER BY b.RB ASC`) |
223 | 232 | if err != nil { |
224 | - return resp, err | |
233 | + return err | |
225 | 234 | } |
226 | 235 | defer rows.Close() |
227 | 236 | |
... | ... | @@ -229,54 +238,63 @@ func getListNavigation(db *sql.DB, listObjType string) ([]ListNavNode, error) { |
229 | 238 | for rows.Next() { |
230 | 239 | rows.Scan(&node.ObjectType, &node.ParentObjectType, &node.LabelField, &node.Icon, |
231 | 240 | &node.ParentFilterField, &node.ParentIDField) |
232 | - resp = append(resp, node) | |
241 | + list.Navigation = append(list.Navigation, node) | |
233 | 242 | } |
234 | 243 | if rows.Err() != nil { |
235 | - return nil, rows.Err() | |
244 | + return rows.Err() | |
236 | 245 | } |
237 | 246 | |
238 | - return resp, nil | |
247 | + return nil | |
239 | 248 | } |
240 | 249 | |
241 | 250 | // getListActions returns list actions for the provided object type. |
242 | -func getListActions(db *sql.DB, objType string) (ListActions, error) { | |
243 | - var resp ListActions | |
251 | +func (list *ListConfig) SetActions(db *sql.DB, objType string) error { | |
244 | 252 | rows, err := db.Query(`SELECT |
245 | - ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT, | |
246 | - ACTION_PRINT, ACTION_GRAPH, ACTION_LIVE_GRAPH | |
253 | + ACTION_CREATE, | |
254 | + ACTION_UPDATE, | |
255 | + ACTION_DELETE, | |
256 | + ACTION_EXPORT, | |
257 | + ACTION_PRINT, | |
258 | + ACTION_GRAPH, | |
259 | + ACTION_LIVE_GRAPH, | |
260 | + ACTION_SAVE_FILE, | |
261 | + ACTION_SHOW_FILE | |
247 | 262 | FROM LIST_CONFIG |
248 | 263 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
249 | 264 | if err != nil { |
250 | - return ListActions{}, err | |
265 | + return err | |
251 | 266 | } |
252 | 267 | defer rows.Close() |
253 | 268 | |
254 | - var create, update, delete, export, print, graph, liveGraph uint32 | |
269 | + var create, update, delete, export, print, graph, liveGraph, saveFile, showFile uint32 | |
255 | 270 | if rows.Next() { |
256 | - rows.Scan(&create, &update, &delete, &export, &print, &graph, &liveGraph) | |
257 | - resp.Create = create != 0 | |
258 | - resp.Update = update != 0 | |
259 | - resp.Delete = delete != 0 | |
260 | - resp.Export = export != 0 | |
261 | - resp.Print = print != 0 | |
262 | - resp.Graph = graph != 0 | |
263 | - resp.LiveGraph = liveGraph != 0 | |
271 | + rows.Scan(&create, &update, &delete, &export, &print, &graph, &liveGraph, &saveFile, &showFile) | |
272 | + list.Actions.Create = create != 0 | |
273 | + list.Actions.Update = update != 0 | |
274 | + list.Actions.Delete = delete != 0 | |
275 | + list.Actions.Export = export != 0 | |
276 | + list.Actions.Print = print != 0 | |
277 | + list.Actions.Graph = graph != 0 | |
278 | + list.Actions.LiveGraph = liveGraph != 0 | |
279 | + list.Actions.SaveFile = saveFile != 0 | |
280 | + list.Actions.ShowFile = showFile != 0 | |
264 | 281 | } |
265 | 282 | if rows.Err() != nil { |
266 | - return ListActions{}, rows.Err() | |
283 | + return rows.Err() | |
267 | 284 | } |
268 | - return resp, nil | |
285 | + | |
286 | + return nil | |
269 | 287 | } |
270 | 288 | |
271 | 289 | // getListFiters returns list filter slice for the provided object type. |
272 | -func getListFilters(db *sql.DB, objType string) ([]ListFilter, error) { | |
273 | - resp := make([]ListFilter, 0) | |
274 | - filtersFields, err := getFilterFieldsAndPosition(db, objType) | |
290 | +func (list *ListConfig) SetFilters(db *sql.DB, objType string) error { | |
291 | + list.Filters = make([]ListFilter, 0) | |
292 | + filtersFields, err := list.GetFilterFieldsAndPosition(db, objType) | |
275 | 293 | if err != nil { |
276 | - return nil, err | |
294 | + return err | |
277 | 295 | } |
278 | 296 | for field, pos := range filtersFields { |
279 | - filters, _ := getFiltersByFilterField(db, field) | |
297 | + filters, _ := list.GetFiltersByFilterField(db, field) | |
280 | 298 | for _, filter := range filters { |
281 | 299 | var f ListFilter |
282 | 300 | f.Position = pos |
... | ... | @@ -286,25 +304,26 @@ func getListFilters(db *sql.DB, objType string) ([]ListFilter, error) { |
286 | 304 | f.FiltersLabel = filter.Label |
287 | 305 | f.FiltersType = filter.Type |
288 | 306 | if filter.Type == "dropdown" { |
289 | - f.DropdownConfig, err = getFilterDropdownConfig(db, field) | |
307 | + err := f.SetDropdownConfig(db, field) | |
290 | 308 | if err != nil { |
291 | - return nil, err | |
309 | + return err | |
292 | 310 | } |
293 | 311 | } |
294 | - resp = append(resp, f) | |
312 | + list.Filters = append(list.Filters, f) | |
295 | 313 | } |
296 | 314 | } |
297 | 315 | |
298 | - sortFilters(resp) | |
316 | + list.sortFilters() | |
299 | 317 | |
300 | - return resp, nil | |
318 | + return nil | |
301 | 319 | } |
302 | 320 | |
303 | 321 | // getFilterFieldsAndPosition returns a map of filter fields and their respective position in the menu. |
304 | -func getFilterFieldsAndPosition(db *sql.DB, objType string) (map[string]uint32, error) { | |
322 | +func (list *ListConfig) GetFilterFieldsAndPosition(db *sql.DB, objType string) (map[string]uint32, error) { | |
305 | 323 | filtersField := make(map[string]uint32, 0) |
306 | 324 | rows, err := db.Query(`SELECT |
307 | - FILTERS_FIELD, RB | |
325 | + FILTERS_FIELD, | |
326 | + RB | |
308 | 327 | FROM LIST_CONFIG_FILTERS |
309 | 328 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
310 | 329 | if err != nil { |
... | ... | @@ -331,10 +350,12 @@ type _filter struct { |
331 | 350 | } |
332 | 351 | |
333 | 352 | // getFiltersByFilterField returns filter slice for the provided filter field. |
334 | -func getFiltersByFilterField(db *sql.DB, filtersField string) ([]_filter, error) { | |
353 | +func (list *ListConfig) GetFiltersByFilterField(db *sql.DB, filtersField string) ([]_filter, error) { | |
335 | 354 | resp := make([]_filter, 0) |
336 | 355 | rows, err := db.Query(`SELECT |
337 | - FILTERS_TYPE, FILTERS_LABEL, DEFAULT_VALUES | |
356 | + FILTERS_TYPE, | |
357 | + FILTERS_LABEL, | |
358 | + DEFAULT_VALUES | |
338 | 359 | FROM LIST_FILTERS_FIELD |
339 | 360 | WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField)) |
340 | 361 | if err != nil { |
... | ... | @@ -354,152 +375,178 @@ func getFiltersByFilterField(db *sql.DB, filtersField string) ([]_filter, error) |
354 | 375 | } |
355 | 376 | |
356 | 377 | // getFilterDropdownConfig returns dropdown menu for the provided filter field. |
357 | -func getFilterDropdownConfig(db *sql.DB, filtersField string) (Dropdown, error) { | |
378 | +func (f *ListFilter) SetDropdownConfig(db *sql.DB, filtersField string) error { | |
358 | 379 | var resp Dropdown |
359 | 380 | rows, err := db.Query(`SELECT |
360 | - FILTERS_FIELD, OBJECT_TYPE, ID_FIELD, LABEL_FIELD | |
381 | + FILTERS_FIELD, | |
382 | + OBJECT_TYPE, | |
383 | + ID_FIELD, | |
384 | + LABEL_FIELD | |
361 | 385 | FROM LIST_DROPDOWN_FILTER |
362 | 386 | WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField)) |
363 | 387 | if err != nil { |
364 | - return resp, err | |
388 | + return err | |
365 | 389 | } |
366 | 390 | defer rows.Close() |
367 | 391 | if rows.Next() { |
368 | 392 | rows.Scan(&resp.FiltersField, &resp.ObjectType, &resp.IDField, &resp.LabelField) |
369 | 393 | } |
370 | 394 | if rows.Err() != nil { |
371 | - return resp, rows.Err() | |
395 | + return rows.Err() | |
372 | 396 | } |
373 | - return resp, nil | |
397 | + | |
398 | + f.DropdownConfig = resp | |
399 | + | |
400 | + return nil | |
374 | 401 | } |
375 | 402 | |
376 | 403 | // sortFilters bubble sorts provided filters slice by position field. |
377 | -func sortFilters(filters []ListFilter) { | |
404 | +func (list *ListConfig) sortFilters() { | |
378 | 405 | done := false |
379 | 406 | var temp ListFilter |
380 | 407 | for !done { |
381 | 408 | done = true |
382 | - for i := 0; i < len(filters)-1; i++ { | |
383 | - if filters[i].Position > filters[i+1].Position { | |
409 | + for i := 0; i < len(list.Filters)-1; i++ { | |
410 | + if list.Filters[i].Position > list.Filters[i+1].Position { | |
384 | 411 | done = false |
385 | - temp = filters[i] | |
386 | - filters[i] = filters[i+1] | |
387 | - filters[i+1] = temp | |
412 | + temp = list.Filters[i] | |
413 | + list.Filters[i] = list.Filters[i+1] | |
414 | + list.Filters[i+1] = temp | |
388 | 415 | } |
389 | 416 | } |
390 | 417 | } |
391 | 418 | } |
392 | 419 | |
393 | 420 | // getListGraph return list graph slice for the provided object type. |
394 | -func getListGraph(db *sql.DB, objType string) ([]ListGraph, error) { | |
395 | - resp := make([]ListGraph, 0) | |
421 | +func (list *ListConfig) SetGraph(db *sql.DB, objType string) error { | |
422 | + list.Graphs = make([]ListGraph, 0) | |
396 | 423 | rows, err := db.Query(`SELECT |
397 | - OBJECT_TYPE, X_FIELD, Y_FIELD, GROUP_FIELD, LABEL | |
424 | + OBJECT_TYPE, | |
425 | + X_FIELD, | |
426 | + Y_FIELD, | |
427 | + GROUP_FIELD, | |
428 | + LABEL | |
398 | 429 | FROM LIST_GRAPHS |
399 | 430 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
400 | 431 | if err != nil { |
401 | - return resp, err | |
432 | + return err | |
402 | 433 | } |
403 | 434 | defer rows.Close() |
404 | 435 | |
405 | 436 | var lg ListGraph |
406 | 437 | for rows.Next() { |
407 | 438 | rows.Scan(&lg.ObjectType, &lg.X, &lg.Y, &lg.GroupField, &lg.Label) |
408 | - resp = append(resp, lg) | |
439 | + list.Graphs = append(list.Graphs, lg) | |
409 | 440 | } |
410 | 441 | if rows.Err() != nil { |
411 | - return resp, rows.Err() | |
442 | + return rows.Err() | |
412 | 443 | } |
413 | - return resp, nil | |
444 | + | |
445 | + return nil | |
414 | 446 | } |
415 | 447 | |
416 | 448 | // getListOptions returns list options for the provided object type. |
417 | -func getListOptions(db *sql.DB, objType string) (ListOptions, error) { | |
418 | - var resp ListOptions | |
449 | +func (list *ListConfig) SetOptions(db *sql.DB, objType string) error { | |
419 | 450 | rows, err := db.Query(`SELECT |
420 | - GLOBAL_FILTER, LOCAL_FILTER, REMOTE_FILTER, PAGINATION, | |
421 | - PAGE_SIZE, PIVOT, DETAIL, TOTAL | |
451 | + GLOBAL_FILTER, | |
452 | + LOCAL_FILTER, | |
453 | + REMOTE_FILTER, | |
454 | + PAGINATION, | |
455 | + PAGE_SIZE, | |
456 | + PIVOT, | |
457 | + DETAIL, | |
458 | + TOTAL | |
422 | 459 | FROM LIST_CONFIG |
423 | 460 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
424 | 461 | if err != nil { |
425 | - return ListOptions{}, err | |
462 | + return err | |
426 | 463 | } |
427 | 464 | defer rows.Close() |
465 | + | |
428 | 466 | if rows.Next() { |
429 | 467 | var gfilter, lfilters, rfilters, pagination, pageSize, pivot, detail, total uint32 |
430 | 468 | rows.Scan(&gfilter, &lfilters, &rfilters, &pagination, &pageSize, &pivot, &detail, &total) |
431 | - resp.GlobalFilter = gfilter != 0 | |
432 | - resp.LocalFilters = lfilters != 0 | |
433 | - resp.RemoteFilters = rfilters != 0 | |
434 | - resp.Pagination = pagination != 0 | |
435 | - resp.PageSize = pageSize | |
436 | - resp.Pivot = pivot != 0 | |
437 | - resp.Detail = detail != 0 | |
438 | - resp.Total = total != 0 | |
469 | + list.Options.GlobalFilter = gfilter != 0 | |
470 | + list.Options.LocalFilters = lfilters != 0 | |
471 | + list.Options.RemoteFilters = rfilters != 0 | |
472 | + list.Options.Pagination = pagination != 0 | |
473 | + list.Options.PageSize = pageSize | |
474 | + list.Options.Pivot = pivot != 0 | |
475 | + list.Options.Detail = detail != 0 | |
476 | + list.Options.Total = total != 0 | |
439 | 477 | } |
440 | 478 | if rows.Err() != nil { |
441 | - return ListOptions{}, rows.Err() | |
479 | + return rows.Err() | |
442 | 480 | } |
443 | - return resp, nil | |
481 | + | |
482 | + return nil | |
444 | 483 | } |
445 | 484 | |
446 | 485 | // getListParent returns list parent node slice for the provided object type. |
447 | -func getListParent(db *sql.DB, objType string) ([]ListParentNode, error) { | |
448 | - resp := make([]ListParentNode, 0) | |
486 | +func (list *ListConfig) SetParent(db *sql.DB, objType string) error { | |
487 | + list.Parent = make([]ListParentNode, 0) | |
449 | 488 | rows, err := db.Query(`SELECT |
450 | - PARENT_OBJECT_TYPE, PARENT_LABEL_FIELD, PARENT_FILTER_FIELD | |
489 | + PARENT_OBJECT_TYPE, | |
490 | + PARENT_LABEL_FIELD, | |
491 | + PARENT_FILTER_FIELD | |
451 | 492 | FROM LIST_CONFIG_CHILD |
452 | 493 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
453 | 494 | if err != nil { |
454 | - return resp, err | |
495 | + return err | |
455 | 496 | } |
456 | 497 | defer rows.Close() |
457 | 498 | |
458 | 499 | var pnode ListParentNode |
459 | 500 | for rows.Next() { |
460 | 501 | rows.Scan(&pnode.ObjectType, &pnode.LabelField, &pnode.FilterField) |
461 | - resp = append(resp, pnode) | |
502 | + list.Parent = append(list.Parent, pnode) | |
462 | 503 | } |
463 | 504 | if rows.Err() != nil { |
464 | - return nil, rows.Err() | |
505 | + return rows.Err() | |
465 | 506 | } |
466 | 507 | |
467 | - return resp, nil | |
508 | + return nil | |
468 | 509 | } |
469 | 510 | |
470 | 511 | // getListPivot list pivot slice for the provided object type. |
471 | -func getListPivot(db *sql.DB, objType string) ([]ListPivot, error) { | |
472 | - resp := make([]ListPivot, 0) | |
512 | +func (list *ListConfig) SetPivot(db *sql.DB, objType string) error { | |
513 | + list.Pivots = make([]ListPivot, 0) | |
473 | 514 | rows, err := db.Query(`SELECT |
474 | - OBJECT_TYPE, GROUP_FIELD, DISTINCT_FIELD, VALUE_FIELD | |
515 | + OBJECT_TYPE, | |
516 | + GROUP_FIELD, | |
517 | + DISTINCT_FIELD, | |
518 | + VALUE_FIELD | |
475 | 519 | FROM LIST_PIVOTS |
476 | 520 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
477 | 521 | if err != nil { |
478 | - return resp, err | |
522 | + return err | |
479 | 523 | } |
480 | 524 | defer rows.Close() |
481 | 525 | |
482 | 526 | var p ListPivot |
483 | 527 | for rows.Next() { |
484 | 528 | rows.Scan(&p.ObjectType, &p.GroupField, &p.DistinctField, &p.Value) |
485 | - resp = append(resp, p) | |
529 | + list.Pivots = append(list.Pivots, p) | |
486 | 530 | } |
487 | 531 | if rows.Err() != nil { |
488 | - return nil, rows.Err() | |
532 | + return rows.Err() | |
489 | 533 | } |
490 | 534 | |
491 | - return resp, nil | |
535 | + return nil | |
492 | 536 | } |
493 | 537 | |
494 | 538 | // getListDetails returns list details for the provided object type. |
495 | -func getListDetails(db *sql.DB, objType string) (ListDetails, error) { | |
539 | +func (list *ListConfig) SetDetails(db *sql.DB, objType string) error { | |
496 | 540 | var resp ListDetails |
497 | 541 | rows, err := db.Query(`SELECT |
498 | - OBJECT_TYPE, PARENT_OBJECT_TYPE, PARENT_FILTER_FIELD, SINGLE_DETAIL | |
542 | + OBJECT_TYPE, | |
543 | + PARENT_OBJECT_TYPE, | |
544 | + PARENT_FILTER_FIELD, | |
545 | + SINGLE_DETAIL | |
499 | 546 | FROM LIST_CONFIG_DETAIL |
500 | 547 | WHERE PARENT_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
501 | 548 | if err != nil { |
502 | - return resp, err | |
549 | + return err | |
503 | 550 | } |
504 | 551 | defer rows.Close() |
505 | 552 | if rows.Next() { |
... | ... | @@ -508,29 +555,35 @@ func getListDetails(db *sql.DB, objType string) (ListDetails, error) { |
508 | 555 | resp.SingleDetail = singleDetail != 0 |
509 | 556 | } |
510 | 557 | if rows.Err() != nil { |
511 | - return resp, rows.Err() | |
558 | + return rows.Err() | |
512 | 559 | } |
513 | 560 | |
514 | - return resp, nil | |
561 | + list.Details = resp | |
562 | + | |
563 | + return nil | |
515 | 564 | } |
516 | 565 | |
517 | 566 | // getListLiveGraph returns live graph for the provided object type. |
518 | -func getListLiveGraph(db *sql.DB, objType string) (ListLiveGraph, error) { | |
567 | +func (list *ListConfig) SetLiveGraph(db *sql.DB, objType string) error { | |
519 | 568 | var resp ListLiveGraph |
520 | 569 | rows, err := db.Query(`SELECT |
521 | - OBJECT_TYPE, VALUE_FIELDS, LABEL_FIELDS | |
570 | + OBJECT_TYPE, | |
571 | + VALUE_FIELDS, | |
572 | + LABEL_FIELDS | |
522 | 573 | FROM LIST_LIVE_GRAPH |
523 | 574 | WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
524 | 575 | if err != nil { |
525 | - return resp, err | |
576 | + return err | |
526 | 577 | } |
527 | 578 | defer rows.Close() |
528 | 579 | if rows.Next() { |
529 | 580 | rows.Scan(&resp.ObjectType, &resp.ValueFields, &resp.LabelFields) |
530 | 581 | } |
531 | 582 | if rows.Err() != nil { |
532 | - return resp, rows.Err() | |
583 | + return rows.Err() | |
533 | 584 | } |
534 | 585 | |
535 | - return resp, nil | |
586 | + list.LiveGraph = resp | |
587 | + | |
588 | + return nil | |
536 | 589 | } | ... | ... |
middleware.go
... | ... | @@ -28,6 +28,17 @@ func ParseForm(h http.HandlerFunc) http.HandlerFunc { |
28 | 28 | } |
29 | 29 | } |
30 | 30 | |
31 | +func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { | |
32 | + return func(w http.ResponseWriter, req *http.Request) { | |
33 | + err := req.ParseMultipartForm(32 << 20) | |
34 | + if err != nil { | |
35 | + BadRequest(w, req, err.Error()) | |
36 | + return | |
37 | + } | |
38 | + h(w, req) | |
39 | + } | |
40 | +} | |
41 | + | |
31 | 42 | var trafficLogger *gologger.Logger |
32 | 43 | |
33 | 44 | func EnableLogging(log string) error { |
... | ... | @@ -38,22 +49,25 @@ func EnableLogging(log string) error { |
38 | 49 | |
39 | 50 | func Log(h http.HandlerFunc) http.HandlerFunc { |
40 | 51 | return func(w http.ResponseWriter, req *http.Request) { |
52 | + t1 := time.Now() | |
53 | + | |
41 | 54 | in := trafficLogger.RequestLog(req, "") |
42 | 55 | |
43 | - w2 := WrapWithStatusRecorder(w) | |
44 | - t1 := time.Now() | |
45 | - h(w2, req) | |
56 | + wRec := WrapWithStatusRecorder(w) | |
57 | + h(wRec, req) | |
58 | + | |
46 | 59 | t2 := time.Now() |
47 | 60 | |
48 | - out := trafficLogger.ResponseLog(w2.Status(), t2.Sub(t1)) | |
61 | + out := trafficLogger.ResponseLog(wRec.Status(), t2.Sub(t1), 0) | |
62 | + | |
49 | 63 | trafficLogger.LogHTTPTraffic(in, out) |
50 | 64 | } |
51 | 65 | } |
52 | 66 | |
53 | 67 | func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { |
54 | 68 | return func(w http.ResponseWriter, req *http.Request) { |
55 | - if _, ok := AuthCheck(req, roles); !ok { | |
56 | - Unauthorized(w, req, "") | |
69 | + if _, err := AuthCheck(req, roles); err != nil { | |
70 | + Unauthorized(w, req, err.Error()) | |
57 | 71 | return |
58 | 72 | } |
59 | 73 | h(w, req) | ... | ... |