postgres-api

command module
v0.0.0-...-c74eed5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2017 License: MIT Imports: 14 Imported by: 0

README

postgres-api : a very simple example of a golang API on top of a Postgres table

GoDoc Go Report Card

Overview

This API uses:

  • Julien Schmidt's httprouter package. In contrast to the default mux of Go's net/http package, this router supports variables in the routing pattern and matches against the request method. The router is optimized for high performance and a small memory footprint.
  • Negroni middleware package. Negroni is an idiomatic approach to web middleware in Go. It is tiny, non-intrusive, and encourages use of net/http Handlers.
  • Godotenv package loads env vars from a .env file. Storing configuration in the environment is one of the tenets of a twelve-factor app. But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Godotenv load variables from a .env file into ENV when the environment is bootstrapped.
  • Envdecode package. Envdecode uses struct tags to map environment variables to fields, allowing you you use any names you want for environment variables. In this way you load the environment variables into a config struct once and can then use them throughout your program.
  • Errors package. The errors package allows you to add context to the failure path of your code in a way that does not destroy the original value of the error.

This program is written for go 1.8 and takes advantage of the ability to drain connections and do a graceful shutdown.

Install

❯ go get github.com/dstroot/postgres-api
❯ cd $GOPATH/src/github.com/dstroot/postgres-api
❯ dep ensure -update
❯ go test -v
❯ go build && ./postgres-api

License

MIT.

Operating

You need a postgres database and a database created to use. Set the .env parameters to point to your postgres installation. Run go test to initialize the table. After that you should be able to build and run the program.

Run psql cli:

$ docker run -it --rm --link postgres:postgres postgres psql -h postgres -U postgres

Here's how you create a database in Postgres:

psql (9.6.2)
Type "help" for help.

postgres=# CREATE DATABASE products;
CREATE DATABASE
postgres=# \q

How to build the program:

$ go build -ldflags "-X main.buildstamp=`date -u '+%Y-%m-%d_%I:%M:%S%p'` -X main.commit=`git rev-parse HEAD` -w -s" && ./postgres-api

Or, just (without build flags)

$ go build && ./postgres-api
References

Documentation

Overview

Package main initializes the application and runs it using go1.8 features to stop it gracefully and allow connections to drain.

When it starts it logs it's hostname and what port it is listening on as well as the go version it was built with and the git commit and build time (if those were provided using ldflags). For example:

`$ go build -ldflags "-X main.buildstamp=`date -u '+%Y-%m-%d_%I:%M:%S%p'` -X main.commit=`git rev-parse HEAD` -w -s"`

Main uses a signal channel to listen for OS signals and will gracefully wait for connections to drain and then stop when it receives a signal to stop, or it will force a shutdown in 5 seconds.

It also has an error channel where it recieves errors from the http server and log them.

Directories

Path Synopsis
Package app contains our application and initialization function.
Package app contains our application and initialization function.
Package handlers contains our route handlers.
Package handlers contains our route handlers.
middleware
Package model contains our API models
Package model contains our API models
Package routes contains our API routes.
Package routes contains our API routes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL