Skip to content

sbk0716/sample-echo-rest-api

Repository files navigation

Build Status codecov License: MIT Go Reference GoReportCard Badge Total alerts

Echo REST API boilerplate

A Golang restful API boilerplate based on Echo framework v4. Includes tools for module generation, db migration, authorization, authentication and more. Any feedback and pull requests are welcome and highly appreciated. Feel free to open issues just for comments and discussions.

HOW TO USE THIS TEMPLATE

DO NOT FORK this is meant to be used from Use this template feature.

  1. Click on Use this template
  2. Give a name to your project
    (e.g. my_awesome_project recommendation is to use all lowercase and underscores separation for repo names.)
  3. Wait until the first run of CI finishes
    (Github Actions will process the template and commit to your new repo)
  4. Then clone your new project and happy coding!

NOTE: WAIT until first CI run on github actions before cloning your new project.

Features

  • User Auth functionality (Signup, Login, Forgot Password, Reset Password, 2FA) using Firebase Auth
  • REST API using labstack/echo.
  • DB Migration using golang-migrate/migrate.
  • Modular structure.
  • Configs via environmental variables.
  • Unit tests.
  • Dependency injection using uber-go/dig.
  • Role based access control using Open Policy Agent.
  • Module generation, quickly create model, usecase, api handler.
  • CLI support. try: go run ./tools/mod/ gen using spf13/cobra.
  • Generate API docs using swaggo. Try: make docs.

Running the project

  • Make sure you have docker installed.
  • Copy .env.example to .env.docker
  • Run docker compose up -d.
  • Go to localhost:8088 to verify if the API server works.

Swagger Docs

To create swagger api documentation you just have to run make docs.
after the command executed successfully, run the app and open http://localhost:8088/docs/index.html to see swagger documentation page.

Environment variables

By default, when you run application with make run-api command, it will look at $HOME/.env for exporting environment variabels. Setting your config as Environment Variables is recommended as by 12-Factor App.

Variables Defined in the project
Name Type Description Example value
DATABASE_URL string Data source URL for main DB postgres://world:hello@postgres/echo_rest_api
PORT integer Http port (accepts also port number only for heroku compability) 8088
AUTO_MIGRATE boolean Enable run migration every time the application starts true
ENV string Environment name development
REDIS_URL string Optional redis://redis:6379
AUTH_PROVIDER string Optional firebase_auth
FIREBASE_CREDENTIALS json firebase json admin key {firebase_admin_key}
FIREBASE_AUTH_CREDENTIALS json filebase json auth key {firebase_auth_key}
KRATOS_API_ENDPOINT string [DEPRECATED] Public endpoint of Kratos http://kratos:4433/
KRATOS_WEBHOOK_API_KEY string [DEPRECATED] Api key for Kratos integration very-very-very-secure-api-key

Commands

Command Description
make run-api Start REST API application
make build-api Build application binary
make setup Run commands to setup development env
make run-db Run DB docker container on local
go run ./tools/mod/ gen Generate module component codes. e.g: go run ./tools/mod/ gen -n Booking
make migration-create [migration_name] Create migration files. migration_name should be snake case
make git-hooks Setup git hooks
make routes Generate routes file for authorization
make docs Generate API docs

Folder Structure

.
├── 3rd-parties         # Thirdparty configs
├── cmd
│   └── api             # Main package of API service
├── config              # Application configs struct
│   ...        
├── docs                # Content documentation and PlantUML for charts and diagrams
├── domains
├── infrastructure
├── migrations
│   └── sql             # Migration files
├── modules
│   ├── core            # Core module, includes apis: users, orgs
│   ├── projects        # Demo module generation
│   └── shared          # To store common usecases and domains which shared between modules
├── out                 # Output folder of PlantUML
├── pkg
│   ├── authz           # Contents Rego rule files for RBAC
│   ├── constants
│   ├── cue             # Contents cue files for data validation
│   ...
│   └── utils           # Contents helper functions
├── tests
└── tools
   ├── modtool         # Module generation
   ├── routes          # Generate routes file for Authorization
   └── scripts         # Some helpful bash commands

Open Source Refs

Contributing

Please open issues if you want the template to add some features that is not in todos.

Create a PR with relevant information if you want to contribute in this template.

TODOs

  • Update docker compose for ory/kratos.
  • Update README.md.
  • Update API docs.
  • Write more tests.