Skip to content

Commit

Permalink
add middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
fengbeihong committed Sep 22, 2021
1 parent 71d42f6 commit 4d281e6
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 11 deletions.
16 changes: 14 additions & 2 deletions cmd/protoc-gen-go-axe/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,28 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
func genHttpService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service) {
serverType := service.GoName + "Server"

g.P("func Register", service.GoName, "HttpServer(s *", httpPackage.Ident("Server"), ",srv ", serverType, ") {")
g.P("type MiddlewareFunc func(", httpPackage.Ident("Handler"), ") ", httpPackage.Ident("Handler"))
g.P()

g.P("func Register", service.GoName, "HttpServer(s *", httpPackage.Ident("Server"), ",srv ", serverType, ", middlewares ...MiddlewareFunc) {")
g.P("mux := ", httpPackage.Ident("NewServeMux"), "()")
g.P()

for _, method := range service.Methods {
hname := genHttpServerMethod(gen, file, g, method)
g.P("mux.HandleFunc(", strconv.Quote(fmt.Sprintf("/%s/%s", service.Desc.FullName(), method.Desc.Name())), ", ", hname, ")")

hf := fmt.Sprintf("_%s_%s_HandlerFunc", service.GoName, method.GoName)
g.P("var ", hf, " ", httpPackage.Ident("Handler"))

g.P(hf, " = ", httpPackage.Ident("Handler"), "(", httpPackage.Ident("HandlerFunc"), "(", hname, "))")
g.P("for _, m := range middlewares {")
g.P(hf, " = m(", hf, ")")
g.P("}")

g.P("mux.Handle(", strconv.Quote(fmt.Sprintf("/%s/%s", service.Desc.FullName(), method.Desc.Name())), ", ", hf, ")")
g.P()
}

g.P("s.Handler = mux")
g.P("}")
g.P()
Expand Down
25 changes: 23 additions & 2 deletions demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"log"
"net/http"
"path"
"runtime"
"time"
Expand All @@ -18,7 +19,27 @@ type echoServer struct {
}

func (s *echoServer) Echo(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
return &pb.EchoResponse{Value: req.GetValue()}, nil
return &pb.EchoResponse{Value: "echo1_" + req.GetValue()}, nil
}

func (s *echoServer) Echo2(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
return &pb.EchoResponse{Value: "echo2_" + req.GetValue()}, nil
}

func middleware1(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("middleware1 - Before Handler")
next.ServeHTTP(w, r)
log.Println("middleware1 - After Handler")
})
}

func middleware2(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("middleware2 - Before Handler")
next.ServeHTTP(w, r)
log.Println("middleware2 - After Handler")
})
}

/////////////////////////////
Expand Down Expand Up @@ -56,7 +77,7 @@ func main() {
// register rpc
pb.RegisterEchoServiceServer(s.GrpcServer(), &echoServer{})
// register http, pattern和handler会自动生成
pb.RegisterEchoServiceHttpServer(s.HttpServer(), &echoServer{})
pb.RegisterEchoServiceHttpServer(s.HttpServer(), &echoServer{}, []pb.MiddlewareFunc{middleware1, middleware2}...)

// 调用client的例子
//go clientExample()
Expand Down
15 changes: 10 additions & 5 deletions demo/pb/echo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/pb/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ message EchoResponse{

service EchoService{
rpc Echo(EchoRequest) returns (EchoResponse){}
rpc Echo2(EchoRequest) returns (EchoResponse){}
}
81 changes: 79 additions & 2 deletions demo/pb/echo_axe.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d281e6

Please sign in to comment.