-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (71 loc) · 1.67 KB
/
Copy pathmain.go
File metadata and controls
84 lines (71 loc) · 1.67 KB
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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"time"
"github.com/devhg/gofly"
)
func formatAsDate(t time.Time) string {
year, month, day := t.Date()
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
}
type Student struct {
Name string
Age int
}
func main() {
r := gofly.Default()
// 添加模板渲染函数
r.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate,
})
// 加载模板
r.LoadHtmlGlob("templates/*")
r.GET("/", func(c *gofly.Context) {
c.HTML(http.StatusOK, "css.tmpl", nil)
})
r.GET("/students", func(c *gofly.Context) {
c.HTML(http.StatusOK, "arr.tmpl", gofly.H{
"title": "gofly demo",
"stuArr": []*Student{
{Name: "dev", Age: 11},
{Name: "hui", Age: 22},
},
})
})
v1 := r.Group("/v1")
{
v1.GET("/", func(c *gofly.Context) {
c.HTML(http.StatusOK, "test_func.tmpl", gofly.H{
"title": "test dateFormat",
"now": time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local),
})
})
v1.GET("/hello", func(c *gofly.Context) {
// expect /hello?name=hui
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
})
}
v2 := r.Group("/v2")
// v2.Use(middlewareForV1())
{
v2.GET("/hello/:name", func(c *gofly.Context) {
// expect /hello/hui
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
})
v2.POST("/login", func(c *gofly.Context) {
c.JSON(http.StatusOK, gofly.H{
"username": c.PostForm("username"),
"password": c.PostForm("password"),
})
})
}
r.GET("/panic", func(c *gofly.Context) {
names := []string{"devhui"}
c.String(http.StatusOK, names[100])
})
r.Static("/assets", "./static")
log.Fatal(r.Run(":8081"))
}