An open API service indexing awesome lists of open source software.

https://github.com/wednesday-solutions/go-template-mysql

An enterprise GraphQL template application built using Golang showcasing - Testing Strategy, DB migrations and seeding, integration with an ORM, containerization using Docker, GraphQL Interface, MySQL, subscriptions, redis caching, paginated endpoints.
https://github.com/wednesday-solutions/go-template-mysql

go-boilerplate go-production go-production-template go-template graphql mysql sqlboiler

Last synced: 6 months ago
JSON representation

An enterprise GraphQL template application built using Golang showcasing - Testing Strategy, DB migrations and seeding, integration with an ORM, containerization using Docker, GraphQL Interface, MySQL, subscriptions, redis caching, paginated endpoints.

Awesome Lists containing this project

README

          







Go template MySQL




An enterprise Go template MySQL application showcasing - Testing strategies, middleware support, and Continuous integration.



___




Expert teams of digital product strategists, developers, and designers.









___

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.

## Out of the box support for

- 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

## Getting started

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:

1. Run the `./scripts/setup-pre-commit.sh` script to install the pre-commit hooks locally.

2. Set the ("ENVIRONMENT_NAME") environment variable, either using terminal or os.Setenv("ENVIRONMENT_NAME","dev").

3. Install the sqlboiler, sql-migrate and gqlgen using
```
go get -v github.com/rubenv/sql-migrate/... \
github.com/volatiletech/sqlboiler \
github.com/99designs/gqlgen
```

4. To run all the migrations using the script setup-local.sh as follows `./scripts/setup-local.sh`.

5. Generate the graphql models using `gqlgen generate`

6. Run the app using:

```bash
go run cmd/server/main.go
```

# Setting up database (mysql)

- Requirement [mysql](https://dev.mysql.com/doc/mysql-getting-started/en/)

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

# Using Docker

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 in `local` stage, bind the `current directory` to `/go/src/server` inside the `go-template_server_1` container and then starts the `terminal` inside `go-template_server_1`. Once the development is over, `exit` the terminal and call `make tear env=local` to stop all the containers

# Setting up Signoz

Set up signoz locally by following the steps [here](https://signoz.io/docs/install/docker)
# Running migrations

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
```
To add new migration use following, it creates a new empty migration template with pattern `-.sql`
```
sql-migrate new -env mysql
```
append query to above file

For more information on migration package refer [here](https://github.com/rubenv/sql-migrate)

# File Structure

```txt
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
```

# DB Models

generate your database models
```
sqlboiler mysql --no-hooks
```

# graphQL

generate the graphql models from the database schema
```
gqlgen generate
```

## API (for graphQL to operate)

- Graphql endpoint ```POST``` request ```/graphql```

- Playground endpoint for schema ```/playground```

Take a look at the following file

- [pkg/api/api.go](pkg/api/api.go)

## Schema

- Schema can generated or altered manually

Take a look at the following folder

- [schema](./schema/)

## Resolver

- Queries and mutation are autogenerated using gqlgen and are to be extended. Take a look at the following files

- [resolver](./resolver)

## Infrastructure

### Create infrastructure

### Precautions
1. Please ensure the maximum limit of number of vpc's in a region has not reached it's limit.
2. The maximum limit of number of buckets has not reached it's limits.
3. Please make the changes to the manifest file of the service.
4. 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: '/'

```
### To deploy

```
./scripts/deploy-ecs.sh gotemplate dev
```

### Update infrastructure

```
./scripts/update-ecs.sh gotemplate dev
```
## Testing

Get test coverage using
```
go test -cover
```

## Generate mocks

Sample command to generate mocks

```
mockgen --build_flags=--mod=mod github.com/go-playground/validator FieldError
```

## Postman Collection

The postman collection can be found [here](postman/collection.json) and has been auto-generated using [graphql-test](https://www.npmjs.com/package/graphql-testkit)
## License

Go template MySQL is licensed under the MIT license. Check the [LICENSE](LICENSE) file for details.