Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Unified Diff: server/prpc/server.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Update tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: server/prpc/server.go
diff --git a/server/prpc/server.go b/server/prpc/server.go
index d78599711740941c2f5f76fde1d066052e139d00..a01043df95a9358b0c4fb23fcd8aff15c1378138 100644
--- a/server/prpc/server.go
+++ b/server/prpc/server.go
@@ -11,7 +11,6 @@ import (
"strings"
"sync"
- "github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -19,7 +18,7 @@ import (
"github.com/luci/luci-go/common/errors"
"github.com/luci/luci-go/common/logging"
"github.com/luci/luci-go/server/auth"
- "github.com/luci/luci-go/server/middleware"
+ "github.com/luci/luci-go/server/router"
prpccommon "github.com/luci/luci-go/common/prpc"
)
@@ -107,7 +106,7 @@ func (s *Server) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
}
// authenticate forces authentication set by RegisterDefaultAuth.
-func (s *Server) authenticate(base middleware.Base) middleware.Base {
+func (s *Server) authenticate() router.Handler {
a := s.Authenticator
if a == nil {
a = GetDefaultAuth()
@@ -118,23 +117,22 @@ func (s *Server) authenticate(base middleware.Base) middleware.Base {
}
if len(a) == 0 {
- return base
+ return router.VoidHandler
}
- return func(h middleware.Handler) httprouter.Handle {
- return base(func(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) {
- c = auth.SetAuthenticator(c, a)
- switch c, err := a.Authenticate(c, r); {
- case errors.IsTransient(err):
- res := errResponse(codes.Internal, http.StatusInternalServerError, escapeFmt(err.Error()))
- res.write(c, w)
- case err != nil:
- res := errResponse(codes.Unauthenticated, http.StatusUnauthorized, escapeFmt(err.Error()))
- res.write(c, w)
- default:
- h(c, w, r, p)
- }
- })
+ return func(c *router.Context) {
+ c.Context = auth.SetAuthenticator(c.Context, a)
+ var err error
+ switch c.Context, err = a.Authenticate(c.Context, c.Request); {
+ case errors.IsTransient(err):
+ res := errResponse(codes.Internal, http.StatusInternalServerError, escapeFmt(err.Error()))
+ res.write(c.Context, c.Writer)
+ c.Abort()
+ case err != nil:
+ res := errResponse(codes.Unauthenticated, http.StatusUnauthorized, escapeFmt(err.Error()))
+ res.write(c.Context, c.Writer)
+ c.Abort()
+ }
}
}
@@ -145,35 +143,33 @@ func (s *Server) authenticate(base middleware.Base) middleware.Base {
//
// The authenticator in 'base' is always replaced by pRPC specific one. For more
// details about the authentication see Server.Authenticator doc.
-func (s *Server) InstallHandlers(r *httprouter.Router, base middleware.Base) {
+func (s *Server) InstallHandlers(r *router.Router, handlers []router.Handler) {
s.mu.Lock()
defer s.mu.Unlock()
- base = s.authenticate(base)
-
- r.POST("/prpc/:service/:method", base(s.handlePOST))
- r.OPTIONS("/prpc/:service/:method", base(s.handleOPTIONS))
+ r.POST("/prpc/:service/:method", append(handlers, s.authenticate(), s.handlePOST)...)
+ r.OPTIONS("/prpc/:service/:method", append(handlers, s.authenticate(), s.handleOPTIONS)...)
}
// handle handles RPCs.
// See https://godoc.org/github.com/luci/luci-go/common/prpc#hdr-Protocol
// for pRPC protocol.
-func (s *Server) handlePOST(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) {
- serviceName := p.ByName("service")
- methodName := p.ByName("method")
- res := s.respond(c, w, r, serviceName, methodName)
+func (s *Server) handlePOST(c *router.Context) {
+ serviceName := c.Params.ByName("service")
+ methodName := c.Params.ByName("method")
+ res := s.respond(c.Context, c.Writer, c.Request, serviceName, methodName)
- c = logging.SetFields(c, logging.Fields{
+ c.Context = logging.SetFields(c.Context, logging.Fields{
"service": serviceName,
"method": methodName,
})
- s.setAccessControlHeaders(c, r, w, false)
- res.write(c, w)
+ s.setAccessControlHeaders(c.Context, c.Request, c.Writer, false)
+ res.write(c.Context, c.Writer)
}
-func (s *Server) handleOPTIONS(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) {
- s.setAccessControlHeaders(c, r, w, true)
- w.WriteHeader(http.StatusOK)
+func (s *Server) handleOPTIONS(c *router.Context) {
+ s.setAccessControlHeaders(c.Context, c.Request, c.Writer, true)
+ c.Writer.WriteHeader(http.StatusOK)
}
func (s *Server) respond(c context.Context, w http.ResponseWriter, r *http.Request, serviceName, methodName string) *response {

Powered by Google App Engine
This is Rietveld 408576698