Reference : Echo Doc
κ° Middleware μ μΈλΆμ μΈ λ΄μ©μ κ°λ³μ ν¬μ€ν μμ λ€λ£° κ²μ΄λ€.
- 3 Levels
- Group Level
- Before Router
- HTTPSRedirect
- HTTPSWWWRedirect
- WWWRedirect
- NonWWWRedirect
- AddTrailingRedirect
- AddTrailingSlash
- RemoveTrailingSlash
- MethodOverride
- Rewrite
- After Router
- BodyLimit
- Logger
- GZip
- Recover
- BasicAuth
- JWTAuth
- Secure
- CORS
- Static
- Before Router
- Root Level
- Route Level
- Group Level
- Skipping Middleware
μμ½μμ μ 곡νλ λ―Έλ€μ¨μ΄λ₯Ό μΈ κ°μ§ λ λ²¨λ‘ λλ μ μλ€.
- Group Level
- Root Level
- Route Level
μ κ·Έλ£Ήμ μμ±νμ¬ νΉμ κ·Έλ£Ήμκ²λ§ middleware κ° μ μ©λκ² μ€μ .
e := echo.New()
admin = e.Group("/admin", middleware.BasicAuth())Root λ 벨μ Router κ° 'μ€νλκΈ° μ΄μ 'κ³Ό 'μ€νλ ν' λΌλ λ κ·Έλ£ΉμΌλ‘ λλλ€.
- Before Router
- After Router
μ΄ λ 벨μμ λ―Έλ€μ¨μ΄λ₯Ό λ±λ‘ν μμλ Pre() ν¨μλ₯Ό μ¬μ©νλ€.
// E.g.
import "github.com/labstack/echo/middleware"
e := echo.New()
e.Pre(middleware.HTTPSRedirect())-
HTTPSRedirect
http μμ²μ https λ‘ λ¦¬λ€μ΄λ νΈhttp://github.com/8luebottle
β β β β β β β Redirect β β β β β β β
https://github.com/8luebottle// E.g. e := echo.New() e.Pre(middleware.HTTPSRedirect())
-
HTTPSWWWRedirect
http μμ²μ www https λ‘ λ¦¬λ€μ΄λ νΈ.http://github.com/8luebottle
β β β β β β β Redirect β β β β β β β
https://www.github.com/8luebottle// E.g. e := echo.New() e.Pre(middleware.HTTPSWWWRedirect())
-
WWWRedirect
λ°μ μμ²μ http www λ‘ λ¦¬λ€μ΄λ νΈ.http://github.com/8luebottle
β β β β β β β Redirect β β β β β β β
http://www.github.com/8luebottle// E.g. e := echo.New() e.Pre(middleware.WWWRedirect())
-
NonWWWRedirect
λ°μ μμ²μ https λ‘ λ¦¬λ€μ΄λ νΈ. (w/o www)http://www.github.com/8luebottle
β β β β β β β Redirect β β β β β β β
https://github.com/8luebottle// E.g. e := echo.New() e.Pre(middleware.NonWWWRedirect())
-
AddTrailingRedirect
-
AddTrailingSlash
-
RemoveTrailingSlash
-
MethodOverride
-
Rewrite
μ΄ λ 벨μμ λ―Έλ€μ¨μ΄λ₯Ό λ±λ‘ν μμλ λλΆλΆμ κ²½μ° Use() ν¨μλ₯Ό μ¬μ©νλ€.
μ΄κ³³μμ λ±λ‘λ λ―Έλ€μ¨μ΄λ λΌμ°ν° μ΄ν(after)μ μ€νλκ² λλ€.
// E.g.
import "github.com/labstack/echo/middleware"
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())μ΄ λ 벨λ¨μ λ΄μ₯λμ΄ μλ λ―Έλ€μ¨μ΄λ μλμ κ°λ€.
- BodyLimit
- Logger
- GZip
- Recover
- BasicAuth
- JWTAuth
- Secure
- CORS
- Static
μ Route λ₯Ό μ μν¨μ ν΅ν΄ λ―Έλ€μ¨μ΄λ₯Ό νΉμ νΈλ€λ¬μκ²λ§ μ μ©μν¬ μ μλ€.
e := echo.New()
e.GET("/", <HandlerA>, <MiddlewareB>)HandlerAμκ²λ§MiddlewareBμ μ©μν€κΈ°
νΉμ 쑰건μ ν΄λΉλ λ λ―Έλ€μ¨μ΄λ₯Ό μ¬μ©νμ§ μκ³ μ€ν΅νλλ‘ μ€μ ν μ μλ€.
- μ€ν΅μμλ
Skipperν¨μλ₯Ό μ¬μ©.Default κ°μ false λ‘ μ€μ λμ΄ μλ€. true μΌ μ Processing λκ³ μλ λ―Έλ€μ¨μ΄λ₯Ό skip.Skipper func(echo.Context) bool
// E.g.
e := echo.New()
e.User(middleware.LoggerWithConfig(middleware.LoggerConfig{
Skipper: func(c echo.Context) bool {
if strings.HasPrefix(c.Request().Host, βlocalhostβ){
return true
}
return false
},
}))