Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor endpoints path builder #38

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading