Skip to content

#2870 add more flexibility on midddleware #4364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rest/config.go
Original file line number Diff line number Diff line change
@@ -52,9 +52,10 @@ type (
MaxConns int `json:",default=10000"`
MaxBytes int64 `json:",default=1048576"`
// milliseconds
Timeout int64 `json:",default=3000"`
CpuThreshold int64 `json:",default=900,range=[0:1000)"`
Signature SignatureConf `json:",optional"`
Timeout int64 `json:",default=3000"`
CpuThreshold int64 `json:",default=900,range=[0:1000)"`
Signature SignatureConf `json:",optional"`
AddNativeMiddlewares bool `json:",default=true"`
// There are default values for all the items in Middlewares.
Middlewares MiddlewaresConf
// TraceIgnorePaths is paths blacklist for trace middleware.
10 changes: 6 additions & 4 deletions rest/engine.go
Original file line number Diff line number Diff line change
@@ -98,7 +98,10 @@ func (ng *engine) bindRoute(fr featuredRoutes, router httpx.Router, metrics *sta
route Route, verifier func(chain.Chain) chain.Chain) error {
chn := ng.chain
if chn == nil {
chn = ng.buildChainWithNativeMiddlewares(fr, route, metrics)
chn = chain.New()
}
if ng.conf.AddNativeMiddlewares {
chn = ng.buildChainWithNativeMiddlewares(chn, fr, route, metrics)
}

chn = ng.appendAuthHandler(fr, chn, verifier)
@@ -123,9 +126,8 @@ func (ng *engine) bindRoutes(router httpx.Router) error {
return nil
}

func (ng *engine) buildChainWithNativeMiddlewares(fr featuredRoutes, route Route,
func (ng *engine) buildChainWithNativeMiddlewares(chn chain.Chain, fr featuredRoutes, route Route,
metrics *stat.Metrics) chain.Chain {
chn := chain.New()

if ng.conf.Middlewares.Trace {
chn = chn.Append(handler.TraceHandler(ng.conf.Name,
@@ -162,8 +164,8 @@ func (ng *engine) buildChainWithNativeMiddlewares(fr featuredRoutes, route Route
if ng.conf.Middlewares.Gunzip {
chn = chn.Append(handler.GunzipHandler)
}

return chn

}

func (ng *engine) checkedMaxBytes(bytes int64) int64 {
2 changes: 1 addition & 1 deletion rest/server_test.go
Original file line number Diff line number Diff line change
@@ -627,7 +627,7 @@ func TestServer_WithChain(t *testing.T) {
}
}

server := MustNewServer(RestConf{}, WithChain(chain.New(middleware1(), middleware2())))
server := MustNewServer(RestConf{AddNativeMiddlewares: true, Middlewares: MiddlewaresConf{Log: true}}, WithChain(chain.New(middleware1(), middleware2())))
server.AddRoutes(
[]Route{
{