ServeHTTP renders the list of current routes.
(w http.ResponseWriter, req *http.Request)
| 330 | |
| 331 | // ServeHTTP renders the list of current routes. |
| 332 | func (r *Routing) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 333 | if req.Method != "GET" && req.Method != "HEAD" { |
| 334 | w.WriteHeader(http.StatusMethodNotAllowed) |
| 335 | return |
| 336 | } |
| 337 | |
| 338 | rt := r.routeTable.Load().(*routeTable) |
| 339 | req.ParseForm() |
| 340 | createdUnix := strconv.FormatInt(rt.created.Unix(), 10) |
| 341 | ts := req.Form.Get("timestamp") |
| 342 | if ts != "" && createdUnix != ts { |
| 343 | http.Error(w, "invalid timestamp", http.StatusBadRequest) |
| 344 | return |
| 345 | } |
| 346 | |
| 347 | showInvalid := req.Form.Get("invalid") == "true" |
| 348 | |
| 349 | routes := rt.validRoutes |
| 350 | if showInvalid { |
| 351 | routes = rt.invalidRoutes |
| 352 | } |
| 353 | |
| 354 | if req.Method == "HEAD" { |
| 355 | w.Header().Set(routesTimestampName, createdUnix) |
| 356 | w.Header().Set(RoutesCountName, strconv.Itoa(len(routes))) |
| 357 | |
| 358 | if strings.Contains(req.Header.Get("Accept"), "application/json") { |
| 359 | w.Header().Set("Content-Type", "application/json") |
| 360 | } else { |
| 361 | w.Header().Set("Content-Type", "text/plain") |
| 362 | } |
| 363 | |
| 364 | return |
| 365 | } |
| 366 | |
| 367 | offset, err := extractParam(req, "offset", 0) |
| 368 | if err != nil { |
| 369 | http.Error(w, "invalid offset", http.StatusBadRequest) |
| 370 | return |
| 371 | } |
| 372 | |
| 373 | limit, err := extractParam(req, "limit", defaultRouteListingLimit) |
| 374 | if err != nil { |
| 375 | http.Error(w, "invalid limit", http.StatusBadRequest) |
| 376 | return |
| 377 | } |
| 378 | |
| 379 | w.Header().Set(routesTimestampName, createdUnix) |
| 380 | w.Header().Set(RoutesCountName, strconv.Itoa(len(routes))) |
| 381 | |
| 382 | paged := slice(routes, offset, limit) |
| 383 | if strings.Contains(req.Header.Get("Accept"), "application/json") { |
| 384 | w.Header().Set("Content-Type", "application/json") |
| 385 | if showInvalid { |
| 386 | resp := make([]InvalidRouteResponse, len(paged)) |
| 387 | for i, route := range paged { |
| 388 | resp[i] = InvalidRouteResponse{Route: route, Error: rt.invalidRouteErrors[route.Id]} |
| 389 | } |