Golang with Postgres and Nginx
Unfortunately, as of October 2022, this example application has been reported to us as broken. We will fix the example presented here to work with the new software release(s) shortly.
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 Release.
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 Release 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.
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.
We build a Docker image for our Nginx server with a straightforward Dockerfile.
Docker Compose
Our backend, Nginx, and PostgreSQL are orchestrated via docker-compose
.
Run the project locally
To run the codebase on your development machine, ensure you have Docker installed.
Run the following command via the terminal:
Once the application has started, you can run the following command to check if everything is working:
Lastly, to stop and remove the containers created by docker-compose
, run:
Deploy to Release
To deploy this application to Release, log in to your Release 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, Release 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.
Release 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.
Release 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.
Last updated