Skip to content

QGB/heroku-go-db-example

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Heroku Go Database Example

This is a simple example of a PostgreSQL-backed Go API application running on Heroku

Components

heroku-go-db-example         -- Directory / repository name
├── .env.dev                 -- Environment variables for local development
├── Makefile                 -- Targets for local development (optional)
├── Procfile                 -- Defines a single `web` process
├── README.md                -- This file
├── bin
│   └── heroku-go-db-example -- The locally-built binary (gitignored)
├── main.go                  -- The main Go application
└── vendor
    ├── github.com
    │   └── lib
    │       └── pq           -- Vendored dependency [1]
    └── vendor.json          -- Buildpack identification / Heroku configuration [2]

Running Locally

When the application is deployed, the Heroku Go buildpack will run go install . to build and install your application to /app/bin. We want to mimic this in local development, so the run target in the Makefile will:

  1. Copy .env.dev to .env to make environment variables available to the application
  2. Create the local database
  3. Compile main.go to bin/heroku-go-db-example -- this is important because Go will use the pacakge name (heroku-go-db-example) when creating the binary.
  4. Prepend the ./bin directory to the $PATH and run heroku local. This allows the web process in the Procfile to find the heroku-go-db-example binary.

This is all done for you by running make since run is the default target.

Deploying

$ heroku apps:create heroku-go-db-example
$ heroku addons:create heroku-postgresql
$ git push heroku master

Testing

GET:  curl -s https://heroku-go-db-example.herokuapp.com | jq .
POST: curl -s -d '{"username":"reagent"}' https://heroku-go-db-example.herokuapp.com | jq .
404:  curl -i -X OPTIONS https://heroku-go-db-example.herokuapp.com
404:  curl -i https://heroku-go-db-example.herokuapp.com/unknown

Links

About

Using a DB w/ Heroku & Golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 72.2%
  • Makefile 26.1%
  • Procfile 1.7%