-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
69 lines (60 loc) · 1.37 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/baa-middleware/accesslog"
"github.com/baa-middleware/recovery"
"github.com/go-baa/baa"
"github.com/go-baa/render"
)
func main() {
b := baa.New()
b.Use(accesslog.Logger())
b.Use(recovery.Recovery())
b.SetDI("render", render.New(render.Options{
Baa: b,
Root: "template/",
Extensions: []string{".html", ".tmpl"},
}))
b.Get("/", func(c *baa.Context) {
c.String(200, "Hello, World!\n")
})
b.Post("/", func(c *baa.Context) {
defer c.Req.Body.Close()
body, err := ioutil.ReadAll(c.Req.Body)
if err != nil {
c.Error(err)
return
}
var data map[string]interface{}
body = []byte(strings.Replace(string(body), "\\x22", "\"", -1))
err = json.Unmarshal(body, &data)
if err != nil {
c.Error(err)
return
}
for k, v := range data {
c.Resp.Write([]byte(fmt.Sprintf("%s: %v\n", k, v)))
}
})
b.Get("/tpl", func(c *baa.Context) {
c.Set("name", "micate")
//c.HTML(200, "template/test.html")
c.HTML(200, "test")
})
b.Get("/file", func(c *baa.Context) {
c.Fetch("template/header.html")
//c.HTML(200, "template/upload.html")
c.HTML(200, "upload")
})
b.Post("/file", func(c *baa.Context) {
c.Set("posts", c.Posts())
c.Set("a", c.Query("a"))
_, fh, _ := c.GetFile("file1")
c.Set("file", fh.Filename)
c.JSON(200, c.Gets())
})
b.Run(":1323")
}