Custom HTTP configuration

More than 12 examples about http server configuration can be found at the _examples/http-server folder.

Use http.ListenAndServe() directly, like this:

  1. func main() {
  2. app := iris.New()
  3. // [...routes]
  4. if err := app.Build(); err!=nil{
  5. panic(err)
  6. }
  7. http.ListenAndServe(":8080", app)
  8. }

Note that you SHOULD call its Build method manually to build the application and the router before using it as an http.Handler.

Another example:

  1. func main() {
  2. app := iris.New()
  3. // [...routes]
  4. app.Build()
  5. srv := &http.Server{
  6. Addr: ":8080",
  7. Handler: app,
  8. ReadTimeout: 10 * time.Second,
  9. WriteTimeout: 10 * time.Second,
  10. MaxHeaderBytes: 1 << 20,
  11. }
  12. srv.ListenAndServe()
  13. }

However, you rarely need an external http.Server instance with Iris. You can listen using any tcp listener, http server or a custom function via Application.Run method.

  1. app.Run(iris.Listener(l net.Listener)) // listen using a custom net.Listener
  2. app.Run(iris.Server(srv *http.Server)) // listen using a custom http.Server
  3. app.Run(iris.Addr(addr string)) // the app.Listen is a shortcut of this method.
  4. app.Run(iris.TLS(addr string, certFileOrContents, keyFileOrContents string)) // listen TLS.
  5. app.Run(iris.AutoTLS(addr, domain, email string)) // listen using letsencrypt (see below).
  6. // and any custom function that returns an error:
  7. app.Run(iris.Raw(f func() error))