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

improve #1144

Closed
n1ce37 opened this Issue Nov 29, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@n1ce37

n1ce37 commented Nov 29, 2018

Generated by Google Translate. I want to do something now and find that iris is hard to do. I have some middlerfunc, such as validation functions. If it fails, I want to directly call the closing function at the end. These functions will also be called after the normal call, such as the logging of some logs. Currently I can't tell which one is doneglobal. At present it seems that all the functions are in a call chain, skip and stopexecuted can not meet my current needs. My implementation is that all functions set the internal code of the program, and then an error directly calls a series of functions at the end.

@n1ce37

This comment has been minimized.

n1ce37 commented Nov 29, 2018

i see apibuilder contain something. it is may be useful.

@kataras

This comment has been minimized.

Owner

kataras commented Nov 29, 2018

You can call any handler from inside other handler, just export it as a function and call it whenever you want it. The ctx.StopExecution will make any ctx.Next to be ignored, this does not mean that it "terminates" your own handler function, you have to do it with with myCloseHandler(ctx) and return. Excuse me if I don't understand correctly your question, code is always the best way to express ourselves, so can I ask you to provide us some example code?

Also, @n1ce37 can you please edit your issue's title with some context of the problem you want help for?

We will be glad to assist you.

@n1ce37

This comment has been minimized.

n1ce37 commented Nov 29, 2018

You can call any handler from inside other handler, just export it as a function and call it whenever you want it. The ctx.StopExecution will make any ctx.Next to be ignored, this does not mean that it "terminates" your own handler function, you have to do it with with myCloseHandler(ctx) and return. Excuse me if I don't understand correctly your question, code is always the best way to express ourselves, so can I ask you to provide us some example code?

Also, @n1ce37 can you please edit your issue's title with some context of the problem you want help for?

We will be glad to assist you.

itis may router set:
app := iris.New()
app.Use(recover.New())
app.Use(token.Verify)

articleRoutes := app.Party("/query/article")
{   
    articleRoutes.GET("/", aa) 
}   
app.DoneGlobal(resp.Resp)

return app 

itis may now jwt verify middlers:
func Verify(ctx iris.Context) {
resp, _ := rpc.Greeter_User.UserAction(context.Background(), &protos.UserRequest{Action: "user/verifylogintoken", RefreshToken: ctx.GetHeader("refresh_token"), AccessToken: ctx.GetHeader("access_token"), UserId: userId})
if resp == nil {
ctx.Values().Set("code", resp.TokenExpired)
handlers := ctx.Handlers()
handlers [len(handlers)-1] (ctx)
return
itis may end func,it will be do some log or metric things in the future
ifunc Resp(ctx iris.Context) {
respData := make(map[string]interface{})

code := ctx.Values().Get("code")
if code != 0 {
    respData["data"] = respCodeMsg[code]
} else {
    respData["data"] = ctx.Values().Get("data")
}
ctx.JSON(data)

} ,
it think it is not graceful:
handlers := ctx.Handlers()
handlerslen(handlers)-1
,but i can not find help in the iris api.
thank u verymuch

@n1ce37

This comment has been minimized.

n1ce37 commented Nov 29, 2018

You can call any handler from inside other handler, just export it as a function and call it whenever you want it. The ctx.StopExecution will make any ctx.Next to be ignored, this does not mean that it "terminates" your own handler function, you have to do it with with myCloseHandler(ctx) and return. Excuse me if I don't understand correctly your question, code is always the best way to express ourselves, so can I ask you to provide us some example code?
Also, @n1ce37 can you please edit your issue's title with some context of the problem you want help for?
We will be glad to assist you.

itis may router set:
app := iris.New()
app.Use(recover.New())
app.Use(token.Verify)

articleRoutes := app.Party("/query/article")
{   
    articleRoutes.GET("/", aa) 
}   
app.DoneGlobal(resp.Resp)

return app 

itis may now jwt verify middlers:
func Verify(ctx iris.Context) {
resp, _ := rpc.Greeter_User.UserAction(context.Background(), &protos.UserRequest{Action: "user/verifylogintoken", RefreshToken: ctx.GetHeader("refresh_token"), AccessToken: ctx.GetHeader("access_token"), UserId: userId})
if resp == nil {
ctx.Values().Set("code", resp.TokenExpired)
handlers := ctx.Handlers()
handlerslen(handlers)-1
return
itis may end func,it will be do some log or metric things in the future
ifunc Resp(ctx iris.Context) {
respData := make(map[string]interface{})

code := ctx.Values().Get("code")
if code != 0 {
    respData["data"] = respCodeMsg[code]
} else {
    respData["data"] = ctx.Values().Get("data")
}
ctx.JSON(data)

} ,
it think it is not graceful:
handlers := ctx.Handlers()
handlerslen(handlers)-1
,but i can not find help in the iris api.
thank u verymuch

beginGlobalHandlers context.Handlers                                                                             
                                                                                                                 
// the per-party done handlers, order matters.                                                                   
doneHandlers context.Handlers                                                                                    
// global done handlers, order doesn't matter.                                                                   
doneGlobalHandlers context.Handlers     

i saw it in the source code, but i can not direct jump

@n1ce37

This comment has been minimized.

n1ce37 commented Nov 29, 2018

beginGlobalHandlers context.Handlers // the per-party done handlers, order matters. doneHandlers context.Handlers // global done handlers, order doesn't matter. doneGlobalHandlers context.Handlers

something like prefix,work,done,all have many handlers,so i meet error can direct jump,thank u for reply

@kataras kataras closed this Dec 8, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment