Giter Site home page Giter Site logo

wednesday-solutions / go-template Goto Github PK

View Code? Open in Web Editor NEW
26.0 7.0 9.0 110.95 MB

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

Home Page: https://wednesday.is/building-products

License: MIT License

Go 98.11% Shell 0.97% Dockerfile 0.53% Makefile 0.40%
golang graphql postgres postgresql sqlboiler databases go-boilerplate go-template graphql-relay postgresql-database

go-template's Introduction

Go Template

An enterprise go template 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 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 make setup-precommit 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

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
  1. To run all the migrations using the script setup-local.sh as follows make setup-local.

  2. Generate the graphql models using gqlgen generate

  3. 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

Setting up database (postgres)

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

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

Running migrations

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

File Structure

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

DB Models

generate your database models

sqlboiler psql --no-hooks

Seed your Database

For seeding Your database models use

go run cmd/seeder/exec/seed.go  

Note: We have Seeder directory because we are using it while building docker image for application

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

Schema

  • Schema can generated or altered manually

    Take a look at the following folder

Resolver

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

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.

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.

To deploy

make deploy-ecs name=gotemplate env=dev

Update infrastructure

make update-ecs name=gotemplate env=dev

Testing

Get test coverage using

go test -cover

Generate mocks

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

Postman Collection

The postman collection can be found here and has been auto-generated using graphql-test

License

Go Template is licensed under the MIT license. Check the LICENSE file for details.

go-template's People

Contributors

alichherawalla avatar anasnadeemws avatar dependabot[bot] avatar gang-wednesday avatar himanshu-wedensday avatar ijas-ws avatar praveenchand-wed avatar saurabh-wednesday avatar tushar-ws avatar yash-handa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-template's Issues

Cross platform pre-commit hook setup

I was trying to setup this template by following the getting started on the README.

I tried running pre-commit-setup.sh with bash but it exited with non-zero return code. brew wasn't found on my system.

Is this template expected to be used on MacOS only? If not can we have the pre-commit installation done by pip? I hope python comes by default on MacOS?

Requesting feedback.

Upgrade dependency "github.com/agiledragon/gomonkey"

Background

Repo github.com/wednesday-solutions/go-template depends on github.com/agiledragon/[email protected].

https://github.com/wednesday-solutions/go-template/blob/master/go.mod#L8

However, comparing version v2.2.0 of github.com/agiledragon/gomonkey from proxy.golang.org and github, there are inconsistencies.

commit time of the copy on github.com

"committer": {
      "name": "agiledragon",
      "email": "[email protected]",
      "date": "2021-08-16T15:49:31Z"
    }

commit time of the copy on proxy.golang.org

{"Version":"v2.2.0","Time":"2021-08-13T12:46:58Z"}

So the checksum from the code in github does not match the checksum saved in sum.golang.org. The v2.2.0 tag of github.com/agiledragon/gomonkey might have been retagged after a minor edition on github. I guess you use proxy.golang.org to get dependencies, but that also shows that your project is depending on the copy of github.com/agiledragon/[email protected] before its edition. Depending upon such inconsistent tag version may also result in some unexpected errors as well as build errors due to different proxy settings.

For example, when someone who does not use proxy.golang.org, say GOPROXY=direct, attempts to get github.com/agiledragon/[email protected], the following errors occur.

go: downloading github.com/agiledragon/gomonkey/v2 v2.2.0
go: github.com/agiledragon/gomonkey/v2@v2.2.0: verifying module: checksum mismatch
        downloaded: h1:SXUF5UN7ZH67ORjFK+e1FoFRZczCSVUvwTywNY2+fcg=
        sum.golang.org: h1:QJWqpdEhGV/JJy70sZ/LDnhbSlMrqHAWHcNOjz1kyuI=

SECURITY ERROR
This download does NOT match the one reported by the checksum server.
The bits may have been replaced on the origin server, or an attacker may
have intercepted the download attempt.

For more information, see 'go help module-auth'.

So, this is a reminder in the hope that you can get rid of this unreliable version of project github.com/agiledragon/gomonkey.

Solution

1. Bump the version of dependency github.com/agiledragon/gomonkey

I would recommend bumping the version of github.com/agiledragon/gomonkey to a new release to ensure dependency copy in proxy.golang.org and github in sync.

References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.