path_api.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package httpapi
  2. import (
  3. "net/http"
  4. )
  5. func (s *server) handlePathSummaries(w http.ResponseWriter, r *http.Request) {
  6. if s.pathAnalysis == nil {
  7. writeJSON(w, http.StatusOK, map[string]any{"summaries": []any{}, "available": false})
  8. return
  9. }
  10. routerID := r.URL.Query().Get("router")
  11. if routerID == "" {
  12. writeError(w, http.StatusBadRequest, "invalid_input", "router required")
  13. return
  14. }
  15. summaries, err := s.pathAnalysis.Summaries(routerID)
  16. if err != nil {
  17. writeError(w, http.StatusInternalServerError, "path_error", err.Error())
  18. return
  19. }
  20. writeJSON(w, http.StatusOK, map[string]any{"summaries": summaries, "available": true})
  21. }
  22. func (s *server) handlePathLatest(w http.ResponseWriter, r *http.Request) {
  23. if s.pathAnalysis == nil {
  24. writeError(w, http.StatusServiceUnavailable, "path_unavailable")
  25. return
  26. }
  27. routerID := r.URL.Query().Get("router")
  28. dest := r.URL.Query().Get("destination")
  29. if routerID == "" || dest == "" {
  30. writeError(w, http.StatusBadRequest, "invalid_input", "router and destination required")
  31. return
  32. }
  33. result, err := s.pathAnalysis.Latest(routerID, dest)
  34. if err != nil {
  35. writeError(w, http.StatusNotFound, "not_found")
  36. return
  37. }
  38. writeJSON(w, http.StatusOK, result)
  39. }
  40. func (s *server) handlePathHistory(w http.ResponseWriter, r *http.Request) {
  41. if s.pathAnalysis == nil {
  42. writeError(w, http.StatusServiceUnavailable, "path_unavailable")
  43. return
  44. }
  45. routerID := r.URL.Query().Get("router")
  46. dest := r.URL.Query().Get("destination")
  47. if routerID == "" || dest == "" {
  48. writeError(w, http.StatusBadRequest, "invalid_input", "router and destination required")
  49. return
  50. }
  51. results, err := s.pathAnalysis.History(routerID, dest, 20)
  52. if err != nil {
  53. writeError(w, http.StatusInternalServerError, "path_error", err.Error())
  54. return
  55. }
  56. writeJSON(w, http.StatusOK, map[string]any{"results": results})
  57. }
  58. func (s *server) handlePathDestinations(w http.ResponseWriter, r *http.Request) {
  59. if s.pathAnalysis == nil {
  60. writeJSON(w, http.StatusOK, map[string]any{"destinations": []any{}, "available": false})
  61. return
  62. }
  63. dests, err := s.pathAnalysis.AllDestinations()
  64. if err != nil {
  65. writeError(w, http.StatusInternalServerError, "path_error", err.Error())
  66. return
  67. }
  68. writeJSON(w, http.StatusOK, map[string]any{"destinations": dests, "available": true})
  69. }