diff --git a/api/v1/api.go b/api/v1/api.go index 7d107a2..1291e23 100644 --- a/api/v1/api.go +++ b/api/v1/api.go @@ -14,12 +14,6 @@ import ( "go.uber.org/zap" ) -const baseUrl = "/api/v1" - -func path(endpoint string) string { - return baseUrl + endpoint -} - func NewRESTApiV1(productionMode bool, logger *zap.Logger) *RESTApiV1 { restAPI := &RESTApiV1{ router: mux.NewRouter(), @@ -35,19 +29,19 @@ func NewRESTApiV1(productionMode bool, logger *zap.Logger) *RESTApiV1 { restAPI.router.HandleFunc("/", restAPI.IndexPage).Methods(http.MethodGet, http.MethodPost, http.MethodOptions, http.MethodPut, http.MethodHead) - restAPI.router.HandleFunc(path("/packetloss/start"), restAPI.PacketlossStart).Methods(http.MethodPost) - restAPI.router.HandleFunc(path("/packetloss/status"), restAPI.PacketlossStatus).Methods(http.MethodGet) - restAPI.router.HandleFunc(path("/packetloss/stop"), restAPI.PacketlossStop).Methods(http.MethodPost) + restAPI.router.HandleFunc(PacketlossPath.Start(), restAPI.PacketlossStart).Methods(http.MethodPost) + restAPI.router.HandleFunc(PacketlossPath.Status(), restAPI.PacketlossStatus).Methods(http.MethodGet) + restAPI.router.HandleFunc(PacketlossPath.Stop(), restAPI.PacketlossStop).Methods(http.MethodPost) - restAPI.router.HandleFunc(path("/bandwidth/start"), restAPI.BandwidthStart).Methods(http.MethodPost) - restAPI.router.HandleFunc(path("/bandwidth/status"), restAPI.BandwidthStatus).Methods(http.MethodGet) - restAPI.router.HandleFunc(path("/bandwidth/stop"), restAPI.BandwidthStop).Methods(http.MethodPost) + restAPI.router.HandleFunc(BandwidthPath.Start(), restAPI.BandwidthStart).Methods(http.MethodPost) + restAPI.router.HandleFunc(BandwidthPath.Status(), restAPI.BandwidthStatus).Methods(http.MethodGet) + restAPI.router.HandleFunc(BandwidthPath.Stop(), restAPI.BandwidthStop).Methods(http.MethodPost) - restAPI.router.HandleFunc(path("/latency/start"), restAPI.LatencyStart).Methods(http.MethodPost) - restAPI.router.HandleFunc(path("/latency/status"), restAPI.LatencyStatus).Methods(http.MethodGet) - restAPI.router.HandleFunc(path("/latency/stop"), restAPI.LatencyStop).Methods(http.MethodPost) + restAPI.router.HandleFunc(LatencyPath.Start(), restAPI.LatencyStart).Methods(http.MethodPost) + restAPI.router.HandleFunc(LatencyPath.Status(), restAPI.LatencyStatus).Methods(http.MethodGet) + restAPI.router.HandleFunc(LatencyPath.Stop(), restAPI.LatencyStop).Methods(http.MethodPost) - restAPI.router.HandleFunc(path("/services/status"), restAPI.NetServicesStatus).Methods(http.MethodGet) + restAPI.router.HandleFunc(ServicesPath.Status(), restAPI.NetServicesStatus).Methods(http.MethodGet) return restAPI } diff --git a/api/v1/api_path.go b/api/v1/api_path.go new file mode 100644 index 0000000..921c84a --- /dev/null +++ b/api/v1/api_path.go @@ -0,0 +1,26 @@ +package api + +const endpointPrefix = "/api/v1" + +type serviceEndpointPath struct { + basePath string +} + +func (e *serviceEndpointPath) Status() string { + return endpointPrefix + e.basePath + "/status" +} + +func (e *serviceEndpointPath) Start() string { + return endpointPrefix + e.basePath + "/start" +} + +func (e *serviceEndpointPath) Stop() string { + return endpointPrefix + e.basePath + "/stop" +} + +var ( + PacketlossPath = &serviceEndpointPath{basePath: "/packetloss"} + BandwidthPath = &serviceEndpointPath{basePath: "/bandwidth"} + LatencyPath = &serviceEndpointPath{basePath: "/latency"} + ServicesPath = &serviceEndpointPath{basePath: "/services"} +) diff --git a/api/v1/bandwidth_test.go b/api/v1/bandwidth_test.go index 5f68c55..6aeeb17 100644 --- a/api/v1/bandwidth_test.go +++ b/api/v1/bandwidth_test.go @@ -17,7 +17,7 @@ func (s *APITestSuite) TestBandwidthStartStop() { jsonBody, err := json.Marshal(s.getDefaultBandwidthStartRequest()) require.NoError(t, err) - req, err := http.NewRequest(http.MethodPost, "/api/v1/bandwidth/start", bytes.NewReader(jsonBody)) + req, err := http.NewRequest(http.MethodPost, api.BandwidthPath.Start(), bytes.NewReader(jsonBody)) require.NoError(t, err) rr := httptest.NewRecorder() @@ -48,7 +48,7 @@ func (s *APITestSuite) TestBandwidthStatus() { jsonBody, err := json.Marshal(s.getDefaultBandwidthStartRequest()) require.NoError(t, err) - req, err := http.NewRequest(http.MethodPost, "/api/v1/bandwidth/start", bytes.NewReader(jsonBody)) + req, err := http.NewRequest(http.MethodPost, api.BandwidthPath.Start(), bytes.NewReader(jsonBody)) require.NoError(t, err) rr := httptest.NewRecorder() diff --git a/api/v1/latency_test.go b/api/v1/latency_test.go index 86344e4..b158728 100644 --- a/api/v1/latency_test.go +++ b/api/v1/latency_test.go @@ -17,7 +17,7 @@ func (s *APITestSuite) TestLatencyStartStop() { jsonBody, err := json.Marshal(s.getDefaultLatencyStartRequest()) require.NoError(t, err) - req, err := http.NewRequest(http.MethodPost, "/api/v1/latency/start", bytes.NewReader(jsonBody)) + req, err := http.NewRequest(http.MethodPost, api.LatencyPath.Start(), bytes.NewReader(jsonBody)) require.NoError(t, err) rr := httptest.NewRecorder() @@ -48,7 +48,7 @@ func (s *APITestSuite) TestLatencyStatus() { jsonBody, err := json.Marshal(s.getDefaultLatencyStartRequest()) require.NoError(t, err) - req, err := http.NewRequest(http.MethodPost, "/api/v1/latency/start", bytes.NewReader(jsonBody)) + req, err := http.NewRequest(http.MethodPost, api.LatencyPath.Start(), bytes.NewReader(jsonBody)) require.NoError(t, err) rr := httptest.NewRecorder() diff --git a/api/v1/packetloss_test.go b/api/v1/packetloss_test.go index 9f8b838..9a8d4b2 100644 --- a/api/v1/packetloss_test.go +++ b/api/v1/packetloss_test.go @@ -17,7 +17,7 @@ func (s *APITestSuite) TestPacketlossStartStop() { jsonBody, err := json.Marshal(s.getDefaultPacketLossStartRequest()) require.NoError(t, err) - req, err := http.NewRequest(http.MethodPost, "/api/v1/packetloss/start", bytes.NewReader(jsonBody)) + req, err := http.NewRequest(http.MethodPost, api.PacketlossPath.Start(), bytes.NewReader(jsonBody)) require.NoError(t, err) rr := httptest.NewRecorder() @@ -48,7 +48,7 @@ func (s *APITestSuite) TestPacketlossStatus() { jsonBody, err := json.Marshal(s.getDefaultPacketLossStartRequest()) require.NoError(t, err) - req, err := http.NewRequest(http.MethodPost, "/api/v1/packetloss/start", bytes.NewReader(jsonBody)) + req, err := http.NewRequest(http.MethodPost, api.PacketlossPath.Start(), bytes.NewReader(jsonBody)) require.NoError(t, err) rr := httptest.NewRecorder()