-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
45 lines (37 loc) · 1004 Bytes
/
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
package main
import (
"github.com/pilu/traffic"
)
func rootHandler(w traffic.ResponseWriter, r *traffic.Request) {
traffic.Logger().Print("Hello")
w.WriteText("Hello World\n")
}
func jsonTestHandler(w traffic.ResponseWriter, r *traffic.Request) {
data := map[string]string{
"foo": "bar",
}
w.WriteJSON(data)
}
func xmlTestHandler(w traffic.ResponseWriter, r *traffic.Request) {
type Person struct {
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
}
w.WriteXML(&Person{
FirstName: "foo",
LastName: "bar",
})
}
func pageHandler(w traffic.ResponseWriter, r *traffic.Request) {
w.WriteText("Category ID: %s\n", r.Param("category_id"))
w.WriteText("Page ID: %s\n", r.Param("id"))
}
func main() {
router := traffic.New()
// Routes
router.Get("/", rootHandler)
router.Get("/json", jsonTestHandler)
router.Get("/xml", xmlTestHandler)
router.Get("/categories/:category_id/pages/:id", pageHandler)
router.Run()
}