2016-05-30 16:08:09 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Currently only these 5 values are used for real
|
|
|
|
const (
|
2016-06-17 18:51:17 +02:00
|
|
|
// DefaultWriteTimeout 15 * time.Second
|
|
|
|
DefaultWriteTimeout = 15 * time.Second
|
|
|
|
// DefaultPongTimeout 60 * time.Second
|
|
|
|
DefaultPongTimeout = 60 * time.Second
|
2016-05-30 16:08:09 +02:00
|
|
|
// DefaultPingPeriod (DefaultPongTimeout * 9) / 10
|
|
|
|
DefaultPingPeriod = (DefaultPongTimeout * 9) / 10
|
2016-06-17 18:51:17 +02:00
|
|
|
// DefaultMaxMessageSize 1024
|
|
|
|
DefaultMaxMessageSize = 1024
|
2016-05-30 16:08:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Websocket the config contains options for 'websocket' package
|
|
|
|
type Websocket struct {
|
|
|
|
// WriteTimeout time allowed to write a message to the connection.
|
2016-06-17 18:51:17 +02:00
|
|
|
// Default value is 15 * time.Second
|
2016-05-30 16:08:09 +02:00
|
|
|
WriteTimeout time.Duration
|
|
|
|
// PongTimeout allowed to read the next pong message from the connection
|
2016-06-17 18:51:17 +02:00
|
|
|
// Default value is 60 * time.Second
|
2016-05-30 16:08:09 +02:00
|
|
|
PongTimeout time.Duration
|
|
|
|
// PingPeriod send ping messages to the connection with this period. Must be less than PongTimeout
|
|
|
|
// Default value is (PongTimeout * 9) / 10
|
|
|
|
PingPeriod time.Duration
|
|
|
|
// MaxMessageSize max message size allowed from connection
|
2016-06-17 18:51:17 +02:00
|
|
|
// Default value is 1024
|
2016-06-15 23:13:56 +02:00
|
|
|
MaxMessageSize int64
|
2016-05-30 16:08:09 +02:00
|
|
|
// Endpoint is the path which the websocket server will listen for clients/connections
|
|
|
|
// Default value is empty string, if you don't set it the Websocket server is disabled.
|
|
|
|
Endpoint string
|
|
|
|
// Headers the response headers before upgrader
|
|
|
|
// Default is empty
|
|
|
|
Headers map[string]string
|
2016-06-16 19:08:59 +02:00
|
|
|
// ReadBufferSize is the buffer size for the underline reader
|
|
|
|
ReadBufferSize int
|
|
|
|
// WriteBufferSize is the buffer size for the underline writer
|
|
|
|
WriteBufferSize int
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultWebsocket returns the default config for iris-ws websocket package
|
2016-06-14 07:45:40 +02:00
|
|
|
func DefaultWebsocket() *Websocket {
|
|
|
|
return &Websocket{
|
2016-06-16 19:08:59 +02:00
|
|
|
WriteTimeout: DefaultWriteTimeout,
|
|
|
|
PongTimeout: DefaultPongTimeout,
|
|
|
|
PingPeriod: DefaultPingPeriod,
|
|
|
|
MaxMessageSize: DefaultMaxMessageSize,
|
|
|
|
ReadBufferSize: 4096,
|
|
|
|
WriteBufferSize: 4096,
|
|
|
|
Headers: make(map[string]string, 0),
|
|
|
|
Endpoint: "",
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges the default with the given config and returns the result
|
2016-06-14 07:45:40 +02:00
|
|
|
func (c *Websocket) Merge(cfg []*Websocket) (config *Websocket) {
|
2016-05-30 16:08:09 +02:00
|
|
|
|
|
|
|
if len(cfg) > 0 {
|
|
|
|
config = cfg[0]
|
2016-06-14 07:45:40 +02:00
|
|
|
mergo.Merge(config, c)
|
2016-05-30 16:08:09 +02:00
|
|
|
} else {
|
|
|
|
_default := c
|
|
|
|
config = _default
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeSingle merges the default with the given config and returns the result
|
2016-06-14 07:45:40 +02:00
|
|
|
func (c *Websocket) MergeSingle(cfg *Websocket) (config *Websocket) {
|
2016-05-30 16:08:09 +02:00
|
|
|
|
|
|
|
config = cfg
|
2016-06-14 07:45:40 +02:00
|
|
|
mergo.Merge(config, c)
|
2016-05-30 16:08:09 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|