Skip to content

Interceptors in methods? #15

@fulldump

Description

@fulldump

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", ....

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions