고랭지농업
Golang

Echo 웹 프레임워크에서 헤더 작성하는 방법

· 2019-12-03 (화) 00:03:23 · 3063

헤매다가 방법을 발견해서 씁니다.

 

일단은 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 마다 헤더를 따로 작성할 수 있습니다.

 

이거때문에 다 갈아엎을까 생각하다가 겨우 찾았네요 ㅎㅎ

댓글을 작성하시려면 로그인이 필요합니다.

소모임

23,243건
+
분류 제목 글쓴이 날짜 조회
Golang 19-12-04 조회 2,182
모형취미 19-12-04 조회 1,415
파이썬 게시판 만들기 19-12-04 조회 2,706
컴퓨터 19-12-04 조회 2,041
컴퓨터 19-12-04 조회 2,150
파이썬 게시판 만들기 19-12-04 조회 1,215
모형취미 19-12-03 조회 1,341
컴퓨터 19-12-03 조회 1,990
컴퓨터 19-12-03 조회 1,820
소모임 신청 19-12-03 조회 2,742
파이썬 게시판 만들기 19-12-03 조회 2,765
Golang 19-12-03 조회 1,576
파이썬 게시판 만들기
[파이썬 게시판 만들기]
19-12-03 조회 1,204
Golang 19-12-03 조회 1,526
Golang 19-12-03 조회 3,064
파이썬 게시판 만들기
[파이썬 게시판 만들기]
19-12-03 조회 1,309
음악/악기 19-12-02 조회 1,522
출산육아 19-12-02 조회 1,435
모형취미 19-12-02 조회 1,442
Golang 19-12-02 조회 1,426
Golang 19-12-02 조회 1,997
Golang 19-12-02 조회 1,406
Golang 19-12-02 조회 2,812
컴퓨터 19-12-02 조회 2,099
영화가좋다
[영화가좋다]
19-12-02 조회 2,196
영화가좋다 19-12-02 조회 2,708
호스팅정보 19-12-02 조회 2,024
서버관리자 19-12-01 조회 3,096
서버관리자 19-12-01 조회 1,600
소모임 신청 19-12-01 조회 3,182