Skip to content

Commit

Permalink
do not return after postprocesser
Browse files Browse the repository at this point in the history
  • Loading branch information
vaporz committed May 21, 2020
1 parent 6a3d173 commit b69dc73
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion component.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Components) preprocessor(req *http.Request) Preprocessor {
}

// PostProcessor--------------
type Postprocessor func(http.ResponseWriter, *http.Request, interface{}, error)
type Postprocessor func(http.ResponseWriter, *http.Request, interface{}, error) error

// ServeHTTP is an empty func, only for implementing http.Handler
func (p Postprocessor) ServeHTTP(http.ResponseWriter, *http.Request) {}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/apache/thrift v0.12.0 h1:pODnxUFNcjP9UTLZGTdeh+j16A8lJbRvD3rOtrk/7bs=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down
22 changes: 16 additions & 6 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ func doRequest(s Servable, serviceName, methodName string, resp http.ResponseWri
components(req).errorHandlerFunc()(resp, req, err)
return
}
doPostprocessor(s, resp, req, serviceResp, err)
err = doPostprocessor(s, resp, req, serviceResp, err)
if err != nil {
components(req).errorHandlerFunc()(resp, req, err)
return
}
writeResponse(s, resp, req, serviceResp)
}

type headerKey struct{}
Expand Down Expand Up @@ -161,14 +166,18 @@ func doPreprocessor(resp http.ResponseWriter, req *http.Request) error {
return nil
}

func doPostprocessor(s Servable, resp http.ResponseWriter, req *http.Request, serviceResponse interface{}, err error) {
func doPostprocessor(s Servable, resp http.ResponseWriter, req *http.Request, serviceResponse interface{}, err error) error {
// run Postprocessor, if any
post := components(req).Postprocessor(req)
if post != nil {
post(resp, req, serviceResponse, err)
return
if post := components(req).Postprocessor(req); post != nil {
if err := post(resp, req, serviceResponse, err); err != nil {
log.Println(err.Error())
return errors.New(fmt.Sprintf("turbo: encounter error in postprocessor for %s, error: %s", req.URL, err))
}
}
return nil
}

func writeResponse(s Servable, resp http.ResponseWriter, req *http.Request, serviceResponse interface{}){
// return as json
m := Marshaler{
FilterProtoJson: s.ServerField().Config.FilterProtoJson(),
Expand All @@ -177,6 +186,7 @@ func doPostprocessor(s Servable, resp http.ResponseWriter, req *http.Request, se
}
jsonBytes, err := m.JSON(serviceResponse)
if err == nil {
resp.Header().Add("Content-Type", "application/json")
resp.Write(jsonBytes)
} else {
log.Println(err.Error())
Expand Down
18 changes: 9 additions & 9 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ func TestLoadComponentsFromConfig(t *testing.T) {
testGet(t, "http://localhost:"+httpPort+"/hello", `intercepted:{"message":"[grpc server]Hello, "}`)
testGet(t, "http://localhost:"+httpPort+"/hellointerceptor", "interceptor_error:from errorHandler:error!")
testGet(t, "http://localhost:"+httpPort+"/hello_preprocessor", `preprocessor:{"message":"[grpc server]Hello, "}`)
testGet(t, "http://localhost:"+httpPort+"/hello_postprocessor", "postprocessor:[grpc server]Hello, ")
testGet(t, "http://localhost:"+httpPort+"/hello_postprocessor", `postprocessor:{"message":"[grpc server]Hello, "}`)
testGet(t, "http://localhost:"+httpPort+"/hello_hijacker", "hijacker")
testGet(t, "http://localhost:"+httpPort+"/hello_convertor?bool_value=true", `{"message":"{\"values\":{\"someId\":1111111},\"boolValue\":true}"}`)
testGet(t, "http://localhost:"+httpPort+"/hello_hijacker", "hijacker")
testGet(t, "http://localhost:"+httpPort+"/hello/error", "from errorHandler:rpc error: code = Unknown desc = grpc error")

changeServiceYamlWithGrpcComponents(httpPort, "50055", "production")
time.Sleep(time.Millisecond * 1000)
testGet(t, "http://localhost:"+httpPort+"/hello", "test1_intercepted:preprocessor:postprocessor:[grpc server]Hello, ")
testGet(t, "http://localhost:"+httpPort+"/hello", `test1_intercepted:preprocessor:postprocessor:{"message":"[grpc server]Hello, "}`)
s.Stop()
}

Expand Down Expand Up @@ -479,7 +479,7 @@ func runCommonTests(t *testing.T, s *turbo.Server, httpPort, rpcType string) {
s.Components.SetPostprocessor([]string{}, "/hello/{your_name:[a-zA-Z0-9]+}", component(s, "postProcessor").(turbo.Postprocessor))
}
testGet(t, "http://localhost:"+httpPort+"/hello/testtest",
`intercepted:preprocessor:postprocessor:[`+rpcType+` server]Hello, testtest`)
`intercepted:preprocessor:postprocessor:{"message":"[`+rpcType+` server]Hello, testtest"}`)

s.Components.SetHijacker([]string{}, "/hello/{your_name:[a-zA-Z0-9]+}", component(s, "hijacker").(turbo.Hijacker))
testGet(t, "http://localhost:"+httpPort+"/hello/testtest",
Expand Down Expand Up @@ -629,14 +629,14 @@ var errorPreProcessor turbo.Preprocessor = func(resp http.ResponseWriter, req *h
return errors.New("error in preprocessor")
}

var postProcessor turbo.Postprocessor = func(resp http.ResponseWriter, req *http.Request, serviceResp interface{}, err error) {
r := serviceResp.(*proto.SayHelloResponse)
resp.Write([]byte("postprocessor:" + r.Message))
var postProcessor turbo.Postprocessor = func(resp http.ResponseWriter, req *http.Request, serviceResp interface{}, err error) error {
resp.Write([]byte("postprocessor:"))
return nil
}

var thriftPostProcessor turbo.Postprocessor = func(resp http.ResponseWriter, req *http.Request, serviceResp interface{}, err error) {
r := serviceResp.(*tgen.SayHelloResponse)
resp.Write([]byte("postprocessor:" + r.Message))
var thriftPostProcessor turbo.Postprocessor = func(resp http.ResponseWriter, req *http.Request, serviceResp interface{}, err error) error {
resp.Write([]byte("postprocessor:"))
return nil
}

var hijacker turbo.Hijacker = func(resp http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit b69dc73

Please sign in to comment.