-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_test.go
68 lines (56 loc) · 1.26 KB
/
example_test.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 app_test
import (
"log"
"net/http"
"github.com/gowww/app"
"github.com/gowww/check"
"github.com/gowww/i18n"
"golang.org/x/text/language"
)
func Example() {
var locales = i18n.Locales{
language.English: {
"hello": "Hello!",
},
language.French: {
"hello": "Bonjour !",
},
}
app.Localize(locales, language.English)
app.Get("/", func(c *app.Context) {
c.View("home")
})
app.Post("/user/:id/files/", func(c *app.Context) {
c.Status(http.StatusCreated)
c.JSON(map[string]interface{}{
"id": c.PathValue("id"),
"filepath": c.PathValue("*"),
})
})
v1 := app.Group("/v1")
{
v1.Get("/user", func(c *app.Context) { c.Text("User for V1") })
v1.Get("/item", func(c *app.Context) { c.Text("Item for V1") })
}
v2 := app.Group("/v2")
{
v2.Get("/user", func(c *app.Context) { c.Text("User for V2") })
v2.Get("/item", func(c *app.Context) { c.Text("Item for V2") })
}
if !app.EnvProduction() {
log.Printf("Developing app on %s\n", app.Address())
}
app.Run()
}
func ExampleContext_BadRequest() {
userChecker := check.Checker{
"email": {check.Required, check.Email},
"phone": {check.Phone},
}
app.Post("/users", func(c *app.Context) {
if c.BadRequest(userChecker, "") {
return
}
c.Status(http.StatusCreated)
})
}