-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_utils.go
99 lines (83 loc) · 2.6 KB
/
handler_utils.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
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
package attache
import (
"encoding/json"
"fmt"
"log"
"net/http"
"path/filepath"
"runtime"
)
// Error is the equivalent of calling ErrorMessage with an empty message
func Error(code int) { ErrorMessage(code, "") }
// ErrorFatal logs err and then calls Error(500)
func ErrorFatal(err error) {
_, file, line, ok := runtime.Caller(1)
if ok {
log.Printf("fatal: %s:%d %s", filepath.Base(file), line, err)
} else {
log.Println("fatal: (unknown loc)", err)
}
Error(500)
}
// ErrorMessage immediately terminates the executing handler chain with
// the given status code and status text
func ErrorMessage(code int, msg string, args ...interface{}) {
panic(httpResult{
code: code,
msg: fmt.Sprintf(msg, args...),
})
}
// ErrorMessageJSON immediately terminates the executing handler chain with
// the given status code and a json body containing the status text
func ErrorMessageJSON(code int, msg string, args ...interface{}) {
panic(httpResult{
code: code,
msg: fmt.Sprintf(msg, args...),
json: true,
})
}
// Success immediately terminates the executing handler chain with
// a 200 OK
func Success() { Error(200) }
// RedirectPage immediately terminates the executing handler chain with a
// 303 (See Other)
func RedirectPage(path string) {
panic(http.RedirectHandler(path, http.StatusSeeOther))
}
// RedirectPermanent immediately terminates the executing handler chain with a
// 308 (Permanent Redirect)
func RedirectPermanent(path string) {
panic(http.RedirectHandler(path, http.StatusPermanentRedirect))
}
// RedirectTemporary immediately terminates the executing handler chain with a
// 307 (Temporary Redirect)
func RedirectTemporary(path string) {
panic(http.RedirectHandler(path, http.StatusTemporaryRedirect))
}
// RenderHTML renders the view with the given name from the ViewCache attached
// to ctx using data as the Execute's data argument.
// The rendered template is written to w with a Content-Type of "text/html".
// ErrorFatal is called for any error encountered
func RenderHTML(ctx interface {
Context
HasViews
}, name string) {
buf := getbuf()
defer putbuf(buf)
if err := ctx.Views().Get(name).Execute(buf, ctx); err != nil {
panic(err)
}
ctx.ResponseWriter().Header().Set("content-type", "text/html")
buf.WriteTo(ctx.ResponseWriter())
}
// RenderJSON marshals data to JSON, then writes the data to w with a
// Content-Type of "application/json". ErrorFatal is called for any
// error encountered
func RenderJSON(w http.ResponseWriter, data interface{}) {
buf, err := json.Marshal(data)
if err != nil {
ErrorFatal(err)
}
w.Header().Set("content-type", "application/json")
w.Write(buf)
}