-
Notifications
You must be signed in to change notification settings - Fork 3
/
rex.go
39 lines (32 loc) · 881 Bytes
/
rex.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
package rex
import (
"bytes"
"encoding/json"
"net/http"
"path"
"github.com/goanywhere/env"
"github.com/goanywhere/fs"
)
// Shortcut for string based map.
type M map[string]interface{}
// Sends the HTTP response in JSON.
func Send(w http.ResponseWriter, v interface{}) {
var buffer = new(bytes.Buffer)
defer func() {
buffer.Reset()
}()
w.Header().Set("Content-type", "application/json; charset=utf-8")
if err := json.NewEncoder(buffer).Encode(v); err == nil {
w.Write(buffer.Bytes())
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Renders a view (Pongo) and sends the rendered HTML string to the client.
// Optional parameters: value, local variables for the view.
// func Render(filename string, v ...interface{}) {}
func init() {
var basedir = fs.Getcd(2)
env.Set("basedir", basedir)
env.Load(path.Join(basedir, ".env"))
}