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

Latest comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.