You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 line
1.0 KiB

  1. package main
  2. import (
  3. "github.com/kataras/iris"
  4. "github.com/kataras/iris/middleware/logger"
  5. "github.com/kataras/iris/middleware/recover"
  6. "github.com/valyala/tcplisten"
  7. )
  8. func main() {
  9. app := iris.New()
  10. app.Use(recover.New())
  11. app.Use(logger.New())
  12. // 输出html
  13. // 请求方式:GET
  14. // 访问地址:http://localhost:8080/welcome
  15. app.Handle("GET", "/welcome", func(ctx iris.Context) {
  16. ctx.HTML("<h1>Welcome</h1>")
  17. })
  18. // 输出字符串
  19. // 类似于app.Handle("GET", "/ping", [...])
  20. // 请求方式GET
  21. // 请求地址:http://localhost:8080/ping
  22. app.Get("/ping", func(ctx iris.Context) {
  23. ctx.WriteString("Pong")
  24. })
  25. // 输出JSON
  26. // 请求方式:GET
  27. // 请求地址: http://localhost:8080/hello
  28. app.Get("/hello", func(ctx iris.Context) {
  29. ctx.JSON(iris.Map{"message": "hello Iris!"})
  30. })
  31. listenerCfg := tcplisten.Config{
  32. ReusePort: true,
  33. DeferAccept: true,
  34. FastOpen: true,
  35. }
  36. l, err := listenerCfg.NewListener("tcp4", ":8080")
  37. if err != nil {
  38. app.Logger().Fatal(err)
  39. }
  40. app.Run(iris.Listener(l)) //8080 监听端口
  41. }