Sunday, November 16, 2014

Routing for a Go Web Project

From a python programmer's perspective, these are lessons I've learned from developing a web app using Go.

After I've planned my project structure for my go app, I started to look into how I should go about developing the app itself. I read the detailed tutorial from the golang wiki wiki site. Although it seemed simple enough to build a web app, I immediately recognized some things I would miss from the python/django world. For example, I didn't want to have code in my view to parse and validate the URLs. I started to look around for a solution and came across Gorilla Toolkit, specifically the mux package. It provided functionality that I was more accustomed to when I was using Django.
In the case of capturing values from the URL or applying a regex to the URL, you would specify routes as:
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
In the ArticleHandler function, you can retreive the value of "category" or "id" by:
vars := mux.Vars(request)
category := vars["category"]
id := vars["id"]
You can also restrict access to a view handler by specifying the request to use a specific method:
r.Methods("GET", "POST")
or requiring the request to use specific headers:
r.Headers("X-Requested-With", "XMLHttpRequest")
You can view the mux documentation all the functionality available. In general, this package helps remove a lot of boilerplate code from the view functions and makes a Django developer feel more at home when building a go web app.

Thursday, November 6, 2014

Layout Structure for a Go Web Project

I wanted to create my first Go web application on App Engine but wasn't sure how to structure my project.

The first step in laying out my project involved reading the following articles:
I wanted to follow the guidelines as described in the "How to Write Go Code" document but also create my project in a way that would be easy to build a web application that can be deployed on App Engine. I decided to split my website into two git repositories.
The first repo would be the website itself and all associated code. This app is responsible for handling requests and saving data to the datastore. The second repository would contain my business logic that would be used for the website as a library. The reason for separating the business logic is that the library may be used in other applications, other than a web app.
I eventually ended up with the following project structure:
├── projects
    ├── bin
    ├── pkg
    ├── src
            ├── alexchan
                ├── careermatchme
                │   ├── setup.txt
                │   └── web
                │       ├── app.go
                │       ├── app.yaml
                │       ├── static
                │       │   ├── css
                │       │   ├── img
                │       │   └── js
                │       └── templates
                │           ├── 404.html
                │           ├── 500.html
                │           ├── base.html
                │           └── jobsearch
                │               ├── about.html
                │               ├── includes
                │               │   ├── pagination.html
                │               └── search
                │                   ├── form.html
                │                   └── results.html
                └── jobs
                    ├── jobs.go
                    └── jobs_test.go
The top-level directory, projects, is where my Go workspace and is where my GOPATH environment variable is set to. My two repositories are under and This allowed me to follow to conventions of described in the first article while setting up my environment to also develop and deploy for App Engine.