2017-11-07 18:12:30 +01:00
|
|
|
# Configuration
|
|
|
|
|
|
|
|
All configuration's values have default values, things will work as you expected with `iris.New()`.
|
|
|
|
|
|
|
|
Configuration is useless before listen functions, so it should be passed on `Application#Run/2` (second argument(s)).
|
|
|
|
|
|
|
|
Iris has a type named `Configurator` which is a `func(*iris.Application)`, any function
|
|
|
|
which completes this can be passed at `Application#Configure` and/or `Application#Run/2`.
|
|
|
|
|
|
|
|
`Application#ConfigurationReadOnly()` returns the configuration values.
|
|
|
|
|
|
|
|
`.Run` **by `Configuration` struct**
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-11-07 18:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
|
|
ctx.HTML("<b>Hello!</b>")
|
|
|
|
})
|
|
|
|
// [...]
|
|
|
|
|
|
|
|
// Good when you want to modify the whole configuration.
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080", iris.WithConfiguration(iris.Configuration{
|
2017-11-07 18:12:30 +01:00
|
|
|
DisableStartupLog: false,
|
|
|
|
DisableInterruptHandler: false,
|
|
|
|
DisablePathCorrection: false,
|
|
|
|
EnablePathEscape: false,
|
|
|
|
FireMethodNotAllowed: false,
|
|
|
|
DisableBodyConsumptionOnUnmarshal: false,
|
|
|
|
DisableAutoFireStatusCode: false,
|
|
|
|
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
|
2020-04-08 15:48:22 +02:00
|
|
|
Charset: "utf-8",
|
2017-11-07 18:12:30 +01:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
`.Run` **by options**
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-11-07 18:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
|
|
ctx.HTML("<b>Hello!</b>")
|
|
|
|
})
|
|
|
|
// [...]
|
|
|
|
|
|
|
|
// Good when you want to change some of the configuration's field.
|
|
|
|
// Prefix: "With", code editors will help you navigate through all
|
|
|
|
// configuration options without even a glitch to the documentation.
|
|
|
|
|
2020-04-08 15:48:22 +02:00
|
|
|
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("utf-8"))
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// or before run:
|
2020-04-08 15:48:22 +02:00
|
|
|
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("utf-8"))
|
2020-03-05 21:41:27 +01:00
|
|
|
// app.Listen(":8080")
|
2017-11-07 18:12:30 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
`.Run` **by TOML config file**
|
|
|
|
|
|
|
|
```tml
|
|
|
|
DisablePathCorrection = false
|
|
|
|
EnablePathEscape = false
|
|
|
|
FireMethodNotAllowed = true
|
|
|
|
DisableBodyConsumptionOnUnmarshal = false
|
|
|
|
TimeFormat = "Mon, 01 Jan 2006 15:04:05 GMT"
|
2020-04-08 15:48:22 +02:00
|
|
|
Charset = "utf-8"
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
[Other]
|
|
|
|
MyServerName = "iris"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-11-07 18:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
|
|
ctx.HTML("<b>Hello!</b>")
|
|
|
|
})
|
|
|
|
// [...]
|
|
|
|
|
|
|
|
// Good when you have two configurations, one for development and a different one for production use.
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080", iris.WithConfiguration(iris.TOML("./configs/iris.tml")))
|
2017-11-07 18:12:30 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
`.Run` **by YAML config file**
|
|
|
|
|
|
|
|
```yml
|
|
|
|
DisablePathCorrection: false
|
|
|
|
EnablePathEscape: false
|
|
|
|
FireMethodNotAllowed: true
|
|
|
|
DisableBodyConsumptionOnUnmarshal: true
|
|
|
|
TimeFormat: Mon, 01 Jan 2006 15:04:05 GMT
|
|
|
|
Charset: UTF-8
|
|
|
|
```
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-11-07 18:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
|
|
ctx.HTML("<b>Hello!</b>")
|
|
|
|
})
|
|
|
|
// [...]
|
|
|
|
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080", iris.WithConfiguration(iris.YAML("./configs/iris.yml")))
|
2017-11-07 18:12:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2019-02-23 06:23:10 +01:00
|
|
|
## Builtin Configurators
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
```go
|
2020-04-20 09:43:45 +02:00
|
|
|
|
|
|
|
// WithGlobalConfiguration will load the global yaml configuration file
|
|
|
|
// from the home directory and it will set/override the whole app's configuration
|
|
|
|
// to that file's contents. The global configuration file can be modified by user
|
|
|
|
// and be used by multiple iris instances.
|
|
|
|
//
|
|
|
|
// This is useful when we run multiple iris servers that share the same
|
|
|
|
// configuration, even with custom values at its "Other" field.
|
|
|
|
//
|
|
|
|
// Usage: `app.Configure(iris.WithGlobalConfiguration)` or `app.Run([iris.Runner], iris.WithGlobalConfiguration)`.
|
|
|
|
WithGlobalConfiguration
|
|
|
|
|
|
|
|
// variables for configurators don't need any receivers, functions
|
|
|
|
// for them that need (helps code editors to recognise as variables without parenthesis completion).
|
|
|
|
|
2017-11-07 18:12:30 +01:00
|
|
|
// WithoutServerError will cause to ignore the matched "errors"
|
|
|
|
// from the main application's `Run` function.
|
|
|
|
//
|
|
|
|
// Usage:
|
2020-03-05 21:41:27 +01:00
|
|
|
// err := app.Listen(":8080", iris.WithoutServerError(iris.ErrServerClosed))
|
2017-11-07 18:12:30 +01:00
|
|
|
// will return `nil` if the server's error was `http/iris#ErrServerClosed`.
|
|
|
|
//
|
|
|
|
// See `Configuration#IgnoreServerErrors []string` too.
|
|
|
|
//
|
|
|
|
// Example: https://github.com/kataras/iris/tree/master/_examples/http-listening/listen-addr/omit-server-errors
|
2020-04-20 09:43:45 +02:00
|
|
|
WithoutServerError(errors ...error) Configurator
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithoutStartupLog turns off the information send, once, to the terminal when the main server is open.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithoutStartupLog
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithoutInterruptHandler disables the automatic graceful server shutdown
|
|
|
|
// when control/cmd+C pressed.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithoutInterruptHandle
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithoutPathCorrection disables the PathCorrection setting.
|
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithoutPathCorrectio
|
|
|
|
|
|
|
|
// WithoutPathCorrectionRedirection disables the PathCorrectionRedirection setting.
|
|
|
|
//
|
|
|
|
// See `Configuration`.
|
|
|
|
WithoutPathCorrectionRedirection
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithoutBodyConsumptionOnUnmarshal disables BodyConsumptionOnUnmarshal setting.
|
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithoutBodyConsumptionOnUnmarshal
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithoutAutoFireStatusCode disables the AutoFireStatusCode setting.
|
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithoutAutoFireStatusCode
|
2017-11-07 18:12:30 +01:00
|
|
|
|
2020-04-20 09:43:45 +02:00
|
|
|
// WithPathEscape enables the PathEscape setting.
|
2017-11-07 18:12:30 +01:00
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithPathEscape
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithOptimizations can force the application to optimize for the best performance where is possible.
|
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithOptimizations
|
2017-11-07 18:12:30 +01:00
|
|
|
|
2020-04-20 09:43:45 +02:00
|
|
|
// WithFireMethodNotAllowed enables the FireMethodNotAllowed setting.
|
2017-11-07 18:12:30 +01:00
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithFireMethodNotAllowed
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithTimeFormat sets the TimeFormat setting.
|
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithTimeFormat(timeformat string) Configurator
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithCharset sets the Charset setting.
|
|
|
|
//
|
|
|
|
// See `Configuration`.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithCharset(charset string) Configurator
|
|
|
|
|
|
|
|
// WithPostMaxMemory sets the maximum post data size
|
|
|
|
// that a client can send to the server, this differs
|
|
|
|
// from the overral request body size which can be modified
|
|
|
|
// by the `context#SetMaxRequestBodySize` or `iris#LimitRequestBodySize`.
|
|
|
|
//
|
|
|
|
// Defaults to 32MB or 32 << 20 if you prefer.
|
|
|
|
WithPostMaxMemory(limit int64) Configurator
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithRemoteAddrHeader enables or adds a new or existing request header name
|
|
|
|
// that can be used to validate the client's real IP.
|
|
|
|
//
|
2020-04-20 09:43:45 +02:00
|
|
|
// By-default no "X-" header is consired safe to be used for retrieving the
|
|
|
|
// client's IP address, because those headers can manually change by
|
|
|
|
// the client. But sometimes are useful e.g., when behind a proxy
|
|
|
|
// you want to enable the "X-Forwarded-For" or when cloudflare
|
|
|
|
// you want to enable the "CF-Connecting-IP", inneed you
|
|
|
|
// can allow the `ctx.RemoteAddr()` to use any header
|
|
|
|
// that the client may sent.
|
|
|
|
//
|
|
|
|
// Defaults to an empty map but an example usage is:
|
|
|
|
// WithRemoteAddrHeader("X-Forwarded-For")
|
2017-11-07 18:12:30 +01:00
|
|
|
//
|
|
|
|
// Look `context.RemoteAddr()` for more.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithRemoteAddrHeader(headerName string) Configurator
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithoutRemoteAddrHeader disables an existing request header name
|
2020-04-20 09:43:45 +02:00
|
|
|
// that can be used to validate and parse the client's real IP.
|
2017-11-07 18:12:30 +01:00
|
|
|
//
|
2020-04-20 09:43:45 +02:00
|
|
|
//
|
|
|
|
// Keep note that RemoteAddrHeaders is already defaults to an empty map
|
|
|
|
// so you don't have to call this Configurator if you didn't
|
|
|
|
// add allowed headers via configuration or via `WithRemoteAddrHeader` before.
|
2017-11-07 18:12:30 +01:00
|
|
|
//
|
|
|
|
// Look `context.RemoteAddr()` for more.
|
2020-04-20 09:43:45 +02:00
|
|
|
WithoutRemoteAddrHeader(headerName string) Configurator
|
|
|
|
|
|
|
|
// WithRemoteAddrPrivateSubnet adds a new private sub-net to be excluded from `context.RemoteAddr`.
|
|
|
|
// See `WithRemoteAddrHeader` too.
|
|
|
|
WithRemoteAddrPrivateSubnet(startIP, endIP string) Configurator
|
2017-11-07 18:12:30 +01:00
|
|
|
|
|
|
|
// WithOtherValue adds a value based on a key to the Other setting.
|
|
|
|
//
|
2020-04-20 09:43:45 +02:00
|
|
|
// See `Configuration.Other`.
|
|
|
|
WithOtherValue(key string, val interface{}) Configurator
|
|
|
|
|
|
|
|
// WithSitemap enables the sitemap generator.
|
|
|
|
// Use the Route's `SetLastMod`, `SetChangeFreq` and `SetPriority` to modify
|
|
|
|
// the sitemap's URL child element properties.
|
|
|
|
//
|
|
|
|
// It accepts a "startURL" input argument which
|
|
|
|
// is the prefix for the registered routes that will be included in the sitemap.
|
|
|
|
//
|
|
|
|
// If more than 50,000 static routes are registered then sitemaps will be splitted and a sitemap index will be served in
|
|
|
|
// /sitemap.xml.
|
|
|
|
//
|
|
|
|
// If `Application.I18n.Load/LoadAssets` is called then the sitemap will contain translated links for each static route.
|
|
|
|
//
|
|
|
|
// If the result does not complete your needs you can take control
|
|
|
|
// and use the github.com/kataras/sitemap package to generate a customized one instead.
|
|
|
|
//
|
|
|
|
// Example: https://github.com/kataras/iris/tree/master/_examples/sitemap.
|
|
|
|
WithSitemap(startURL string) Configurator
|
|
|
|
|
|
|
|
// WithTunneling is the `iris.Configurator` for the `iris.Configuration.Tunneling` field.
|
|
|
|
// It's used to enable http tunneling for an Iris Application, per registered host
|
|
|
|
//
|
|
|
|
// Alternatively use the `iris.WithConfiguration(iris.Configuration{Tunneling: iris.TunnelingConfiguration{ ...}}}`.
|
|
|
|
WithTunneling
|
2017-11-07 18:12:30 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
## Custom Configurator
|
|
|
|
|
|
|
|
With the `Configurator` developers can modularize their applications with ease.
|
|
|
|
|
|
|
|
Example Code:
|
|
|
|
|
|
|
|
```go
|
|
|
|
// file counter/counter.go
|
|
|
|
package counter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/core/host"
|
2017-11-07 18:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func Configurator(app *iris.Application) {
|
|
|
|
counterValue := 0
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
|
|
|
|
for range ticker.C {
|
|
|
|
counterValue++
|
|
|
|
}
|
|
|
|
|
|
|
|
app.ConfigureHost(func(h *host.Supervisor) { // <- HERE: IMPORTANT
|
|
|
|
h.RegisterOnShutdown(func() {
|
|
|
|
ticker.Stop()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
app.Get("/counter", func(ctx iris.Context) {
|
|
|
|
ctx.Writef("Counter value = %d", counterValue)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```go
|
|
|
|
// file: main.go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"counter"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-11-07 18:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
app.Configure(counter.Configurator)
|
|
|
|
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2017-11-07 18:12:30 +01:00
|
|
|
}
|
|
|
|
```
|