Skip to content

Commit

Permalink
cleaning up kit example
Browse files Browse the repository at this point in the history
  • Loading branch information
jprobinson committed Jun 23, 2017
1 parent 8ce6660 commit 3ed843c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
14 changes: 9 additions & 5 deletions examples/servers/kit/api/cats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package api

import (
"context"
"fmt"
"net/http"

kitserver "github.com/NYTimes/gizmo/server/kit"
"github.com/NYTimes/gizmo/server/kit"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
ocontext "golang.org/x/net/context"

Expand All @@ -12,7 +14,7 @@ import (

// GRPC layer, add the service-wide middleware ourselves
func (s service) GetCats(ctx ocontext.Context, r *google_protobuf.Empty) (*CatsResponse, error) {
res, err := s.Middleware(s.getCats)(ctx, r)
res, err := s.getCats(ctx, r)
if res != nil {
return res.(*CatsResponse), err
}
Expand All @@ -23,10 +25,12 @@ func (s service) GetCats(ctx ocontext.Context, r *google_protobuf.Empty) (*CatsR
func (s service) getCats(ctx context.Context, _ interface{}) (interface{}, error) {
res, err := s.client.SemanticConceptSearch("des", "cats")
if err != nil {
kitserver.Logger(ctx).Log("unable to get cats", err)
return &CatsResponse{Status: "ERROR"}, nil
kit.LogErrorMsg(ctx, err, "unable to get cats")
return nil, kit.NewJSONStatusResponse(
&CatsResponse{Status: "ERROR"},
http.StatusInternalServerError)
}
kitserver.Logger(ctx).Log("cats results found", len(res))
kit.LogMsg(ctx, fmt.Sprintf("cats results found: %d", len(res)))
return semToCat(res), nil
}

Expand Down
13 changes: 8 additions & 5 deletions examples/servers/kit/api/mostpopular.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package api

import (
"context"
"fmt"
"net/http"
"strconv"

kitserver "github.com/NYTimes/gizmo/server/kit"
"github.com/NYTimes/gizmo/server/kit"
ocontext "golang.org/x/net/context"

"github.com/NYTimes/gizmo/examples/nyt"
Expand All @@ -26,19 +27,21 @@ func (s service) getMostPopular(ctx context.Context, r interface{}) (interface{}

res, err := s.client.GetMostPopular(mpr.ResourceType, mpr.Section, uint(mpr.Timeframe))
if err != nil {
return nil, kitserver.NewJSONStatusResponse(err.Error(), http.StatusBadRequest)
return nil, kit.NewJSONStatusResponse(
&GetMostPopularResourceTypeSectionTimeframeRequest{},
http.StatusBadRequest)
}

kitserver.Logger(ctx).Log("most popular results found", len(res))
kit.LogMsg(ctx, fmt.Sprintf("most popular results found: %d", len(res)))
return mpToMP(res), nil
}

// CUSTOM HTTP REQUEST DECODER
func decodeMostPopularRequest(ctx context.Context, r *http.Request) (interface{}, error) {
vs := kitserver.Vars(r)
vs := kit.Vars(r)
timeframe, err := strconv.ParseUint(vs["timeframe"], 10, 8)
if err != nil {
return nil, kitserver.NewJSONStatusResponse(
return nil, kit.NewJSONStatusResponse(
&MostPopularResponse{Status: "bad request"},
http.StatusBadRequest)
}
Expand Down

0 comments on commit 3ed843c

Please sign in to comment.