Title: Leveraging Gorilla Mux for a Webserver with Postgres Date: 2018-02-26 19:19 Tags: go, golang, web, gorilla mux, postgres, pg, psql, http, httptest
# install docker in order to trivially run postgres locally
go get github.com/lib/pq github.com/gorilla/mux
sudo ./start-db.sh
sudo ./create_tables.sh
sudo ./psql.sh
\l
\dt+
\d+
\d+ TABLENAME
./run.sh
(which just executes something like go run main.go db.go note.go)
curl localhost:8080
<html><body>index web page</body></html>
curl -I -X GET localhost:8080
HTTP/1.1 200 OK
Date: Sun, 15 Apr 2018 17:48:47 GMT
Content-Length: 40
Content-Type: text/html; charset=utf-8
curl -X POST --header "Content-Type: application/json" --data '{"note":"some note text"}' localhost:8080/note
sudo ./psql.sh
Unit testing should not require external dependencies, the -short command can still skip "long" unit tests
go test
or go test -v
Since the integration tests expect to actively use a real database there is an environment variable that tells the system how to initialize
Otherwise the integration tests are skipped, no database needed!
./integration_tests.sh
(or expandeded into TEST_INTEGRATION=true go test -v
)
Environment variables (for overrides) and the default:
"TEST_DB_HOST", "127.0.0.1"
"TEST_DB_PORT", "5432"
"TEST_DB_SSL", "disable"
"TEST_DB_USERNAME", "myuser"
"TEST_DB_PASSWORD", "mypassword"
"TEST_DB_NAME", "mydb"
The application has different parts in order to separate concerns:
start-db.sh and create-tables.sh are helpers to setup a local dev environment using docker
- tables.sql is the database schema
- main.go initializes and starts the application
- db.go abstracts the persistence layer connection
- notes.go represents an example "model" (like MVC or https://en.wikipedia.org/wiki/Data_access_object)
- router.go manage the routes to the web application
- controller-.go are the definitions of handlers for each route
- _test.go are test files