DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

krishna
krishna

Posted on

API versioning in golang ECHO

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

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: