Creating Restful Services with Go and Couchbase
Written by Ryan Moore on 22 May 2014
Tweet
In this post we will talk about using Couchbase with Go, but this time by writing a Restful interface. This is a continuation of Couchbase with Golang.
We'll be using the great Gorilla Mux library for handling the HTTP
requests. While you don't have to use the Gorilla Mux library, it provides a nice way of handling urls that contain parameters which the default Golang HTTP Handlers don't do out of the box. Using Gorilla Mux is really easy. Create the router, add your handlers and you are off and running.
Each handler is written using Go's default net/http
handler functions. To get the id parameter from the "/users/{id}"
url Gorilla Mux provides a mux.Vars
method which makes it easy to retrieve.
Now that the basics of using Gorilla Mux are out of the way, let's throw in Couchbase and actually create a data driven Restful service. We'll first start by retrieving data from Couchbase. Then we'll write new data.
As was shown in Couchbase with Golang, getting data from Couchbase is pretty straight forward. So lets start by getting back data on a specific user. We'll update the example from above to return back a user struct from couchbase.
For the most part the code above is pretty straight forward. Get the id
parameter. Get the user
. Write it out using the built in Go json encoder. The one thing that is kind of weird is the error handling. Currently the Couchbase API does not provide a way to inspect the result from the Get
method call to see if the id
we passed in exists. That's what the strings.Contains(err.Error(), "KEY_ENOENT")
is all about. It's inspecting the error message that Couchbase returns to see if the reason for the error is KEY_ENOENT
which means the id
does not exist and we should return a HTTP 404
instead of a HTTP 500
error message.
Next, we'll add the ability to POST
a new user to the UsersHandler
. Then we'll modify the UsersHandler
into separate handlers so that GET
and POST
are processed in different handlers. To avoid confusion I want to make it explicit which Handler we are modifying. We are not modifying the UserHandler
from above but the UsersHandler
. This is important because when we do the POST
we do not want to post to /users/{identifier}
. We want to POST
to /users
which is the UsersHandler
.
No real gotchyas here. Again due to all the nice json handling code built into Go we were able to Decode the json payload {"id": "2", "name": "UserName"}
into a User struct and then Add it to Couchbase. We added some extra code to inform a user with a HTTP 409 Conflict
if they are attempting to create a user with a duplicate id
.
Finally, lets clean up the UsersHandler
to remove the switch
statement so that the POST
is in it's own handler. To do this we'll use one more feature from Gorilla Mux which makes removing the switch
statement easy. First we'll modify the code that registers the handlers to the following.
We add a new handler that will only handle POST
and modify the origial to only handle GET
. Then we split the code in the UsersHandler
into two handlers.
And with that we are able to create new users, display them, all with different handlers. I hope this has been helpful for those who are starting out writing Rest interfaces with Couchbase and Go. The final code for this post is hosted on Github here. In the next post we'll look at making our code testable.
comments powered by Disqus