Echo 웹 프레임워크에서 헤더 작성하는 방법 정보
Echo 웹 프레임워크에서 헤더 작성하는 방법관련링크
본문
헤매다가 방법을 발견해서 씁니다.
일단은 2가지가 있습니다.
1가지는 전체 페이지에 헤더를 주는 법.
func HeaderSet(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { c.Response().Header().Set("Expires", "0") c.Response().Header().Set(echo.HeaderLastModified, time.Now().Format("Mon, 02 Dec 2019 09:56:15 GMT")) // Mon, 02 Dec 2019 09:56:15 GMT c.Response().Header().Set("Cache-Control", "no-store, no-cache, must-revalidate") c.Response().Header().Set("Cache-Control", "pre-check=0, post-check=0, max-age=0") c.Response().Header().Set("Pragma", "no-cache") c.Response().Header().Set(echo.HeaderContentType, "text/html; charset=utf-8") c.Response().Header().Set("X-Robots-Tag", "noindex") return next(c) } }
라는 함수를 작성 후
e := echo.New() e.Use(HeaderSet)
로 미들웨어를 추가하여 쓸 수 있습니다.
만약 e.Use 를 사용하여 헤더를 작성했을 경우
전체 페이지에 해당 헤더가 추가됩니다.
2번째 방법으로는 각 HTTP Request (맞는지 모르겠네요)에 헤더를 따로 추가하는 방법입니다.
e.POST("/install/1", InstallProcess) // 인스톨 프로세스
라고 했다면 InstallProcess 는 핸들러 함수가 됩니다.
func InstallProcess(c echo.Context) error { c.Response().Header().Set("Expires", "0") c.Response().Header().Set(echo.HeaderLastModified, time.Now().Format("Mon, 02 Dec 2019 09:56:15 GMT")) // Mon, 02 Dec 2019 09:56:15 GMT c.Response().Header().Set("Cache-Control", "no-store, no-cache, must-revalidate") c.Response().Header().Set("Cache-Control", "pre-check=0, post-check=0, max-age=0") c.Response().Header().Set("Pragma", "no-cache") c.Response().Header().Set(echo.HeaderContentType, "text/html; charset=utf-8") c.Response().Header().Set("X-Robots-Tag", "noindex") c.Response().WriteHeader(http.StatusOK)
이 핸들러 함수에
c.Response().Header().Set(키, 값)
c.Response().WriteHeader(HTTP 상태 코드 값)
을 넣어준다면 각 HTTP Request 마다 헤더를 따로 작성할 수 있습니다.
이거때문에 다 갈아엎을까 생각하다가 겨우 찾았네요 ㅎㅎ
추천
0
0
댓글 0개