VividCortex / siesta

Composable framework for writing HTTP handlers in Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pull common code into Siesta?

JnBrymn opened this issue · comments

In an internal repo we have a post hook for writing a JSON response. I wonder if something like this should be pulled into an util directory within siesta. Then I could just specify that this code be used with service.AddPost(siesta.JsonResponseWriter). Then, by reading the JsonResponseWriter documentation I know that I can use status_code and response - life would be easy.

This is an opensource repository.

I thought about doing that, but there are still some questions to be answered. I wouldn't want to enforce the use of certain context keys, for example, so how would we handle that?

Maybe using closures?

func JsonResponseWriter(contextKey string) func(Context, http.ResponseWriter, *http.Request) {
    return func(c siesta.Context, w http.ResponseWriter, r *http.Request, done func()) {
        // write a response using the contextKey
    }
}

and then use it with

service.AddPost(siesta.JsonResponseWriter("my_response"))

Hm. I like!