Skip to content

Latest commit

Β 

History

History
191 lines (149 loc) Β· 4.25 KB

File metadata and controls

191 lines (149 loc) Β· 4.25 KB

Echo-Middleware

Reference : Echo Doc

각 Middleware 의 세뢀적인 λ‚΄μš©μ€ κ°œλ³„μ˜ ν¬μŠ€νŒ…μ—μ„œ λ‹€λ£° 것이닀.

Table of Contents

3 Levels

μ—μ½”μ—μ„œ μ œκ³΅ν•˜λŠ” 미듀웨어λ₯Ό μ„Έ κ°€μ§€ 레벨둜 λ‚˜λˆŒ 수 μžˆλ‹€.

  1. Group Level
  2. Root Level
  3. Route Level

Group Level

μƒˆ 그룹을 μƒμ„±ν•˜μ—¬ νŠΉμ • κ·Έλ£Ήμ—κ²Œλ§Œ middleware κ°€ 적용되게 μ„€μ •.

e := echo.New()
admin = e.Group("/admin", middleware.BasicAuth())

↑ return to TOC

Root Level

Root λ ˆλ²¨μ€ Router κ°€ 'μ‹€ν–‰λ˜κΈ° 이전'κ³Ό 'μ‹€ν–‰λœ ν›„' λΌλŠ” 두 그룹으둜 λ‚˜λ‰œλ‹€.

  • Before Router
  • After Router

Before 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

↑ return to TOC

After Router

이 λ ˆλ²¨μ—μ„œ 미듀웨어λ₯Ό 등둝할 μ‹œμ—λŠ” λŒ€λΆ€λΆ„μ˜ 경우 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

↑ return to TOC

Route Level

μƒˆ Route λ₯Ό μ •μ˜ν•¨μ„ 톡해 미듀웨어λ₯Ό νŠΉμ • ν•Έλ“€λŸ¬μ—κ²Œλ§Œ μ μš©μ‹œν‚¬ 수 μžˆλ‹€.

e := echo.New()
e.GET("/", <HandlerA>, <MiddlewareB>)
  • HandlerA μ—κ²Œλ§Œ MiddlewareB μ μš©μ‹œν‚€κΈ°

↑ return to TOC

Skipping Middleware

νŠΉμ • 쑰건에 해당될 λ•Œ 미듀웨어λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  μŠ€ν‚΅ν•˜λ„λ‘ μ„€μ • ν•  수 μžˆλ‹€.

  • μŠ€ν‚΅μ‹œμ—λŠ” Skipper ν•¨μˆ˜λ₯Ό μ‚¬μš©.
    Skipper func(echo.Context) bool
    Default 값은 false 둜 μ„€μ •λ˜μ–΄ μžˆλ‹€. true 일 μ‹œ Processing 되고 μžˆλŠ” 미듀웨어λ₯Ό skip.
// 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
    },
}))

↑ return to TOC