-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (48 loc) · 1.38 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
package main
import (
"fmt"
)
func main() {
router := NewRouter()
app := NewApplication(router)
router.Use(Any, func(ctx *Context, next func()) {
fmt.Printf("---> %s %s\n", ctx.Request().Method, ctx.Request().URL)
next()
fmt.Printf("<--- %d %s\n", ctx.Response().StatusCode, ctx.Request().URL)
})
router.Get("/", func(ctx *Context, next func()) {
fmt.Fprintf(ctx.Response(), "I got you!")
})
router2 := NewRouter()
router2.Get("/auth/login", func(ctx *Context, next func()) {
ctx.Response().Headers().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(ctx.Response(), `<form method="post" action="/auth/login?username=bob&password=pas$">
<button type="submit">click me</button>
</form>`)
})
router2.Post("/auth/login", func(ctx *Context, next func()) {
fmt.Fprintf(
ctx.Response(),
"Username: %s, Password: %s",
ctx.Request().Query("username"),
ctx.Request().Query("password"),
)
})
router.Use("/auth", router2.Next)
router3 := NewRouter()
router3.Use(Any, func(ctx *Context, next func()) {
if ctx.Request().Query("auth") != "1" {
ctx.Response().StatusCode = 401
ctx.Response().End()
return
}
next()
})
router3.Get("/token", func(ctx *Context, next func()) {
fmt.Fprintf(ctx.Response(), "Token: ce62ff2ec91f4ce2935c8b3c36a42722")
})
router.Use(Any, router3.Next)
if err := app.Listen(":8080"); err != nil {
panic(err)
}
}