The fastest way to build a restful api with Echo with a structured project that using PostgreSQL db and JWT base authentication middleware.
This project ships following features as default
- ORM Integration
- Easy Database Migration
- Authentication Using Jwt
- Easy dotenv Management
- Easy to Mock all Interfaces
- CORS Configuration
Inspired from Gin boilerplate.
- gorm: ORM library for Golang
- jwt-go: JSON Web Tokens (JWT) as middleware
- mockery: Go test mock library
- godotenv: go dotenv library
- Go Modules
- Built-in CORS Middleware
- Built-in RequestID Middleware
- Feature PostgreSQL 12
- Environment support
- Unit test
$ go get github.com/triaton/go-echo-boilerplate
$ cd $GOPATH/src/github.com/triaton/go-echo-boilerplate
$ go mod init
$ go install
Rename .env.example to .env and place your database credentials and jwt secret key
$ mv .env.example .env
$ go run main.go
$ go build -v
$ ./go-echo-boilerplate
$ go test -v ./...
$ chmod +x generate-test-coverage.sh
$ ./generate-test-coverage.sh
This will generate cover.html
with detailed coverage result.
$ mockery --all --keep-tree
For more information please visit this link.
To add a controller
- Add
*_controller.go
file - Define controller struct, define route entries, define route handlers and request dto interfaces. Please refer
auth/auth_controller.go
. - Add controller object to
routes/api.go
...
func DefineApiRoute(e *echo.Echo) {
controllers := []common.Controller{
auth.AuthController{},
blogs.BlogsController{},
// add newly added controller here
}
var routes []common.Route
...
To add a service
- Add
*_service.go
file. - Define service interface, define service methods as well as singleton methods.
- Please don't forget to add
set
method. It will be used when mocking service methods.
To add db models
- add directory
models
and define*.model.go
file. - Define model struct and include
models.Base
. Please referblogs/models/blog.model.go
.
Edit database/migrations.go
to add a new migration. For detailed guide, please visit this link.
Download Postman -> Import -> Import From Link https://www.getpostman.com/collections/401dc48dd6e9b15cc287
You are welcome to contribute to keep it up to date and always improving!
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.