IRIS服务器监听地址

IRIS服务器监听地址普通

目录结构

主目录listenAddr

  1. —— main.go

代码示例

main.go

  1. package main
  2. import (
  3. "github.com/kataras/iris"
  4. )
  5. func main() {
  6. app := iris.New()
  7. app.Get("/", func(ctx iris.Context) {
  8. ctx.HTML("<h1>Hello World!</h1>")
  9. })
  10. // http://localhost:8080
  11. app.Run(iris.Addr(":8080"))
  12. }

IRIS服务器监听地址关闭错误

目录结构

主目录omitServerErrors

  1. —— main.go
  2. —— main_test.go

代码示例

main.go

  1. package main
  2. import (
  3. "github.com/kataras/iris"
  4. )
  5. func main() {
  6. app := iris.New()
  7. app.Get("/", func(ctx iris.Context) {
  8. ctx.HTML("<h1>Hello World!</h1>")
  9. })
  10. err := app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
  11. if err != nil {
  12. // do something
  13. }
  14. // same as:
  15. // err := app.Run(iris.Addr(":8080"))
  16. // if err != nil && (err != iris.ErrServerClosed || err.Error() != iris.ErrServerClosed.Error()) {
  17. // [...]
  18. // }
  19. }

main_test.go

  1. package main
  2. import (
  3. "bytes"
  4. stdContext "context"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/kataras/iris"
  9. )
  10. func logger(app *iris.Application) *bytes.Buffer {
  11. buf := &bytes.Buffer{}
  12. app.Logger().SetOutput(buf)
  13. // disable the "Now running at...." in order to have a clean log of the error.
  14. // we could attach that on `Run` but better to keep things simple here.
  15. app.Configure(iris.WithoutStartupLog)
  16. return buf
  17. }
  18. func TestListenAddr(t *testing.T) {
  19. app := iris.New()
  20. // we keep the logger running as well but in a controlled way.
  21. log := logger(app)
  22. // close the server at 3-6 seconds
  23. go func() {
  24. time.Sleep(3 * time.Second)
  25. ctx, cancel := stdContext.WithTimeout(stdContext.TODO(), 3*time.Second)
  26. defer cancel()
  27. app.Shutdown(ctx)
  28. }()
  29. err := app.Run(iris.Addr(":9829"))
  30. // in this case the error should be logged and return as well.
  31. if err != iris.ErrServerClosed {
  32. t.Fatalf("expecting err to be `iris.ErrServerClosed` but got: %v", err)
  33. }
  34. expectedMessage := iris.ErrServerClosed.Error()
  35. if got := log.String(); !strings.Contains(got, expectedMessage) {
  36. t.Fatalf("expecting to log to contains the:\n'%s'\ninstead of:\n'%s'", expectedMessage, got)
  37. }
  38. }
  39. func TestListenAddrWithoutServerErr(t *testing.T) {
  40. app := iris.New()
  41. // we keep the logger running as well but in a controlled way.
  42. log := logger(app)
  43. // close the server at 3-6 seconds
  44. go func() {
  45. time.Sleep(3 * time.Second)
  46. ctx, cancel := stdContext.WithTimeout(stdContext.TODO(), 3*time.Second)
  47. defer cancel()
  48. app.Shutdown(ctx)
  49. }()
  50. // we disable the ErrServerClosed, so the error should be nil when server is closed by `app.Shutdown`.
  51. // so in this case the iris/http.ErrServerClosed should be NOT logged and NOT return.
  52. err := app.Run(iris.Addr(":9827"), iris.WithoutServerError(iris.ErrServerClosed))
  53. if err != nil {
  54. t.Fatalf("expecting err to be nil but got: %v", err)
  55. }
  56. if got := log.String(); got != "" {
  57. t.Fatalf("expecting to log nothing but logged: '%s'", got)
  58. }
  59. }