4

I am starting building a api in go(golang), but I have few questions...

So in my main function or init function(because I might use appengine) I was thinking in calling a function which will define all my routes using gorilla mux. Each pice of my application(post, comments etc...) will have its one package with its structures/methods/functions.

Questions:

  1. Because I was thinking in defining the routes in one function, do I need to import in this file all my packages, to send the requests to the right handlers?

  2. What about helper function, for example I would like to set content type of the response to be application/json for all the handlers where this is necessary, how I will be able to do that?

I'm not looking for frameworks, just some pointer about how can I overcome those questions in golang way.

RockNinja
  • 2,179
  • 4
  • 16
  • 16
  • 1
    I think you probably need to call `Register` from a `main`-like package that imports all your view-containing packages (and isn't imported by them), or even from various packages' `init()`s (but eh, I see the appeal to organizing your routes in one place). Up to a certain size of app, if your post/comment/etc. views are closely integrated they might not need to be different packages anyway. Here's my answer to a related SO Q about package layout: http://stackoverflow.com/questions/20380333/cyclic-dependencies-and-interfaces-in-golang/20394211#20394211 – twotwotwo Sep 11 '14 at 22:33
  • Seems like the JSON content-type/encoding thing is basically a utility function might be imported by packages that define views. So: routes on "top", utils on "bottom." There was actually a great blog post about this (something like "how we use Go without a framework") that covered how they deal with this and several other little things; I can't find it, though. – twotwotwo Sep 11 '14 at 22:39
  • If anyone *can* find that "how we use Go without a framework" post I'd love you forever. I sort-of recall something about them writing their own wrapper to do JSON en/decoding, but I really can't find it now. – twotwotwo Sep 11 '14 at 23:20

1 Answers1

4

If you define all of the routes in a single function, then the file containing this function will need to import the packages that implement the handlers. The only way to refer to a type or function in another package is to import the package.

Here's a helper for setting the content type and encoding a value to JSON:

func JSONHandler(f func(w http.ResponseWriter, r *http.Request) interface{}) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    v := f(w, r)
    if v != nil {
        w.Header().Set("Content-Type", "application/json")
        if err := json.NewEncoder(w).Encode(v); err != nil {
            log.Println(err)
        }
    }
  })
}

The argument to this function is a function that returns a value to encode to the response as JSON. For example, this function returns the client's user agent as JSON.

func UserAgentHandler(w http.ResponseWriter, r *http.Request) interface{} {
  return struct { UserAgent string }{ req.Header.Get("User-Agent") }
}

Use the following code to register this handler with the Gorilla mux r:

r.Handle("/user-agent", JSONHandler(UserAgentHandler))

There are many ways to improve JSONHandler.

Simon Fox
  • 5,995
  • 1
  • 18
  • 22