Skip to content

adityasatrio/golang-echo-boilerplate

Repository files navigation

Micro Go Template

Boilerplate template for backend project using go language for optimize and efficient resources.

Contents

Getting started

Install and generate ORM

First before we run the application, lets fresh create model schema using ent:

  1. Get dependency for golang ent
go get entgo.io/ent/cmd/ent
  1. Create new model schema, the generated model located on ent/schema/model_name.go
    https://entgo.io/docs/schema-def for schema documentation
go run entgo.io/ent/cmd/ent init {model_name}
  1. Generate assets
go generate ./ent

Install and generate mock using Mockery

generate mock file for all interface in domains. Install mockery first

go install github.com/vektra/mockery/v2@v2.32.0

then generate mock using below command

 mockery --all --dir internal/applications --output mocks --keeptree --packageprefix mock_

Generate Dependency Injection

  1. Install google wire CLI
go install github.com/google/wire/cmd/wire@latest
  1. Add wire on your $PATH, so we can use wire CLI on every project
  2. Create {domains}_injector.go in your feature directory
  3. Then Run wire using makefile
>> make wire
>> Enter directory: 
>> A_templates_directory

OpenAPI Docs and Swagger

Steps to generate OpenAPI Docs and use via Swagger UI:

  1. Install swag command into your local machine:
    go install github.com/swaggo/swag/cmd/swag@latest
  2. Annotate each controller with Declarative Comment Format
  3. Update and synchronize the cmd/docs module with your update:
    make swagger
  4. Restart the service and access Swagger UI at http://localhost:8888/:app-name/swagger/index.html

Migration

generate migration, up, down and status

  1. Run the command to download and install the migration library.
go install github.com/pressly/goose/v3/cmd/goose@latest
  1. Run the command for migration creation, this will create the migration file version
make migration-create name={name_migration} type={go|sql}
  1. Run the command for migration up, this command will execute the generated migration files
make migration-up
  1. Run the command for migration down, this command will execute rollback migration based on version of your migration file
make migration-down
  1. Run the command for migration status, we can check status the migration using this command
make migration-status

Run config

  • Default general config is located in root-project-path/.env
  • For credential the default config is located in root-project-path/secret.env
    • We can use custom path on running the executable application ./main -credentials-path="ABC" -credentials-name="XYZ"

Build project

using Makefile to run the project. make all will execute schema ent generation, mockery for mock test, swagger docs, testing project and build then run project

make all

Example code

  • health check example using MVC pattern - health
  • System parameter for feature flag as CRUD example using MVC & repository pattern - system_parameter
  • Transaction CRUD example using ent go - service
  • Publish and subscribe using rabbitmq - [add link]

References project

List of project features

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages