-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: deprecate http handlers (#108)
* refactor: deprecate httpapi handlers * refactor: remove unnecessary commented code * fix: middleware bug
- Loading branch information
Showing
73 changed files
with
3,639 additions
and
11,515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package grpc_interceptor | ||
|
||
import ( | ||
"context" | ||
|
||
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto" | ||
) | ||
|
||
type dummyService struct { | ||
pb_testproto.TestServiceServer | ||
} | ||
|
||
func (s *dummyService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { | ||
if ping.Value == "panic" { | ||
panic("very bad thing happened") | ||
} | ||
if ping.Value == "nilpanic" { | ||
panic(nil) | ||
} | ||
return s.TestServiceServer.Ping(ctx, ping) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package grpc_interceptor | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/odpf/columbus/metrics" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func StatsD(mm *metrics.StatsdMonitor) grpc.UnaryServerInterceptor { | ||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
if mm == nil { | ||
return handler(ctx, req) | ||
} | ||
start := time.Now() | ||
resp, err := handler(ctx, req) | ||
code := status.Code(err) | ||
mm.ResponseTimeGRPC(info.FullMethod, int64(time.Since(start)/time.Millisecond)) | ||
mm.ResponseStatusGRPC(info.FullMethod, code.String()) | ||
return resp, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package grpc_interceptor | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" | ||
grpc_testing "github.com/grpc-ecosystem/go-grpc-middleware/testing" | ||
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto" | ||
"github.com/odpf/columbus/lib/mocks" | ||
"github.com/odpf/columbus/metrics" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
var ( | ||
statsdPrefix = "columbusApi" | ||
metricsSeparator = "." | ||
) | ||
|
||
type StatsDTestSuite struct { | ||
*grpc_testing.InterceptorTestSuite | ||
statsdClient *mocks.StatsdClient | ||
} | ||
|
||
func TestStatsDSuite(t *testing.T) { | ||
statsdClient := new(mocks.StatsdClient) | ||
|
||
monitor := metrics.NewStatsdMonitor(statsdClient, statsdPrefix, metricsSeparator) | ||
s := &StatsDTestSuite{ | ||
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{ | ||
TestService: &dummyService{TestServiceServer: &grpc_testing.TestPingService{T: t}}, | ||
ServerOpts: []grpc.ServerOption{ | ||
grpc_middleware.WithUnaryServerChain( | ||
StatsD(monitor)), | ||
}, | ||
}, | ||
statsdClient: statsdClient, | ||
} | ||
suite.Run(t, s) | ||
} | ||
|
||
func (s *StatsDTestSuite) TestUnary_StatsDMetrics() { | ||
s.statsdClient.EXPECT().Increment("columbusApi.responseStatusCode,statusCode=OK,method=/mwitkow.testproto.TestService/Ping").Once() | ||
s.statsdClient.EXPECT().Timing("columbusApi.responseTime,method=/mwitkow.testproto.TestService/Ping", int64(0)).Once() | ||
_, err := s.Client.Ping(context.Background(), &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}) | ||
code := status.Code(err) | ||
require.Equal(s.T(), codes.OK, code) | ||
s.statsdClient.AssertExpectations(s.T()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package grpc_interceptor | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" | ||
grpc_testing "github.com/grpc-ecosystem/go-grpc-middleware/testing" | ||
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto" | ||
"github.com/odpf/columbus/lib/mocks" | ||
"github.com/odpf/columbus/user" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
const ( | ||
identityHeaderKey = "Columbus-User-ID" | ||
defaultProvider = "shield" | ||
) | ||
|
||
type UserTestSuite struct { | ||
*grpc_testing.InterceptorTestSuite | ||
userRepo *mocks.UserRepository | ||
} | ||
|
||
func TestUserSuite(t *testing.T) { | ||
mockUserRepo := new(mocks.UserRepository) | ||
userSvc := user.NewService(mockUserRepo, user.Config{ | ||
IdentityProviderDefaultName: defaultProvider, | ||
}) | ||
s := &UserTestSuite{ | ||
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{ | ||
TestService: &dummyService{TestServiceServer: &grpc_testing.TestPingService{T: t}}, | ||
ServerOpts: []grpc.ServerOption{ | ||
grpc_middleware.WithUnaryServerChain( | ||
ValidateUser(identityHeaderKey, userSvc)), | ||
}, | ||
}, | ||
userRepo: mockUserRepo, | ||
} | ||
suite.Run(t, s) | ||
} | ||
|
||
func (s *UserTestSuite) TestUnary_IdentityHeaderNotPresent() { | ||
_, err := s.Client.Ping(s.SimpleCtx(), &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}) | ||
code := status.Code(err) | ||
require.Equal(s.T(), codes.InvalidArgument, code) | ||
require.EqualError(s.T(), err, "rpc error: code = InvalidArgument desc = identity header is empty") | ||
} | ||
|
||
func (s *UserTestSuite) TestUnary_UserServiceError() { | ||
userEmail := "user-email-error" | ||
customError := errors.New("some error") | ||
s.userRepo.EXPECT().GetID(mock.Anything, userEmail).Return("", customError) | ||
s.userRepo.EXPECT().Create(mock.Anything, mock.Anything).Return("", customError) | ||
|
||
ctx := metadata.AppendToOutgoingContext(context.Background(), identityHeaderKey, userEmail) | ||
_, err := s.Client.Ping(ctx, &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}) | ||
code := status.Code(err) | ||
require.Equal(s.T(), codes.Internal, code) | ||
|
||
s.userRepo.AssertExpectations(s.T()) | ||
} | ||
|
||
func (s *UserTestSuite) TestUnary_HeaderPassed() { | ||
userEmail := "user-email" | ||
userID := "user-id" | ||
s.userRepo.EXPECT().GetID(mock.Anything, userEmail).Return(userID, nil) | ||
|
||
ctx := metadata.AppendToOutgoingContext(s.SimpleCtx(), identityHeaderKey, userEmail) | ||
_, err := s.Client.Ping(ctx, &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}) | ||
code := status.Code(err) | ||
require.Equal(s.T(), codes.OK, code) | ||
|
||
s.userRepo.AssertExpectations(s.T()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,9 @@ | ||
package httpapi | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/odpf/columbus/api/httpapi/handlers" | ||
) | ||
|
||
type Handler struct { | ||
Asset *handlers.AssetHandler | ||
Type *handlers.TypeHandler | ||
Record *handlers.RecordHandler | ||
Search *handlers.SearchHandler | ||
Lineage *handlers.LineageHandler | ||
Tag *handlers.TagHandler | ||
TagTemplate *handlers.TagTemplateHandler | ||
User *handlers.UserHandler | ||
Discussion *handlers.DiscussionHandler | ||
} | ||
|
||
func RegisterRoutes(router *mux.Router, handlerCollection *Handler) { | ||
setupV1Beta1Router(router, handlerCollection) | ||
|
||
router.NotFoundHandler = http.HandlerFunc(handlers.NotFound) | ||
router.MethodNotAllowedHandler = http.HandlerFunc(handlers.MethodNotAllowed) | ||
Record *handlers.RecordHandler | ||
} |
Oops, something went wrong.