Search…
Golang with Postgres and Nginx

Overview

In this walkthrough, we’ll look at the code and configuration required to deploy a Go server that connects to a Postgres database, running behind an Nginx proxy on ReleaseHub.
You can find the completed code for the project here.

Fork the project

Fork the project on GitHub to create a copy of the repository. Your fork of the project will allow you to integrate it with ReleaseHub using your account, and experiment and make changes without affecting the original project.

Project structure

Our example project uses docker-compose to orchestrate our backend, Nginx, and Postgres.

The backend

The backend is a simple Go server, using the Gorilla web toolkit to serve JSON content over HTTP. It connects to a PostgreSQL database, which stores the data that is served at / via the backend.
We have a simple route handler, anchored at /, which executes a blogHandler function.
func blogHandler(w http.ResponseWriter, r *http.Request) {
db, err := connect()
if err != nil {
w.WriteHeader(500)
return
}
defer db.Close()
rows, err := db.Query("SELECT title FROM blog")
if err != nil {
w.WriteHeader(500)
return
}
var titles []string
for rows.Next() {
var title string
err = rows.Scan(&title)
titles = append(titles, title)
}
json.NewEncoder(w).Encode(titles)
}
func main() {
...
r := mux.NewRouter()
r.HandleFunc("/", blogHandler)
log.Fatal(http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stdout, r)))
}
The blog handler queries the database, and returns the query result as JSON.
Additionally, on startup, the backend will bootstrap the required tables and data into the database.

Nginx

Our Nginx server is configured to listen on port 80 and forward any requests to the backend service.
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend:8000;
}
}
We build a Docker image for our Nginx server with a straightforward Dockerfile.
FROM nginx:1.13-alpine
COPY conf /etc/nginx/conf.d/default.conf

Docker Compose

Our backend, Nginx, and PostgreSQL are orchestrated via docker-compose.
services:
backend:
build: backend
secrets:
- db-password
depends_on:
- db
db:
image: postgres
restart: always
secrets:
- db-password
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=example
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
proxy:
build: proxy
ports:
- 80:80
depends_on:
- backend
volumes:
db-data:
secrets:
db-password:
file: db/password.txt

Run the project locally

To run the codebase on your development machine, ensure you have Docker installed.
Run the following command via the terminal:
docker-compose up
Once the application has started, you can run the following command to check if everything is working:
$ curl localhost:80
["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"]
Lastly, to stop and remove the containers created by docker-compose , run:
docker-compose down

Deploy to ReleaseHub

To deploy this application to ReleaseHub, log in to your ReleaseHub account and navigate to the dashboard.
From the dashboard, select the Create New App button. This will take you to the Create Your Application screen.
Refresh the repository list, and select the forked version of the nginx-golang-postgres repository from the dropdown.
Using the docker-compose configuration, ReleaseHub will be able to automatically detect the services in the codebase.
Select a name for your application, and click Generate App Template. It's a good idea to keep the name short but descriptive, as it will be used in your hostnames for this application.
ReleaseHub will then allow you to modify the Application Template that was generated. For the purposes of this application, nothing needs to be modified here, so click Save & Continue.
Lastly, before building and deploying the application, you can configure any build arguments, environment variables, or Just-In-Time File Mounts. Once again, we don't need to make any changes here, so click Start Build & Deploy.
ReleaseHub will kick off a build and deployment for your application. This shouldn’t take more than a few minutes for this particular example.
Once our application has been successfully deployed into an environment, we can hover over the proxy badge on the environments tab. This badge indicates that the proxy service configured in our docker-compose.yaml file has a host name URL that we can access.
Clicking on this badge will take us to our live application, where we should see the response from our backend being routed via Nginx.