- Notifications
You must be signed in to change notification settings - Fork6
Example go rest sql application, use as a template to get started quickly
License
pauljamescleary/go-rest-postgres
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This repository demonstrates how to fully wire up and deploy a Golang REST API with a PostgreSQL database backend.
The application demonstrates:
If you want to use this as a template, here a short guide:
- Modify the
go.modfilemoduleto be the name of your project - Modify the
database/schema.hclfile to meet your database needs - Modify the
docker-compose.ymlfile to use the name of your project - Modify
pkg/common/router/routerand add additional routes as needed - Add your domain models to
pkg/common/modelsdirectory - Add your data access (repository) to the
pkg/common/dbdirectory
In general, when you introduce a new separate domain concept:
- Update the
database/schema.hclfile with new tables - Create a new route handler
pkg/common/handler/xxx.go - Create model types
pkg/common/models/xxx.go - Create a new repository
pkg/common/db/xxx.go - Update the main
pkg/common/router/router.gofile to use your new routes / handlers - Add a new
pkg/common/router/xxx_test.gofile to test your new routes / handlers
If you want to add additional config entries:
- Modify the
Configstruct inpkg/common/config.go - Add a default (typically works locally) value in
cmd/config.yaml - Override the default value through an environment variable at runtime (i.e. not local) as needed
The author of this repository carries opinions about how to best organize an application / repository. Those opinions / choices are described below:
- No ORM (Object Relational Mapper) - Golang has a few ORMs that are popular, in particularGORM andEnt. However, the mapping ability in tools likeScany take care of a lot of tedious work of working with databases (mapping rows to structs). Declaritive SQL in code (imo) is preferable to code generation and clunky SQL-esque APIs.
- Atlas for database migrations. There are 1000 ways to run migrations in golang, and unfortunately there doesn't seem to be a lot of consensus. However, what I enjoy about Atlas is that it includes Terraform support and it allows you to define your schema in an HCL file and it magically figures out how to update your target database. You can see this in
database/schema.hcland just a simpleatlas schema applyand your database is now in sync. - Echo for the REST API - There are a lot of http frameworks in the golang echosystem, includingEcho andGin. Honestly, I found using the docs with Echo simpler, but don't have a strong opinion on the web framework side.
net/httpmight be just as good as the other choices,Ginbeing pretty popular. - PGX for database aaccess -
database/sqlwould be fine as well, but PGX is PostgreSQL optimized and is a good choice when tied to Postgres. - Taskfile instead of Makefile - There is nothing inherently wrong with Makefile, Taskfile is a reasonable alternative with simple, validatable YAML syntax
- Pre-commit - makes sure that users cannot commit / push code that isn't up to standards. In this project we check formatting, linting, golang critic, among others to keep the repo tidy.
- InstallDocker. Used for testing
- InstallTaskfile, required to do pretty much anything
- InstallPre-Commit. This project uses pre-commit to ensure code is all nice and tidy before others can see it.
- Install the pre-commit hooks by running
pre-commit install - Optionally installAtlas. Atlas is used for database migrations.Note: you can skip this step and just rely on docker, as atlas is only needed to explore its abilities
Make sure you have Docker and Taskfile installed...
- Run
task server.run- Starts up the postgres docker database
- Runs migrations so the database is ready
- Performs a build of the REST API
- Starts the REST API on port 1323. Access onhttp://localhost:1323
cmd- this is where the default config and the main app livesdatabase- this is where theschema.hcl file lives. Modify this file to alter the databasepkg- this is where most of the code livescommonconfig- for loading the config file / incorporating environment variable overridesdb- the underlying database in PGX, and domain specific Repositorieshandler- for handling each of the type of echo requests / routesmodels- core domain model classes, surfaced in the API and used in the Repositoriesrouter- where we map echo routes to handlers
This project usesTaskfile for running tasks. The following tasks are available
build- Builds a local executable, outputs to out/bin/gominclean- Cleans up build artifacts, including out, bin, and test reportsd.build- Builds the docker iamge, marks it as latestd.down- Shuts down all docker containers in the docker compose filed.up- Starts up all docker containers, builds and runs the API as welldb.migrate- Runs the database migration, ensures that the local postgres database is runningdb.up- Starts the database WITHOUT migrationsserver.run- Starts the database, runs migrations, builds the server, and starts the servertest- Runs all of the tests in all of the go source directories
This project primarily usesE2E tests hitting the API directly and using the docker postres database.
Tests live inpkg/common/router - create a newxxx_test.go file as needed
This project is setup to load a default config found incmd/config.yaml that is overridable viaEnvironment Variables. For example, you can override the database url by setting an environment variable namedAPP_DB_URL.
- - Simple REST API
- - Add initial Makefile
- - Add pre-commit
- - Initial database setup
- - Incorpoate database into REST API
- - Integration tests for database
- - E2E tests for REST API
- - Add github build for golang
- - Add docker packaging
- - Add docker image build in Github Actions on Tag
- - Terraform plan for RDS
- - Terraform plan for ECS
- - Github Actions to run terraform and deploy to AWS
About
Example go rest sql application, use as a template to get started quickly
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.