summaryrefslogtreecommitdiff
path: root/context.go
blob: 6241179007abaed09710eef41a695fdf4f821f88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package growel

import (
	"encoding/json"
	"net/http"
	"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"
func (c *Context) Plain(code int, data string) {
	c.W.Header().Set("Content-Type", "text/plain; charset=utf-8")
	c.W.WriteHeader(code)
	c.W.Write([]byte(data))
}

// JSON receives a map or struct and converts it to JSON
// then writes it to ResponseWriter with code passed as parameter
// this automatically sets Content-Type to "application/json; charset=utf-8"
func (c *Context) JSON(code int, data any) {
	c.W.Header().Set("Content-Type", "application/json; charset=utf-8")
	c.W.WriteHeader(code)

	enc := json.NewEncoder(c.W)
	_ = enc.Encode(data)
}

// XML is unfinished and works almost the same as Plain()
// but it sets Content-Type to "application/xml; charset=utf-8"
func (c *Context) XML(code int, data string) {
	c.W.Header().Set("Content-Type", "application/xml; charset=utf-8")
	c.W.WriteHeader(code)
	c.W.Write([]byte(data))
}

// Error writes an error response to ResponseWriter
// with code and message passed as parameters
// this automatically sets Content-Type to "application/json; charset=utf-8"
//
// Response is a JSON string and contains 3 values:
// error, message, status
func (c *Context) Error(code int, msg string) {
	c.JSON(code, map[string]string{
		"error":   http.StatusText(code),
		"message": msg,
		"status":  strconv.Itoa(code),
	})
}

// NotFound is a shortcut to write 404 Error to response writer
// this is based on Error function
func (c *Context) NotFound(msg string) {
	c.Error(http.StatusNotFound, msg)
}

// BadRequest is a shortcut to write 400 Error to response writer
// this is based on Error function
func (c *Context) BadRequest(msg string) {
	c.Error(http.StatusBadRequest, msg)
}

// Unauthorized is a shortcut to write 401 Error to response writer
// this is based on Error function
func (c *Context) Unauthorized(msg string) {
	c.Error(http.StatusUnauthorized, msg)
}

// Forbidden is a shortcut to write 403 Error to response writer
// this is based on Error function
func (c *Context) Forbidden(msg string) {
	c.Error(http.StatusForbidden, msg)
}

// InternalError is a shortcut to write 500 Error to response writer
// this is based on Error function
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)
}