Skip to content

runemadsen/ok-go

Repository files navigation

Ok Go Web Template

Ok Go

A Golang web application template with most of the stuff you've come to enjoy from Ruby on Rails. For more information of why I built this, read the blog post. Pull requests are more than encouraged! I've tried to comment the code as much as possible.

Features

The following features are a part of the template. You will probably end up using only a selected handful of them for your specific projects.

  • Middleware support with Negroni. The template has a single custom middleware handler to serve as inspiration, but more can be added easily if needed.

  • Routing with Gorilla Mux. You can replace this with your favorite Golang routing library if needed. This is your controllers folder in Rails.

  • Database ORM support with Gorm. The template ships with a simple Go struct and some basic CRUD routes.

  • Database migrations with Gomigrate and Gofer for easy command line syntax.

  • Template rendering with Render. This is your views folder in Rails.

  • Asset pipeline with Gulp. Although not specifically a Golang setup, it generates digested .coffee and .scss assets, as well as a manifest.json with the file paths. A Golang asset_path helper is available in the templates. Digested assets are disabled in development mode for ease of development.

  • Configuration is stored in the OS environment, with support for development (.env) and test (.env.test) environments with Godotenv.

  • Testing with Ginkgo and Gomega. Golang prefers the tests to live alongside the actual files, and will automatically ignore files named *_test.go during compilation. This template has both simple route tests and model tests, as well as some nice helper functions to access the HTTP responses like you know it from RSpec.

Getting Started

All of the folders have README files to describe their purpose.

Development

This guide assumes that you have a working Go environment, and Postgres running on port 5432.

  1. Create your dev and test databases. createdb okgo_dev; createdb okgo_test
  2. Clone this repo to your GOPATH/src folder. git clone git@github.com:runemadsen/ok-go.git github.com/runemadsen/ok-go
  3. Create a .env file and a .env.test file in the new folder root. This will be the place to put environment specific variables. You only need one for now: DATABASE_URL=postgres://MYUSER@localhost:5432/okgo_ENVIRONMENT?sslmode=disable
  4. Install all imported packages with go get, and a few extra command-line requirements:
    • go get -u github.com/codegangsta/gin
    • go get -u github.com/chuckpreslar/gofer/gofer
    • sudo npm install -g gulp && npm install

That's it. If you start using this template for your own application, you might want to rename the runemadsen/ok-go packages to your own repository name.

To run the development server, run gulp server and open localhost:3000 in your browser. Gin will recompile the app code on any changes to .go files, but does currently not support watching .html template files. Gulp will recompile the assets on any changes.

Test

Run ginkgo -r from the root folder to run the tests.

Heroku

This template is built to just work on Heroku. Right now, it requires you to commit the golang dependencies and precompiled assets to the Git repo.

  1. heroku create -b https://github.com/kr/heroku-buildpack-go.git
  2. Add a database to your new heroku server.
  3. heroku config:set GO_ENV=production
  4. godep save
  5. gulp assets:precompile
  6. git add .; git commit -m 'adding compiled assets and dependencies'
  7. git push heroku master
  8. heroku run gofer db:migrate