loading...

API versioning in golang ECHO

krishnakummar profile image krishna 惻1 min read
package main

import (
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New() 

    v1 := e.Group("/v1")
    groupV1Routes(v1)

    v2 := e.Group("/v2")
    groupv2Routes(v2)

    e.Logger.Fatal(e.Start(":8090"))
}

func groupV1Routes(e *echo.Group) {
    grA := e.Group("/groupa")
    grA.GET("/event", handlerGet1)

    grB := e.Group("/groupb")
    grB.PATCH("/:id", handlerPost)
}

func groupv2Routes(e *echo.Group) {
    grA := e.Group("/groupa")   
    grA.GET("/event", handlerGet2)

}

func handlerGet1(c echo.Context) error {
    return c.String(http.StatusOK, "THIS IS VERSION 1!")
}

func handlerGet2(c echo.Context) error {
    return c.String(http.StatusOK, "THIS IS VERSION TWO")
}

func handlerPost(c echo.Context) error {
    return c.String(http.StatusOK, c.Param("id"))
}

Discussion

pic
Editor guide