summaryrefslogtreecommitdiff
path: root/context.go
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-12-02 16:26:47 +0800
committerEltrac <hi@guhub.cn>2025-12-02 16:26:47 +0800
commit8aba8708896dd36c87ba470a560cfe36d530ee7d (patch)
tree2ffacf546921cb06fa3af512bd5ddcc5f59ad96c /context.go
parent447479c1fc0cb1c7ed99f2bbab63e2e05c00e959 (diff)
feat: support Cookies
Diffstat (limited to 'context.go')
-rw-r--r--context.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/context.go b/context.go
index 271b886..e0de660 100644
--- a/context.go
+++ b/context.go
@@ -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)
+}