Skip to content

A production-ready Go Echo boilerplate for quick web app development. Includes CRUD operations, JWT authentication, and user roles.

Notifications You must be signed in to change notification settings

Altynboy/go-echo-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Echo Boilerplate

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.

Table of Contents

Features

CRUD Operations

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-Controller-Model Structure

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

Authorization using Login Password

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 Authentication using Middleware

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
}

Roles for Users

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"
)

JSON Response Wrapper

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

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{})

Postman Collection with Environment Setup

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.

Getting Started

Prerequisites

  • Go (version 1.23+)
  • Postman (optional, for testing API endpoints)

Installation

  1. Clone the repository:
    git clone https://github.com/altynboy/go-echo-boilerplate.git
    cd go-echo-boilerplate
  2. Install dependecies
    go mod tidy
  3. 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
      }
    }
  4. Run the application
    go run main.go

About

A production-ready Go Echo boilerplate for quick web app development. Includes CRUD operations, JWT authentication, and user roles.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages