diff --git a/src/tr1d1um/http.go b/src/tr1d1um/http.go index 17c33fb8..1c0db886 100644 --- a/src/tr1d1um/http.go +++ b/src/tr1d1um/http.go @@ -19,9 +19,8 @@ package main import ( "context" - "net/http" - "fmt" + "net/http" "strings" "github.com/Comcast/webpa-common/logging" @@ -86,9 +85,9 @@ func (ch *ConversionHandler) ServeHTTP(origin http.ResponseWriter, req *http.Req //set timeout for response waiting ctx, cancel := context.WithTimeout(req.Context(), ch.sender.GetRespTimeout()) - response, err := ch.sender.Send(ch, origin, wdmpPayload, req.WithContext(ctx)) - cancel() // we are done using the context timeout on the request + defer cancel() + response, err := ch.sender.Send(ch, origin, wdmpPayload, req.WithContext(ctx)) ch.sender.HandleResponse(ch, err, response, origin, false) } @@ -97,6 +96,7 @@ func (ch *ConversionHandler) HandleStat(origin http.ResponseWriter, req *http.Re var errorLogger = logging.Error(ch.logger) ctx, cancel := context.WithTimeout(req.Context(), ch.sender.GetRespTimeout()) + defer cancel() fullPath := ch.targetURL + req.URL.RequestURI() requestToServer, err := http.NewRequest(http.MethodGet, fullPath, nil) @@ -111,7 +111,6 @@ func (ch *ConversionHandler) HandleStat(origin http.ResponseWriter, req *http.Re requestWithContext := requestToServer.WithContext(ctx) response, err := ch.PerformRequest(requestWithContext) - cancel() origin.Header().Set("Content-Type", "application/json") ch.sender.HandleResponse(ch, err, response, origin, true) diff --git a/src/tr1d1um/tr1d1um.go b/src/tr1d1um/tr1d1um.go index 81939972..2bb3911c 100644 --- a/src/tr1d1um/tr1d1um.go +++ b/src/tr1d1um/tr1d1um.go @@ -87,10 +87,11 @@ func tr1d1um(arguments []string) (exitCode int) { conversionHandler := SetUpHandler(v, logger) r := mux.NewRouter() + baseRouter := r.PathPrefix(fmt.Sprintf("%s/%s", baseURI, v.GetString("version"))).Subrouter() - AddRoutes(r, preHandler, conversionHandler, v) + AddRoutes(baseRouter, preHandler, conversionHandler) - if exitCode = ConfigureWebHooks(r, preHandler, v, logger); exitCode != 0 { + if exitCode = ConfigureWebHooks(baseRouter, preHandler, v, logger); exitCode != 0 { return } @@ -134,7 +135,7 @@ func ConfigureWebHooks(r *mux.Router, preHandler *alice.Chain, v *viper.Viper, l } //AddRoutes configures the paths and connection rules to TR1D1UM -func AddRoutes(r *mux.Router, preHandler *alice.Chain, conversionHandler *ConversionHandler, v *viper.Viper) { +func AddRoutes(r *mux.Router, preHandler *alice.Chain, conversionHandler *ConversionHandler) { var BodyNonEmpty = func(request *http.Request, match *mux.RouteMatch) (accept bool) { if request.Body != nil { var tmp bytes.Buffer @@ -148,21 +149,19 @@ func AddRoutes(r *mux.Router, preHandler *alice.Chain, conversionHandler *Conver return } - apiHandler := r.PathPrefix(fmt.Sprintf("%s/%s", baseURI, v.GetString("version"))).Subrouter() - - apiHandler.Handle("/device/{deviceid}/{service}", preHandler.Then(conversionHandler)). + r.Handle("/device/{deviceid}/stat", preHandler.ThenFunc(conversionHandler.HandleStat)). Methods(http.MethodGet) - apiHandler.Handle("/device/{deviceid}/stat", preHandler.ThenFunc(conversionHandler.HandleStat)). + r.Handle("/device/{deviceid}/{service}", preHandler.Then(conversionHandler)). Methods(http.MethodGet) - apiHandler.Handle("/device/{deviceid}/{service}", preHandler.Then(conversionHandler)). + r.Handle("/device/{deviceid}/{service}", preHandler.Then(conversionHandler)). Methods(http.MethodPatch).MatcherFunc(BodyNonEmpty) - apiHandler.Handle("/device/{deviceid}/{service}/{parameter}", preHandler.Then(conversionHandler)). + r.Handle("/device/{deviceid}/{service}/{parameter}", preHandler.Then(conversionHandler)). Methods(http.MethodDelete) - apiHandler.Handle("/device/{deviceid}/{service}/{parameter}", preHandler.Then(conversionHandler)). + r.Handle("/device/{deviceid}/{service}/{parameter}", preHandler.Then(conversionHandler)). Methods(http.MethodPut, http.MethodPost).MatcherFunc(BodyNonEmpty) } diff --git a/src/tr1d1um/tr1d1um_test.go b/src/tr1d1um/tr1d1um_test.go index 94cb17f6..c07b7b2a 100644 --- a/src/tr1d1um/tr1d1um_test.go +++ b/src/tr1d1um/tr1d1um_test.go @@ -23,7 +23,6 @@ import ( "net/http" "net/http/httptest" "testing" - "time" "github.com/Comcast/webpa-common/logging" @@ -64,7 +63,7 @@ func TestRouteConfigurations(t *testing.T) { v := viper.New() v.Set("version", "v2") - AddRoutes(r, fakePreHandler, fakeHandler, v) + AddRoutes(r.PathPrefix("/api/v2").Subrouter(), fakePreHandler, fakeHandler) var nonEmpty bytes.Buffer nonEmpty.WriteString(`{empty: false}`)