HandleGetServiceRoutingRequest handles request to get all service routing rules
()
| 394 | |
| 395 | // HandleGetServiceRoutingRequest handles request to get all service routing rules |
| 396 | func (s *Server) HandleGetServiceRoutingRequest() http.HandlerFunc { |
| 397 | |
| 398 | return func(w http.ResponseWriter, r *http.Request) { |
| 399 | |
| 400 | defer utils.CloseTheCloser(r.Body) |
| 401 | |
| 402 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 403 | defer cancel() |
| 404 | |
| 405 | // Verify token |
| 406 | _, err := s.auth.VerifyToken(utils.GetToken(r)) |
| 407 | if err != nil { |
| 408 | _ = helpers.Logger.LogError(helpers.GetRequestID(ctx), "Failed to get service routes", err, nil) |
| 409 | _ = helpers.Response.SendErrorResponse(ctx, w, http.StatusUnauthorized, err) |
| 410 | return |
| 411 | } |
| 412 | |
| 413 | vars := mux.Vars(r) |
| 414 | projectID := vars["project"] |
| 415 | serviceID, exists := r.URL.Query()["id"] |
| 416 | |
| 417 | serviceRoutes, err := s.driver.GetServiceRoutes(ctx, projectID) |
| 418 | if err != nil { |
| 419 | _ = helpers.Logger.LogError(helpers.GetRequestID(ctx), "Failed to get service routing rules", err, nil) |
| 420 | _ = helpers.Response.SendErrorResponse(ctx, w, http.StatusInternalServerError, err) |
| 421 | return |
| 422 | } |
| 423 | |
| 424 | if exists { |
| 425 | for k, result := range serviceRoutes { |
| 426 | if k == serviceID[0] { |
| 427 | w.Header().Set("Content-Type", "application/json") |
| 428 | w.WriteHeader(http.StatusOK) |
| 429 | _ = json.NewEncoder(w).Encode(model.Response{Result: result}) |
| 430 | return |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | w.Header().Set("Content-Type", "application/json") |
| 435 | w.WriteHeader(http.StatusInternalServerError) |
| 436 | _ = json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("serviceID(%s) not present in state", serviceID[0])}) |
| 437 | return |
| 438 | } |
| 439 | |
| 440 | result := make(model.Routes, 0) |
| 441 | for _, value := range serviceRoutes { |
| 442 | result = append(result, value...) |
| 443 | } |
| 444 | |
| 445 | w.Header().Set("Content-Type", "application/json") |
| 446 | w.WriteHeader(http.StatusOK) |
| 447 | _ = json.NewEncoder(w).Encode(model.Response{Result: result}) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | func (s *Server) handleProxy() http.HandlerFunc { |
| 452 | return func(w http.ResponseWriter, r *http.Request) { |
no test coverage detected