forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.go
68 lines (48 loc) · 1.3 KB
/
controllers.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
package controllers
import (
"encoding/json"
"fmt"
"net/http"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/convox/rack/api/httperr"
)
func awsError(err error) string {
if ae, ok := err.(awserr.Error); ok {
return ae.Code()
}
return ""
}
func GetForm(r *http.Request, name string) string {
r.ParseMultipartForm(4096)
if len(r.PostForm[name]) == 1 {
return r.PostForm[name][0]
} else {
return ""
}
}
func RenderError(rw http.ResponseWriter, err error) *httperr.Error {
rw.Write([]byte(fmt.Sprintf(`{"error":%q}`, err.Error())))
return httperr.Server(err)
}
func RenderJson(rw http.ResponseWriter, object interface{}) *httperr.Error {
data, err := json.MarshalIndent(object, "", " ")
if err != nil {
return RenderError(rw, err)
}
data = append(data, '\n')
rw.Header().Set("Content-Type", "application/json")
_, err = rw.Write(data)
return httperr.Server(err)
}
func RenderText(rw http.ResponseWriter, text string) *httperr.Error {
_, err := rw.Write([]byte(text))
return httperr.Server(err)
}
func RenderSuccess(rw http.ResponseWriter) *httperr.Error {
_, err := rw.Write([]byte(`{"success":true}`))
return httperr.Server(err)
}
func Redirect(rw http.ResponseWriter, r *http.Request, path string) *httperr.Error {
http.Redirect(rw, r, path, http.StatusFound)
return nil
}