golax icon indicating copy to clipboard operation
golax copied to clipboard

Interceptors in methods?

Open fulldump opened this issue 10 years ago • 0 comments

If an interceptor should affect only a method (or two methods, for example GET and PUT):

var my_interceptor := &golax.Interceptor{
    Before: func (c *golax.Context) {
        if "GET" == c.Request.Method || "PUT" == c.Request.Method {
            // Do things
        }
    },
}

If we implement the "MethodInterceptor":

a := golax.NewApi()
a.Root.
    MethodInterceptor("GET", func (c *golax.Context) {}, &golax.Interceptor{}).
    Method("POST", func (c *golax.Context) {}).
    Node("...")

Or even worse: we will lose node chaining:

a := golax.NewApi()
a.Root.
    Node("a").
    Method("GET", func (c *golax.Context){}).
    Interceptor(func (c *golax.Context{}).   // interceptor added to method GET
    Interceptor(func (c *golax.Context{}).   // other interceptor added to method GET
    Doc(golax.Doc{}).  // Documentation added to method GET
    BackNode().  // Node a again
    Node("b").   // Nodes chain: /a/b

Here is a good proposal!

a := golax.NewApi()

a.Root.
    Node("a").
    Doc(golax.Doc{Description: "Documentation for node 'a' "}).
    Interceptor(interceptor_one).   // Interceptor one belongs to 'a'
    Interceptor(interceptor_two).   // Interceptor two belongs to 'a'
    Method("GET", func (c *golax.Context) ).   // GET /a
    Doc(golax.Doc{Description: "Documentation for node a, method GET}).
    Interceptor(interceptor_X).    // Interceptor X belongs to node 'a', method 'GET'
    Interceptor(interceptor_three).    // Interceptor three belongs to node 'a', method 'GET'
    Method("POST", func (c *golax.Context) ).  // POST /a
    Doc(golax.Doc{Description: "Documentation for node a, method POST}).
    Interceptor(interceptor_X).    // Interceptor X belongs to node 'a', method 'POST'
    Interceptor(interceptor_four).    // Interceptor four belongs to node 'a', method 'POST'
    // HERE IS THE THING:  
    // `Node` method inside a `Method` has a implicit `BackNode`
    Node("b").   // Node 'b' is attached to node 'a'
    Interceptor(interceptor_seven).   // Interceptor seven belongs to node '/a/b'
    Method("GET", ....

fulldump avatar Mar 05 '16 10:03 fulldump