This boilerplate is designed to provide a quick start for production ready web application development using the Echo framework in Go. It includes implementations for CRUD operations, JWT authentication, user roles, and more.
This boilerplate supports Create, Read, Update, and Delete operations for various models. The CRUD functionality is implemented using a clean architecture approach with separate layers for routing, controllers, and models.
- Router: Defines all the application routes and middleware.
- Controller: Handles the business logic and communicates between the router and models.
- Model: Represents the data structure and provides functions to interact with the database.
Users can authenticate themselves using a login endpoint that requires a username and password. Passwords are securely hashed using bcrypt before being stored in the database.
JWT (JSON Web Token) is used to authenticate API requests. Middleware is employed to protect routes and ensure that only authenticated users can access certain endpoints.
{
"name": "test",
"id": 2,
"role": "USER",
"exp": 1716461438
}
User roles (e.g., Admin, User) are implemented to manage permissions and access control within the application. Middleware checks user roles to authorize access to specific routes.
const (
Admin UserRole = "ADMIN"
Moderator UserRole = "MODERATOR"
User UserRole = "USER"
)
All API responses are wrapped in a standard JSON format, including status codes, messages, and data payloads, ensuring consistency and ease of use for frontend applications.
{
"Success": true,
"Message": "Success",
"Data": [
{
"blogs": [
{
"ID": 1,
"CreatedAt": "2024-05-22T22:41:11.616688Z",
"UpdatedAt": "2024-05-22T22:41:11.616688Z",
"DeletedAt": null,
"UserId": 2,
"Title": "test1",
"Content": "content1"
},
{
"ID": 2,
"CreatedAt": "2024-05-23T12:50:46.833646Z",
"UpdatedAt": "2024-05-23T12:50:46.833646Z",
"DeletedAt": null,
"UserId": 2,
"Title": "test2",
"Content": "content2"
}
]
}
]
}
Environment variables are used to configure the application, such as database connection strings, JWT secret keys, and other settings. The config.json
file is utilized to manage these variables.
Environment variables
{
"Db": {
"Host": "localhost",
"Port": "5432",
"Name": "test",
"User": "postgres",
"Password": "123",
"SslMode": "disable"
},
"Jwt": {
"SecretKey": "secret_key",
"ExpTime": 3
}
}
implement like this using viper
in main.go
func initConfig() error {
viper.AddConfigPath("config")
viper.SetConfigName("config")
return viper.ReadInConfig()
}
//... somewherre in your code
// example loading database config envs
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s",
viper.GetString("Db.Host"),
viper.GetString("Db.Port"),
viper.GetString("Db.Name"),
viper.GetString("Db.User"),
viper.GetString("Db.Password"),
viper.GetString("Db.SslMode"),
)}), &gorm.Config{})
A Postman collection (_postman
folder) is provided to facilitate testing of the API endpoints. It includes pre-configured requests for all routes and an environment setup to manage variables like base URL and authentication tokens.
- Go (version 1.23+)
- Postman (optional, for testing API endpoints)
- Clone the repository:
git clone https://github.com/altynboy/go-echo-boilerplate.git cd go-echo-boilerplate
- Install dependecies
go mod tidy
- Setup env variables in
config.json
{ "Db": { "Host": "localhost", "Port": "5432", "Name": "test", "User": "postgres", "Password": "123", "SslMode": "disable" }, "Jwt": { "SecretKey": "secret_key", "ExpTime": 3 } }
- Run the application
go run main.go