An enterprise go template application showcasing - Testing strategies, middleware support, and Continuous integration.
___---
We’re always looking for people who value their work, so come and join us. We are hiring!
The Go Template is a template/starter go project.
- GraphQL Relay
- Dockerization
- Authorization middleware
- Redis Cache
- Graphql Subscription
- Paginated endpoints
- Simplified support for migrations and seeders
- DAO layer for all database interactions
- Distributed tracing
- Monitoring
- Alerts
Using go-template requires having Go 1.7 or above. Once you download go-template (either using Git or go get) you need to configure the following:
-
Run the
make setup-precommit
script to install the pre-commit hooks locally. -
Set the ("ENVIRONMENT_NAME") environment variable, either using terminal or os.Setenv("ENVIRONMENT_NAME","dev").
-
Install the sqlboiler, sql-migrate and gqlgen using
go get -v github.com/rubenv/sql-migrate/... \
github.com/volatiletech/sqlboiler \
github.com/99designs/gqlgen
For Go 1.16 or above, you need to install sqlboiler using
go install github.com/volatiletech/sqlboiler/v4@latest
go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latest
For Go 1.18 and above install the sql-migrate using
go install github.com/rubenv/sql-migrate/...@latest
-
To run all the migrations using the script setup-local.sh as follows
make setup-local
. -
Generate the graphql models using
gqlgen generate
-
Run the app using:
go run cmd/server/main.go
NOTE: Please do not delete .env.base
file of the project and rebuild the using docker-compose everytime you
make changes to it
- Requirement postgresql
Steps to set up database with username
and role
using terminal
- Enter postgres terminal
psql postgres
- Create new database
CREATE DATABASE go_template;
- Create a new role with password
CREATE ROLE go_template_role WITH LOGIN PASSWORD 'go_template_role456';
NOTE: Replace these credentials in .env
file of the project
To ease the development process a make file is provided
make docker
Requires.env.local
file to be present and set
This starts the containers inlocal
stage, bind thecurrent directory
to/go/src/server
inside thego-template_server_1
container and then starts theterminal
insidego-template_server_1
. Once the development is over,exit
the terminal and callmake tear env=local
to stop all the containers
Set up signoz locally by following the steps here
Migrations are present in internal/migrations
package. Run below command to run all migrations at once:
sql-migrate up -env postgres
To drop migration use following
sql-migrate down -env postgres -limit=0
To check status of migration use following
sql-migrate new -env postgres <name>
To add new migration use following, it creates a new empty migration template with pattern <current time>-<name>.sql
sql-migrate new -env postgres <name>
append query to above file
For more information on migration package refer here
go-template/
└──.github/
│ └──workflow/go-template-ci.yml # this file contains the config of github action
└──cmd/
│ └──seeder/
│ │ └──v1/1_roles.go # seed file to load roles into DB
│ │ └──v2/2_users.go # seed file to load users into DB
│ └──server/main.go # this is the starting point of the go server
└──daos/ # this directory will hold info about the DB transactions
└──gqlmodels/ # this directory contain modules for gqlgen and is mostly auto-generated
└──internal/
│ └──config/ # this package loads env variables into a config object
│ └──jwt/ # this package has JWT related middlewares and convertors
│ └──middleware/
│ └──auth/
│ └──secure/
│ └──migrations/ # these are the migrations to be applied
│ └──postgres/ # this takes care of connecting to postgre
│ └──server/ # this package have functionality to start a echo server
│ └──services/ # this will have services used in the server
└──models/
└──pkg/
│ └──api/api.go # the starting point of the api
│ └──utl/
│ └──convert/ # this package has functionality for type conversions
│ └──mock/ # this package has mock related to passwords and JWTs
│ └──throttle/ # this package has functionality for request rate throttling
│ └──rediscache/ # this package has functionality for accessing and using redis
│ └──resultwrapper/ # this package exports the custom errors produced by application
│ └──secure/ # this package has password related functionalities
│ └──zap/ # this package has setup for uber-go zap logger
└──resolver/ # this directory will contain resolvers to populate info for graphQL queries, mutations and subscriptions
└──scripts/ # this directory will contain different utility scripts
│ └──setup-local.sh # script to set up database and run the app locally
│ └──setup-ecs.sh # script to provision the ECS infrastructure in the cloud
│ └──update-ecs.sh # script to deploy new version of the app to the provisioned ECS
│ └──setup-pre-commit.sh # script to setup the pre-commit hooks and the enviornment
│ └──line-formatter.sh # auto format to adhere to the lll.line-length criteria
└──schema/ # this directory will have all the .graphql files which make the graphql api
└──.env.local # a sample .env file for reference
└──.env.base # a base .env file should be included in all environments
└──.pre-commit-config.yaml # config to run pre-commit utility
└──docker-compose.*.yml # docker-compose file corresponding to the state of project (local, prod, test)
└──docker-compose.yml # docker-compose file which serves as a base to other docker-compose files
└──generate-modules.sh # script to generate modules
└──gqlgen.yml # file to configure gqlgen
└──makefile
└──migrate-run.sh # script to run DB migrations
└──setup-local.sh # a helper script to setup local env (do migration, etc)
└──sqlboiler.toml # sqlboiler config file
└──test.sh # a helper script to run test in local env
generate your database models
sqlboiler psql --no-hooks
For seeding Your database models use
go run cmd/seeder/main.go ## to build the execs for seeding
go run cmd/seeder/exec/seed.go ## to seed
Note: We have Seeder directory because we are using it while building docker image for application
generate the graphql models from the database schema
gqlgen generate
-
Graphql endpoint
POST
request/graphql
-
Playground endpoint for schema
/playground
Take a look at the following file
-
Schema can generated or altered manually
Take a look at the following folder
-
Queries and mutation are autogenerated using gqlgen and are to be extended. Take a look at the following files
- Please ensure the maximum limit of number of vpc's in a region has not reached it's limit.
- The maximum limit of number of buckets has not reached it's limits.
- Please make the changes to the manifest file of the service.
- Ensure that the aws cli has been installed and configured with the appropriate credentials and profiles.
Application name should container only lowercase letters. No hyphens and underscores or any other special characters.
make setup-ecs name=gotemplate env=dev
Please change the ENV_INJECTION variable as true in .env.base file to true, so it will not try to find a local .env file
Also add the environment variables to the task,add this block of yml code in ${service name}/manifest.yaml:
variables:
ENVIRONMENT_NAME: develop
#to inject our .env file from aws s3 inside the container
taskdef_overrides:
- path: ContainerDefinitions[0].EnvironmentFiles[0]
value:
type: "s3"
value: "arn:aws:s3:::gotemplate-dev-bucket/develop/.env"
Make sure that the manifest.yml has http.path: '/'
http:
# Requests to this path will be forwarded to your service.
# To match all requests you can use the "/" path.
path: "/"
# You can specify a custom health check path. The default is "/".
# healthcheck: '/'
Also make sure the execution role has an appropriate policy attached to it so it can access our .env file inside the s3 bucket, and inject it as environment variables.
make deploy-ecs name=gotemplate env=dev
make update-ecs name=gotemplate env=dev
Get test coverage using
go test -cover
Install Mockgen Using
go install github.com/gleisonmv/mockgen@latest
Sample command to generate mocks
mockgen --build_flags=--mod=mod github.com/go-playground/validator FieldError
The postman collection can be found here and has been auto-generated using graphql-test
Go Template is licensed under the MIT license. Check the LICENSE file for details.