-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
51 lines (45 loc) · 1.48 KB
/
base.go
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
package core
import (
"net/http"
"time"
"github.com/Z-M-Huang/Tools/data"
"github.com/Z-M-Huang/Tools/utils"
"github.com/gin-gonic/gin"
)
//WriteResponse Write api response
func WriteResponse(c *gin.Context, code int, obj interface{}) {
switch c.NegotiateFormat(gin.MIMEJSON, gin.MIMEXML, gin.MIMEYAML, gin.MIMEPlain) {
case gin.MIMEJSON:
c.JSON(code, obj)
case gin.MIMEXML:
c.XML(code, obj)
case gin.MIMEYAML:
c.YAML(code, obj)
default:
c.String(http.StatusNotAcceptable, "Not Acceptable")
}
}
//WriteUnexpectedError Write unexpected api response
func WriteUnexpectedError(c *gin.Context, response *data.PageResponse) {
response.SetAlert(&data.AlertData{
IsDanger: true,
Message: "Um... Your data got eaten by the cyber space... Would you like to try again?",
})
WriteResponse(c, http.StatusInternalServerError, response)
}
//GetResponseInContext get response struct from context
func GetResponseInContext(contextKey map[string]interface{}) *data.PageResponse {
response := contextKey[utils.ResponseCtxKey]
if response == nil {
return nil
}
return response.(*data.PageResponse)
}
//SetCookie sets cookie
func SetCookie(c *gin.Context, cookieName, cookieVal string, expiresAt time.Time, httpOnly bool) {
if data.Config.HTTPS {
c.SetCookie(cookieName, cookieVal, int(expiresAt.Sub(time.Now()).Seconds()), "/", data.Config.Host, true, httpOnly)
} else {
c.SetCookie(cookieName, cookieVal, int(expiresAt.Sub(time.Now()).Seconds()), "/", data.Config.Host, false, httpOnly)
}
}