2016-05-30 16:08:09 +02:00
package config
import (
"os"
2016-06-17 01:11:15 +02:00
"strconv"
2016-05-30 16:08:09 +02:00
"github.com/imdario/mergo"
2016-07-06 20:24:34 +02:00
"github.com/kataras/fasthttp"
2016-05-30 16:08:09 +02:00
)
2016-07-06 20:24:34 +02:00
// Default values for base Server conf
2016-05-30 16:08:09 +02:00
const (
2016-06-17 01:11:15 +02:00
// DefaultServerHostname returns the default hostname which is 127.0.0.1
DefaultServerHostname = "127.0.0.1"
// DefaultServerPort returns the default port which is 8080
DefaultServerPort = 8080
2016-07-06 20:24:34 +02:00
// DefaultMaxRequestBodySize is 4MB
DefaultMaxRequestBodySize = fasthttp . DefaultMaxRequestBodySize
2016-06-17 01:11:15 +02:00
)
var (
2016-06-14 07:45:40 +02:00
// DefaultServerAddr the default server addr which is: 127.0.0.1:8080
2016-06-17 01:11:15 +02:00
DefaultServerAddr = DefaultServerHostname + ":" + strconv . Itoa ( DefaultServerPort )
2016-05-30 16:08:09 +02:00
)
// ServerName the response header of the 'Server' value when writes to the client
const ServerName = "iris"
// Server used inside server for listening
type Server struct {
// ListenningAddr the addr that server listens to
ListeningAddr string
CertFile string
KeyFile string
// Mode this is for unix only
Mode os . FileMode
2016-07-06 20:24:34 +02:00
// MaxRequestBodySize Maximum request body size.
//
// The server rejects requests with bodies exceeding this limit.
//
// By default request body size is 4MB.
MaxRequestBodySize int64
2016-06-29 17:49:09 +02:00
// RedirectTo, defaults to empty, set it in order to override the station's handler and redirect all requests to this address which is of form(HOST:PORT or :PORT)
//
// NOTE: the http status is 'StatusMovedPermanently', means one-time-redirect(the browser remembers the new addr and goes to the new address without need to request something from this server
// which means that if you want to change this address you have to clear your browser's cache in order this to be able to change to the new addr.
//
// example: https://github.com/iris-contrib/examples/tree/master/multiserver_listening2
RedirectTo string
2016-07-05 14:29:32 +02:00
// Virtual If this server is not really listens to a real host, it mostly used in order to achieve testing without system modifications
Virtual bool
2016-05-30 16:08:09 +02:00
}
// DefaultServer returns the default configs for the server
func DefaultServer ( ) Server {
2016-07-06 20:24:34 +02:00
return Server { ListeningAddr : DefaultServerAddr ,
MaxRequestBodySize : DefaultMaxRequestBodySize }
2016-05-30 16:08:09 +02:00
}
// Merge merges the default with the given config and returns the result
func ( c Server ) Merge ( cfg [ ] Server ) ( config Server ) {
if len ( cfg ) > 0 {
config = cfg [ 0 ]
mergo . Merge ( & config , c )
} else {
_default := c
config = _default
}
return
}
2016-07-06 20:24:34 +02:00
// MergeSingle merges the default with the given config and returns the result
func ( c Server ) MergeSingle ( cfg Server ) ( config Server ) {
config = cfg
mergo . Merge ( & config , c )
return
}