2016-09-09 07:09:03 +02:00
package iris
import (
2017-02-16 04:00:08 +01:00
"io/ioutil"
"path/filepath"
2016-09-09 07:09:03 +02:00
"strconv"
"time"
2016-10-11 21:35:12 +02:00
"github.com/imdario/mergo"
2017-02-16 04:00:08 +01:00
"gopkg.in/yaml.v2"
2016-09-09 07:09:03 +02:00
)
type (
// OptionSetter sets a configuration field to the main configuration
2017-02-16 02:26:02 +01:00
// used to help developers to write less and configure only what
// they really want and nothing else.
//
// Usage:
2017-02-15 07:40:43 +01:00
// iris.New(iris.Configuration{Charset: "UTF-8", Gzip:true})
2016-09-09 07:09:03 +02:00
// now can be done also by using iris.Option$FIELD:
2017-02-15 07:40:43 +01:00
// iris.New(iris.OptionCharset("UTF-8"), iris.OptionGzip(true))
2017-02-16 02:26:02 +01:00
//
// Benefits:
// 1. Developers have no worries what option to pass,
// they can just type iris.Option and all options should
// be visible to their editor's autocomplete-popup window
// 2. Can be passed with any order
2016-09-09 07:09:03 +02:00
// 3. Can override previous configuration
OptionSetter interface {
// Set receives a pointer to the global Configuration type and does the job of filling it
Set ( c * Configuration )
}
// OptionSet implements the OptionSetter
OptionSet func ( c * Configuration )
)
// Set is the func which makes the OptionSet an OptionSetter, this is used mostly
func ( o OptionSet ) Set ( c * Configuration ) {
o ( c )
}
2017-02-16 04:00:08 +01:00
// YAML reads Configuration from a file.yml.
//
// Accepts the absolute path of the file.yml.
// An error will be shown to the user via panic with the error message.
// Error may occur when the file.yml doesn't exists or is not formatted correctly.
//
// Usage:
// 1. `app := iris.New(YAML("myfile.yml"))`
// 2. `app.Set(YAML("myfile.yml"))`
func YAML ( filename string ) Configuration {
c := DefaultConfiguration ( )
// get the abs
// which will try to find the 'filename' from current workind dir too.
yamlAbsPath , err := filepath . Abs ( filename )
if err != nil {
panic ( "FATAL ERROR .yml.filename to absolute: " + err . Error ( ) )
}
// read the raw contents of the file
data , err := ioutil . ReadFile ( yamlAbsPath )
if err != nil {
panic ( "FATAL ERROR .yml.ReadFile: " + err . Error ( ) )
}
// put the file's contents as yaml to the default configuration(c)
if err := yaml . Unmarshal ( data , & c ) ; err != nil {
panic ( "FATAL ERROR .yml.Unmarshal: " + err . Error ( ) )
}
return c
}
2017-02-16 02:26:02 +01:00
// Configuration the whole configuration for an Iris station instance
// these can be passed via options also, look at the top of this file(configuration.go).
// Configuration is a valid OptionSetter.
2016-09-09 07:09:03 +02:00
type Configuration struct {
2016-09-27 15:28:38 +02:00
// VHost is the addr or the domain that server listens to, which it's optional
// When to set VHost manually:
// 1. it's automatically setted when you're calling
// $instance.Listen/ListenUNIX/ListenTLS/ListenLETSENCRYPT functions or
// ln,_ := iris.TCP4/UNIX/TLS/LETSENCRYPT; $instance.Serve(ln)
// 2. If you using a balancer, or something like nginx
// then set it in order to have the correct url
// when calling the template helper '{{url }}'
// *keep note that you can use {{urlpath }}) instead*
//
2017-01-02 20:20:17 +01:00
// Note: this is the main's server Host, you can setup unlimited number of net/http servers
2016-09-27 15:28:38 +02:00
// listening to the $instance.Handler after the manually-called $instance.Build
//
2017-02-16 02:26:02 +01:00
// Default comes from iris.Default.Listen/.Serve with iris' listeners (iris.TCP4/UNIX/TLS/LETSENCRYPT).
2017-02-16 17:14:55 +01:00
VHost string ` yaml:"VHost" `
2016-09-27 15:28:38 +02:00
// VScheme is the scheme (http:// or https://) putted at the template function '{{url }}'
// It's an optional field,
// When to set VScheme manually:
2017-02-16 02:26:02 +01:00
// 1. You didn't start the main server using $instance.Listen/ListenTLS/ListenLETSENCRYPT
// or $instance.Serve($instance.TCP4()/.TLS...)
// 2. if you're using something like nginx and have iris listening with
// addr only(http://) but the nginx mapper is listening to https://
2016-09-27 15:28:38 +02:00
//
2017-02-16 02:26:02 +01:00
// Default comes from iris.Default.Listen/.Serve with iris' listeners (TCP4,UNIX,TLS,LETSENCRYPT).
2017-02-16 17:14:55 +01:00
VScheme string ` yaml:"VScheme" `
2016-09-27 15:28:38 +02:00
2017-02-16 02:26:02 +01:00
// ReadTimeout is the maximum duration before timing out read of the request.
2017-02-16 17:14:55 +01:00
ReadTimeout time . Duration ` yaml:"ReadTimeout" `
2017-02-16 04:00:08 +01:00
2017-02-16 02:26:02 +01:00
// WriteTimeout is the maximum duration before timing out write of the response.
2017-02-16 17:14:55 +01:00
WriteTimeout time . Duration ` yaml:"WriteTimeout" `
2017-01-02 20:20:17 +01:00
// MaxHeaderBytes controls the maximum number of bytes the
// server will read parsing the request header's keys and
// values, including the request line. It does not limit the
// size of the request body.
// If zero, DefaultMaxHeaderBytes is used.
2017-02-16 17:14:55 +01:00
MaxHeaderBytes int ` yaml:"MaxHeaderBytes" `
2016-09-27 15:28:38 +02:00
2016-09-16 09:02:18 +02:00
// CheckForUpdates will try to search for newer version of Iris based on the https://github.com/kataras/iris/releases
// If a newer version found then the app will ask the he dev/user if want to update the 'x' version
// if 'y' is pressed then the updater will try to install the latest version
// the updater, will notify the dev/user that the update is finished and should restart the App manually.
// Notes:
// 1. Experimental feature
2016-09-17 23:50:10 +02:00
// 2. If setted to true, the app will start the server normally and runs the updater in its own goroutine,
2017-02-16 02:26:02 +01:00
// in order to no delay the boot time on your development state.
2016-09-16 09:02:18 +02:00
// 3. If you as developer edited the $GOPATH/src/github/kataras or any other Iris' Go dependencies at the past
// then the update process will fail.
//
2017-02-16 02:26:02 +01:00
// Usage: app := iris.New(iris.Configuration{CheckForUpdates: true})
2016-09-17 23:50:10 +02:00
//
2017-02-16 02:26:02 +01:00
// Defaults to false.
2017-02-16 17:14:55 +01:00
CheckForUpdates bool ` yaml:"CheckForUpdates" `
2016-09-16 09:02:18 +02:00
2017-01-10 14:03:02 +01:00
// DisablePathCorrection corrects and redirects the requested path to the registered path
2016-09-09 07:09:03 +02:00
// for example, if /home/ path is requested but no handler for this Route found,
// then the Router checks if /home handler exists, if yes,
// (permant)redirects the client to the correct path /home
//
2017-02-16 02:26:02 +01:00
// Defaults to false.
2017-02-16 04:00:08 +01:00
DisablePathCorrection bool ` yaml:"disablePathCorrection" `
2016-09-09 07:09:03 +02:00
2017-01-10 07:54:39 +01:00
// EnablePathEscape when is true then its escapes the path, the named parameters (if any).
// Change to false it if you want something like this https://github.com/kataras/iris/issues/135 to work
2016-09-09 07:09:03 +02:00
//
2017-01-10 07:54:39 +01:00
// When do you need to Disable(false) it:
2016-09-09 07:09:03 +02:00
// accepts parameters with slash '/'
// Request: http://localhost:8080/details/Project%2FDelta
// ctx.Param("project") returns the raw named parameter: Project%2FDelta
// which you can escape it manually with net/url:
// projectName, _ := url.QueryUnescape(c.Param("project").
//
2017-02-16 02:26:02 +01:00
// Defaults to false.
2017-02-16 17:14:55 +01:00
EnablePathEscape bool ` yaml:"EnablePathEscape" `
2016-09-09 07:09:03 +02:00
2017-02-16 02:26:02 +01:00
// FireMethodNotAllowed if it's true router checks for StatusMethodNotAllowed(405) and
// fires the 405 error instead of 404
// Defaults to false.
2017-02-16 17:14:55 +01:00
FireMethodNotAllowed bool ` yaml:"FireMethodNotAllowed" `
2016-10-11 21:35:12 +02:00
2017-01-30 11:35:43 +01:00
// DisableBodyConsumptionOnUnmarshal manages the reading behavior of the context's body readers/binders.
// If setted to true then it
// disables the body consumption by the `context.UnmarshalBody/ReadJSON/ReadXML`.
//
// By-default io.ReadAll` is used to read the body from the `context.Request.Body which is an `io.ReadCloser`,
// if this field setted to true then a new buffer will be created to read from and the request body.
2017-02-16 02:26:02 +01:00
// The body will not be changed and existing data before the
// context.UnmarshalBody/ReadJSON/ReadXML will be not consumed.
2017-02-16 17:14:55 +01:00
DisableBodyConsumptionOnUnmarshal bool ` yaml:"DisableBodyConsumptionOnUnmarshal" `
2017-01-30 11:35:43 +01:00
2016-09-09 07:09:03 +02:00
// TimeFormat time format for any kind of datetime parsing
2017-02-17 09:45:47 +01:00
// Defaults to "Mon, 02 Jan 2006 15:04:05 GMT".
2017-02-16 17:14:55 +01:00
TimeFormat string ` yaml:"TimeFormat" `
2016-09-09 07:09:03 +02:00
// Charset character encoding for various rendering
// used for templates and the rest of the responses
2017-02-16 02:26:02 +01:00
// Defaults to "UTF-8".
2017-02-16 17:14:55 +01:00
Charset string ` yaml:"Charset" `
2016-09-09 07:09:03 +02:00
2017-02-16 02:26:02 +01:00
// Gzip enables gzip compression on your Render actions, this includes any type of render,
// templates and pure/raw content
// If you don't want to enable it globally, you could just use the third parameter
// on context.Render("myfileOrResponse", structBinding{}, iris.RenderOptions{"gzip": true})
// Defaults to false.
2017-02-16 17:14:55 +01:00
Gzip bool ` yaml:"Gzip" `
2016-09-09 07:09:03 +02:00
2017-02-16 02:26:02 +01:00
// Other are the custom, dynamic options, can be empty.
// This field used only by you to set any app's options you want
// or by custom adaptors, it's a way to simple communicate between your adaptors (if any)
// Defaults to a non-nil empty map.
2017-02-16 17:14:55 +01:00
Other map [ string ] interface { } ` yaml:"Other" `
2016-09-09 07:09:03 +02:00
}
// Set implements the OptionSetter
func ( c Configuration ) Set ( main * Configuration ) {
2017-02-16 04:00:08 +01:00
if err := mergo . MergeWithOverwrite ( main , c ) ; err != nil {
panic ( "FATAL ERROR .Configuration as OptionSetter: " + err . Error ( ) )
}
2016-09-09 07:09:03 +02:00
}
// All options starts with "Option" preffix in order to be easier to find what dev searching for
var (
2016-09-27 15:28:38 +02:00
// OptionVHost is the addr or the domain that server listens to, which it's optional
// When to set VHost manually:
// 1. it's automatically setted when you're calling
// $instance.Listen/ListenUNIX/ListenTLS/ListenLETSENCRYPT functions or
// ln,_ := iris.TCP4/UNIX/TLS/LETSENCRYPT; $instance.Serve(ln)
// 2. If you using a balancer, or something like nginx
// then set it in order to have the correct url
// when calling the template helper '{{url }}'
// *keep note that you can use {{urlpath }}) instead*
//
2017-01-02 20:20:17 +01:00
// Note: this is the main's server Host, you can setup unlimited number of net/http servers
2016-09-27 15:28:38 +02:00
// listening to the $instance.Handler after the manually-called $instance.Build
//
2017-02-16 02:26:02 +01:00
// Default comes from iris.Default.Listen/.Serve with iris' listeners (iris.TCP4/UNIX/TLS/LETSENCRYPT).
2016-09-27 15:28:38 +02:00
OptionVHost = func ( val string ) OptionSet {
return func ( c * Configuration ) {
c . VHost = val
}
}
// OptionVScheme is the scheme (http:// or https://) putted at the template function '{{url }}'
// It's an optional field,
// When to set Scheme manually:
2017-02-16 02:26:02 +01:00
// 1. You didn't start the main server using $instance.Listen/ListenTLS/ListenLETSENCRYPT
// or $instance.Serve($instance.TCP4()/.TLS...)
// 2. if you're using something like nginx and have iris listening with
// addr only(http://) but the nginx mapper is listening to https://
2016-09-27 15:28:38 +02:00
//
2017-02-16 02:26:02 +01:00
// Default comes from iris.Default.Listen/.Serve with iris' listeners (TCP4,UNIX,TLS,LETSENCRYPT).
2016-09-27 15:28:38 +02:00
OptionVScheme = func ( val string ) OptionSet {
return func ( c * Configuration ) {
c . VScheme = val
}
}
2017-02-16 02:26:02 +01:00
// OptionReadTimeout sets the Maximum duration before timing out read of the request.
2016-09-27 15:28:38 +02:00
OptionReadTimeout = func ( val time . Duration ) OptionSet {
return func ( c * Configuration ) {
c . ReadTimeout = val
}
}
2017-02-16 02:26:02 +01:00
// OptionWriteTimeout sets the Maximum duration before timing out write of the response.
2016-09-27 15:28:38 +02:00
OptionWriteTimeout = func ( val time . Duration ) OptionSet {
return func ( c * Configuration ) {
c . WriteTimeout = val
}
}
2017-01-02 20:20:17 +01:00
// MaxHeaderBytes controls the maximum number of bytes the
// server will read parsing the request header's keys and
// values, including the request line. It does not limit the
// size of the request body.
// If zero, DefaultMaxHeaderBytes(8MB) is used.
OptionMaxHeaderBytes = func ( val int ) OptionSet {
2016-09-27 15:28:38 +02:00
return func ( c * Configuration ) {
2017-01-02 20:20:17 +01:00
c . MaxHeaderBytes = val
2016-09-27 15:28:38 +02:00
}
}
2016-09-16 09:02:18 +02:00
// OptionCheckForUpdates will try to search for newer version of Iris based on the https://github.com/kataras/iris/releases
// If a newer version found then the app will ask the he dev/user if want to update the 'x' version
// if 'y' is pressed then the updater will try to install the latest version
// the updater, will notify the dev/user that the update is finished and should restart the App manually.
// Notes:
// 1. Experimental feature
// 2. If setted to true, the app will have a little startup delay
// 3. If you as developer edited the $GOPATH/src/github/kataras or any other Iris' Go dependencies at the past
// then the update process will fail.
//
2017-02-14 04:54:11 +01:00
// Usage: iris.Default.Set(iris.OptionCheckForUpdates(true)) or
// iris.Default.Config.CheckForUpdates = true or
2016-09-16 09:02:18 +02:00
// app := iris.New(iris.OptionCheckForUpdates(true))
2017-02-16 02:26:02 +01:00
// Defaults to false.
2016-09-16 09:02:18 +02:00
OptionCheckForUpdates = func ( val bool ) OptionSet {
return func ( c * Configuration ) {
c . CheckForUpdates = val
}
}
2017-01-10 14:03:02 +01:00
// OptionDisablePathCorrection corrects and redirects the requested path to the registered path
2016-09-09 07:09:03 +02:00
// for example, if /home/ path is requested but no handler for this Route found,
// then the Router checks if /home handler exists, if yes,
// (permant)redirects the client to the correct path /home
//
2017-02-16 02:26:02 +01:00
// Defaults to false.
2016-09-09 07:09:03 +02:00
OptionDisablePathCorrection = func ( val bool ) OptionSet {
return func ( c * Configuration ) {
c . DisablePathCorrection = val
}
}
2016-09-16 09:02:18 +02:00
2017-01-10 07:54:39 +01:00
// OptionEnablePathEscape when is true then its escapes the path, the named path parameters (if any).
OptionEnablePathEscape = func ( val bool ) OptionSet {
2016-09-09 07:09:03 +02:00
return func ( c * Configuration ) {
2017-01-10 07:54:39 +01:00
c . EnablePathEscape = val
2016-09-09 07:09:03 +02:00
}
}
2016-09-16 09:02:18 +02:00
2017-02-16 02:26:02 +01:00
// FireMethodNotAllowed if it's true router checks for StatusMethodNotAllowed(405)
// and fires the 405 error instead of 404
// Defaults to false.
2016-10-11 21:35:12 +02:00
OptionFireMethodNotAllowed = func ( val bool ) OptionSet {
return func ( c * Configuration ) {
c . FireMethodNotAllowed = val
}
}
2017-01-30 11:35:43 +01:00
// OptionDisableBodyConsumptionOnUnmarshal manages the reading behavior of the context's body readers/binders.
// If setted to true then it
// disables the body consumption by the `context.UnmarshalBody/ReadJSON/ReadXML`.
//
// By-default io.ReadAll` is used to read the body from the `context.Request.Body which is an `io.ReadCloser`,
// if this field setted to true then a new buffer will be created to read from and the request body.
// The body will not be changed and existing data before the context.UnmarshalBody/ReadJSON/ReadXML will be not consumed.
OptionDisableBodyConsumptionOnUnmarshal = func ( val bool ) OptionSet {
return func ( c * Configuration ) {
c . DisableBodyConsumptionOnUnmarshal = val
}
}
2017-02-16 02:26:02 +01:00
// OptionTimeFormat time format for any kind of datetime parsing.
2017-02-17 09:45:47 +01:00
// Defaults to "Mon, 02 Jan 2006 15:04:05 GMT".
2016-09-09 07:09:03 +02:00
OptionTimeFormat = func ( val string ) OptionSet {
return func ( c * Configuration ) {
c . TimeFormat = val
}
}
2016-09-16 09:02:18 +02:00
2016-09-09 07:09:03 +02:00
// OptionCharset character encoding for various rendering
// used for templates and the rest of the responses
2017-02-16 02:26:02 +01:00
// Defaults to "UTF-8".
2016-09-09 07:09:03 +02:00
OptionCharset = func ( val string ) OptionSet {
return func ( c * Configuration ) {
c . Charset = val
}
}
2016-09-16 09:02:18 +02:00
2016-09-09 07:09:03 +02:00
// OptionGzip enables gzip compression on your Render actions, this includes any type of render, templates and pure/raw content
2017-01-10 14:03:02 +01:00
// If you don't want to enable it globally, you could just use the third parameter on context.Render("myfileOrResponse", structBinding{}, iris.RenderOptions{"gzip": true})
2017-02-16 02:26:02 +01:00
// Defaults to false.
2016-09-09 07:09:03 +02:00
OptionGzip = func ( val bool ) OptionSet {
return func ( c * Configuration ) {
c . Gzip = val
}
}
2017-02-16 02:26:02 +01:00
// Other are the custom, dynamic options, can be empty.
// This field used only by you to set any app's options you want
// or by custom adaptors, it's a way to simple communicate between your adaptors (if any)
// Defaults to a non-nil empty map.
OptionOther = func ( key string , val interface { } ) OptionSet {
2016-09-09 07:09:03 +02:00
return func ( c * Configuration ) {
2017-02-16 02:26:02 +01:00
if c . Other == nil {
c . Other = make ( map [ string ] interface { } , 0 )
}
c . Other [ key ] = val
2016-09-09 07:09:03 +02:00
}
}
)
var (
// DefaultTimeFormat default time format for any kind of datetime parsing
DefaultTimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
// StaticCacheDuration expiration duration for INACTIVE file handlers, it's a global configuration field to all iris instances
StaticCacheDuration = 20 * time . Second
)
// Default values for base Iris conf
const (
DefaultDisablePathCorrection = false
2017-01-10 07:54:39 +01:00
DefaultEnablePathEscape = false
2016-09-09 07:09:03 +02:00
DefaultCharset = "UTF-8"
2016-09-27 15:28:38 +02:00
// Per-connection buffer size for requests' reading.
// This also limits the maximum header size.
//
// Increase this buffer if your clients send multi-KB RequestURIs
// and/or multi-KB headers (for example, BIG cookies).
//
// Default buffer size is 8MB
2017-01-02 20:20:17 +01:00
DefaultMaxHeaderBytes = 8096
2016-09-27 15:28:38 +02:00
2017-01-02 20:20:17 +01:00
// DefaultReadTimeout no read client timeout
DefaultReadTimeout = 0
// DefaultWriteTimeout no serve client timeout
DefaultWriteTimeout = 0
2016-09-09 07:09:03 +02:00
)
// DefaultConfiguration returns the default configuration for an Iris station, fills the main Configuration
2017-02-16 04:00:08 +01:00
func DefaultConfiguration ( ) Configuration {
return Configuration {
2017-01-30 11:35:43 +01:00
VHost : "" ,
VScheme : "" ,
ReadTimeout : DefaultReadTimeout ,
WriteTimeout : DefaultWriteTimeout ,
MaxHeaderBytes : DefaultMaxHeaderBytes ,
CheckForUpdates : false ,
DisablePathCorrection : DefaultDisablePathCorrection ,
EnablePathEscape : DefaultEnablePathEscape ,
FireMethodNotAllowed : false ,
DisableBodyConsumptionOnUnmarshal : false ,
TimeFormat : DefaultTimeFormat ,
Charset : DefaultCharset ,
Gzip : false ,
2017-02-16 02:26:02 +01:00
Other : make ( map [ string ] interface { } , 0 ) ,
2016-09-09 07:09:03 +02:00
}
}
// Default values for base Server conf
const (
// DefaultServerHostname returns the default hostname which is 0.0.0.0
DefaultServerHostname = "0.0.0.0"
2016-09-27 15:28:38 +02:00
// DefaultServerPort returns the default port which is 8080, not used
2016-09-09 07:09:03 +02:00
DefaultServerPort = 8080
)
var (
// DefaultServerAddr the default server addr which is: 0.0.0.0:8080
DefaultServerAddr = DefaultServerHostname + ":" + strconv . Itoa ( DefaultServerPort )
)