Skip to content

Instantly share code, notes, and snippets.

@oxlb
Created March 9, 2019 23:01
Show Gist options
  • Select an option

  • Save oxlb/f8c764111cf3a4f1ad55fafde65d84ec to your computer and use it in GitHub Desktop.

Select an option

Save oxlb/f8c764111cf3a4f1ad55fafde65d84ec to your computer and use it in GitHub Desktop.
echo framework header based versioning
package main
import (
"net/http"
"fmt"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
e.Pre(APIVersion);
e.GET("v1/todo", func(c echo.Context) error {
return c.String(http.StatusOK, "TODO v1")
})
e.GET("v2/todo", func(c echo.Context) error {
return c.String(http.StatusOK, "TODO v2")
})
e.Logger.Fatal(e.Start(":1324"))
}
// APIVersion Header Based Versioning
func APIVersion(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
headers := req.Header
apiVersion:= headers.Get("version")
req.URL.Path = fmt.Sprintf("/%s%s",apiVersion, req.URL.Path)
return next(c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment