-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.go
92 lines (75 loc) · 2.46 KB
/
admin.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
package frontend
import (
"net/http"
"github.com/plifk/market/internal/services"
)
// AdminHandler for the application.
type AdminHandler struct {
Frontend *Frontend
dashboardHandler *AdminDashboardHandler
usersHandler *AdminUsersHandler
securityHandler *AdminSecurityHandler
reportsHandler *AdminReportsHandler
rolesHandler *AdminRolesHandler
}
// Load /admin routes.
func (h *AdminHandler) Load() {
h.dashboardHandler = &AdminDashboardHandler{Frontend: h.Frontend}
h.usersHandler = &AdminUsersHandler{Frontend: h.Frontend}
h.securityHandler = &AdminSecurityHandler{Frontend: h.Frontend}
h.reportsHandler = &AdminReportsHandler{Frontend: h.Frontend}
h.rolesHandler = &AdminRolesHandler{Frontend: h.Frontend}
}
func (h *AdminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if user := services.UserFromRequest(r); user.Access != services.AdminAuthorization {
h.Frontend.HTTPError(w, r, http.StatusNotFound)
return
}
var handler http.Handler
switch route := dirRouter(r.URL.Path); {
case route.is("/admin"):
handler = h.dashboardHandler
case route.is("/admin/users"):
handler = h.usersHandler
case route.is("/admin/security"):
handler = h.securityHandler
case route.is("/admin/reports"):
handler = h.reportsHandler
case route.is("/admin/roles"):
handler = h.rolesHandler
}
if handler == nil {
handler = h.Frontend.staticHandler
}
handler.ServeHTTP(w, r)
}
// AdminDashboardHandler for the application.
type AdminDashboardHandler struct {
Frontend *Frontend
}
// DashboardHandler for /admin.
func (h *AdminDashboardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
// AdminUsersHandler for the application.
type AdminUsersHandler struct {
Frontend *Frontend
}
// UsersHandler for /admin/users.
func (h *AdminUsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
// AdminSecurityHandler for the application.
type AdminSecurityHandler struct {
Frontend *Frontend
}
// UsersHandler for /admin/security.
func (h *AdminSecurityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
// AdminReportsHandler for the application.
type AdminReportsHandler struct {
Frontend *Frontend
}
// UsersHandler for /admin/reports.
func (h *AdminReportsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
// AdminRolesHandler for the application.
type AdminRolesHandler struct {
Frontend *Frontend
}
// UsersHandler for /admin/roles.
func (h *AdminRolesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}