diff options
| author | Eltrac <hi@guhub.cn> | 2025-12-02 16:26:47 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-12-02 16:26:47 +0800 |
| commit | 8aba8708896dd36c87ba470a560cfe36d530ee7d (patch) | |
| tree | 2ffacf546921cb06fa3af512bd5ddcc5f59ad96c /context.go | |
| parent | 447479c1fc0cb1c7ed99f2bbab63e2e05c00e959 (diff) | |
feat: support Cookies
Diffstat (limited to 'context.go')
| -rw-r--r-- | context.go | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -6,6 +6,8 @@ import ( "strconv" ) +/* === Writer Functions */ + // Plain writes plain text to ResponseWriter // with code and string passed as parameters // this automatically sets Content-Type to "text/plain; charset=utf-8" @@ -77,3 +79,35 @@ func (c *Context) Forbidden(msg string) { func (c *Context) InternalError(msg string) { c.Error(http.StatusInternalServerError, msg) } + +/* Cookies */ + +func (c *Context) Cookies() []*http.Cookie { + return c.R.Cookies() +} + +func (c *Context) Cookie(name string) *http.Cookie { + for _, cookie := range c.R.Cookies() { + if cookie.Name == name { + return cookie + } + } + return nil +} + +func (c *Context) SetCompleteCookie(cookie *http.Cookie) { + http.SetCookie(c.W, cookie) +} + +func (c *Context) SetCookie(name string, value string) { + cookie := http.Cookie{ + Name: name, + Value: value, + Path: "/", + MaxAge: 3600, + HttpOnly: true, + Secure: true, + SameSite: http.SameSiteLaxMode, + } + c.SetCompleteCookie(&cookie) +} |
