-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.go
42 lines (34 loc) · 1.04 KB
/
basic.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
package main
import (
"fmt"
"log"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
// Index is the index handler
func Index(ctx *fasthttp.RequestCtx) {
fmt.Fprint(ctx, "Welcome!\n")
}
// Hello is the Hello handler
func Hello(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name"))
}
// MultiParams is the multi params handler
func MultiParams(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "hi, %s, %s!\n", ctx.UserValue("name"), ctx.UserValue("word"))
}
// QueryArgs is used for uri query args test #11:
// if the req uri is /ping?name=foo, output: Pong! foo
// if the req uri is /piNg?name=foo, redirect to /ping, output: Pong!
func QueryArgs(ctx *fasthttp.RequestCtx) {
name := ctx.QueryArgs().Peek("name")
fmt.Fprintf(ctx, "Pong! %s\n", string(name))
}
func main() {
router := fasthttprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
router.GET("/multi/:name/:word", MultiParams)
router.GET("/ping", QueryArgs)
log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
}