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
|
package main
import (
"fmt"
"log"
"github.com/fasthttp/router"
"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"))
}
// RegexParams is the params handler with regex validation
func RegexParams(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "hi, %s\n", ctx.UserValue("name"))
}
// 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() {
r := router.New()
r.GET("/", Index)
r.GET("/hello/{name}", Hello)
r.GET("/multi/{name}/{word}", MultiParams)
r.GET("/regex/{name:[a-zA-Z]+}/test", RegexParams)
r.GET("/optional/{name?:[a-zA-Z]+}/{word?}", MultiParams)
r.GET("/ping", QueryArgs)
log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler))
}
|