Created
March 9, 2019 23:01
-
-
Save oxlb/f8c764111cf3a4f1ad55fafde65d84ec to your computer and use it in GitHub Desktop.
echo framework header based versioning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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