fasthttp.go 650 B

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "github.com/buaazp/fasthttprouter"
  7. "github.com/valyala/fasthttp"
  8. )
  9. func Index(ctx *fasthttp.RequestCtx) {
  10. ctx.SetContentType("application/json")
  11. ctx.SetStatusCode(fasthttp.StatusOK)
  12. users := []string{"rvasily"}
  13. body, _ := json.Marshal(users)
  14. ctx.SetBody(body)
  15. }
  16. func GetUser(ctx *fasthttp.RequestCtx) {
  17. fmt.Fprintf(ctx, "you try to see user %s\n", ctx.UserValue("id"))
  18. }
  19. func main() {
  20. router := fasthttprouter.New()
  21. router.GET("/", Index)
  22. router.GET("/users/:id", GetUser)
  23. fmt.Println("starting server at :8080")
  24. log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
  25. }