An enterprise Go template MySQL 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 MySQL 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
./scripts/setup-pre-commit.sh
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
-
To run all the migrations using the script setup-local.sh as follows
./scripts/setup-local.sh
. -
Generate the graphql models using
gqlgen generate
-
Run the app using:
go run cmd/server/main.go
- Requirement mysql
Steps to set up database with username
and role
using terminal
- Enter mysql terminal ``mysql -uroot -p```
- Create new database
CREATE DATABASE go_template;
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 mysql
To drop migration use following
sql-migrate down -env mysql -limit=0
To check status of migration use following
sql-migrate new -env mysql <name>
To add new migration use following, it creates a new empty migration template with pattern <current time>-<name>.sql
sql-migrate new -env mysql <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
│ └──mysql/ # 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
└──.pre-commit-config.yaml # config to run pre-commit utility
└──dbconfig.yml
└──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 mysql --no-hooks
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.
./scripts/setup-ecs.sh gotemplate dev
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: '/'
./scripts/deploy-ecs.sh gotemplate dev
./scripts/update-ecs.sh gotemplate dev
Get test coverage using
go test -cover
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 MySQL is licensed under the MIT license. Check the LICENSE file for details.