Skip to content

Commit

Permalink
chore: refactor endpoints path builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Dec 7, 2023
1 parent 7ca1d84 commit 82471a5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
26 changes: 10 additions & 16 deletions api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions api/v1/api_path.go
Original file line number Diff line number Diff line change
@@ -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"}
)
4 changes: 2 additions & 2 deletions api/v1/bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions api/v1/latency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions api/v1/packetloss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 82471a5

Please sign in to comment.