A completely separate router for administrator routes
()
| 217 | |
| 218 | // A completely separate router for administrator routes |
| 219 | func adminRouter() chi.Router { |
| 220 | r := chi.NewRouter() |
| 221 | r.Use(AdminOnly) |
| 222 | r.Get("/", func(w http.ResponseWriter, r *http.Request) { |
| 223 | w.Write([]byte("admin: index")) |
| 224 | }) |
| 225 | r.Get("/accounts", func(w http.ResponseWriter, r *http.Request) { |
| 226 | w.Write([]byte("admin: list accounts..")) |
| 227 | }) |
| 228 | r.Get("/users/{userId}", func(w http.ResponseWriter, r *http.Request) { |
| 229 | w.Write([]byte(fmt.Sprintf("admin: view user id %v", chi.URLParam(r, "userId")))) |
| 230 | }) |
| 231 | return r |
| 232 | } |
| 233 | |
| 234 | // AdminOnly middleware restricts access to just administrators. |
| 235 | func AdminOnly(next http.Handler) http.Handler { |