Skip to content

Commit

Permalink
Merge pull request #16 from Comcast/hotfix/MiscImprovements
Browse files Browse the repository at this point in the history
Misc important fixes
  • Loading branch information
schmidtw authored Oct 31, 2017
2 parents ddeb5b2 + 1d8a78f commit 1f7447a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
9 changes: 4 additions & 5 deletions src/tr1d1um/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package main

import (
"context"
"net/http"

"fmt"
"net/http"
"strings"

"github.com/Comcast/webpa-common/logging"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand All @@ -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)
Expand Down
19 changes: 9 additions & 10 deletions src/tr1d1um/tr1d1um.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions src/tr1d1um/tr1d1um_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/http"
"net/http/httptest"
"testing"

"time"

"github.com/Comcast/webpa-common/logging"
Expand Down Expand Up @@ -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}`)
Expand Down

0 comments on commit 1f7447a

Please sign in to comment.