Example of creating REST API with Echo framework

Supported Go versions

As of version 4.0.0, Echo is available as a Go module.
Therefore a Go version capable of understanding /vN suffixed imports is required:

  • 1.9.7+
  • 1.10.3+
  • 1.11+

Any of these versions will allow you to import Echo as github.com/labstack/echo/v4 which is the recommended
way of using Echo going forward.

For older versions, please use the latest v3 tag.

Feature Overview

  • Echo API Example

Example

package main

import (
	"github.com/labstack/echo"
	"forum-api/routers"
)

func main() {
	e := echo.New()
	routers.InitRoutes(e)
	e.Logger.Fatal(e.Start(":8080"))
}

Routing

package routers

import (
	"github.com/labstack/echo"
	handler "forum-api/handler"
)

func InitRoutes(e *echo.Echo) {
	e.GET("/group", handler.NewGroupHandler().List())
	e.GET("/group/:identify", handler.NewGroupHandler().Show())
}

Handler


type GroupHandler struct {
	BaseHandler
}

func NewGroupHandler() *GroupHandler {
	return &GroupHandler{}
}

func (h *GroupHandler) List() echo.HandlerFunc {
	return func(c echo.Context) error {
		groups, _, _ := models.NewGroups().List(25, 1)
		c.Response().Header().Set("Server", "4Rum")
		c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
		return json.NewEncoder(c.Response()).Encode(groups)
	}
}

Model

package models

import "database/sql"

type Groups struct {
	ID              uint16 `json:"id"`
	Name            string `json:"name"`
	Provider        string `json:"provider"`
	ProviderID      string `json:"provider_id"`
	MemberNumber    string `json:"member_number"`
	Cover           string `json:"cover"`
	Identity        string `json:"identity"`
	DetaDescription string `json:"meta_description"`
}

func NewGroups() *Groups {
	return &Groups{}
}

func GetSQLList() string {
	return "SELECT id, name, provider, provider_id, member_number, cover, identity, meta_description FROM groups WHERE status = ?"
}

func (this *Groups) List(listRows int, status ...int) (groups []Groups, rows int64, err error) {
	var selectStmt *sql.Stmt
	DB := getConnection()
	statusType := 1;
	rows = 0;	

	if len(status) > 0 {
		statusType = status[0]
	}

	selectStmt, err = DB.Prepare(GetSQLList())	
	results, err := selectStmt.Query(statusType)
	
	for results.Next() {
		group := new(Groups)
		err = results.Scan(&group.ID, &group.Name, &group.Provider, &group.ProviderID, &group.MemberNumber, &group.Cover, &group.Identity, &group.DetaDescription)
		groups = append(groups, *group)
		rows++;
	}
	
	return groups, rows,  err
}

Refer: