github.com/openziti/transport@v0.1.5/ws/config.go (about)

     1  package ws
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/openziti/transport"
     6  	"github.com/pkg/errors"
     7  	"time"
     8  )
     9  
    10  type WSConfig struct {
    11  	writeTimeout      time.Duration
    12  	readTimeout       time.Duration
    13  	idleTimeout       time.Duration
    14  	pongTimeout       time.Duration
    15  	pingInterval      time.Duration
    16  	handshakeTimeout  time.Duration
    17  	readBufferSize    int
    18  	writeBufferSize   int
    19  	enableCompression bool
    20  	serverCert        string
    21  	key               string
    22  }
    23  
    24  func NewDefaultWSConfig() *WSConfig {
    25  	return &WSConfig{
    26  		writeTimeout:      transport.DefaultWsWriteTimeout,
    27  		readTimeout:       transport.DefaultWsReadTimeout,
    28  		idleTimeout:       transport.DefaultWsIdleTimeout,
    29  		pongTimeout:       transport.DefaultWsPongTimeout,
    30  		handshakeTimeout:  transport.DefaultWsHandshakeTimeout,
    31  		readBufferSize:    transport.DefaultWsReadBufferSize,
    32  		writeBufferSize:   transport.DefaultWsWriteBufferSize,
    33  		enableCompression: transport.DefaultWsEnableCompression,
    34  	}
    35  }
    36  
    37  func (self *WSConfig) Load(data map[interface{}]interface{}) error {
    38  	if v, found := data["writeTimeout"]; found {
    39  		if i, ok := v.(int); ok {
    40  			self.writeTimeout = time.Second * time.Duration(i)
    41  		} else {
    42  			return errors.New("invalid 'writeTimeout' value")
    43  		}
    44  	}
    45  	if v, found := data["readTimeout"]; found {
    46  		if i, ok := v.(int); ok {
    47  			self.readTimeout = time.Second * time.Duration(i)
    48  		} else {
    49  			return errors.New("invalid 'readTimeout' value")
    50  		}
    51  	}
    52  	if v, found := data["idleTimeout"]; found {
    53  		if i, ok := v.(int); ok {
    54  			self.idleTimeout = time.Second * time.Duration(i)
    55  		} else {
    56  			return errors.New("invalid 'idleTimeout' value")
    57  		}
    58  	}
    59  	if v, found := data["pongTimeout"]; found {
    60  		if i, ok := v.(int); ok {
    61  			self.pongTimeout = time.Second * time.Duration(i)
    62  		} else {
    63  			return errors.New("invalid 'pongTimeout' value")
    64  		}
    65  	}
    66  	if v, found := data["pingInterval"]; found {
    67  		if i, ok := v.(int); ok {
    68  			self.pingInterval = time.Second * time.Duration(i)
    69  		} else {
    70  			return errors.New("invalid 'pingInterval' value")
    71  		}
    72  	} else {
    73  		self.pingInterval = transport.DefaultWsPingInterval
    74  	}
    75  	if v, found := data["handshakeTimeout"]; found {
    76  		if i, ok := v.(int); ok {
    77  			self.handshakeTimeout = time.Second * time.Duration(i)
    78  		} else {
    79  			return errors.New("invalid 'handshakeTimeout' value")
    80  		}
    81  	}
    82  	if v, found := data["readBufferSize"]; found {
    83  		if i, ok := v.(int); ok {
    84  			self.readBufferSize = i
    85  		} else {
    86  			return errors.New("invalid 'readBufferSize' value")
    87  		}
    88  	}
    89  	if v, found := data["writeBufferSize"]; found {
    90  		if i, ok := v.(int); ok {
    91  			self.writeBufferSize = i
    92  		} else {
    93  			return errors.New("invalid 'writeBufferSize' value")
    94  		}
    95  	}
    96  	if v, found := data["enableCompression"]; found {
    97  		if i, ok := v.(bool); ok {
    98  			self.enableCompression = i
    99  		} else {
   100  			return errors.New("invalid 'enableCompression' value")
   101  		}
   102  	}
   103  	if v, found := data["server_cert"]; found {
   104  		if s, ok := v.(string); ok {
   105  			self.serverCert = s
   106  		} else {
   107  			return errors.New("invalid 'server_cert' value")
   108  		}
   109  	}
   110  	if v, found := data["key"]; found {
   111  		if s, ok := v.(string); ok {
   112  			self.key = s
   113  		} else {
   114  			return errors.New("invalid 'key' value")
   115  		}
   116  	}
   117  
   118  	if len(self.serverCert) == 0 {
   119  		return errors.New("transport.ws.serverCert was not specified'")
   120  	}
   121  	if len(self.key) == 0 {
   122  		return errors.New("transport.ws.key was not specified'")
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func (self *WSConfig) Dump() string {
   129  	out := "ws.Config{\n"
   130  	out += fmt.Sprintf("\t%-30s %d\n", "writeTimeout", self.writeTimeout)
   131  	out += fmt.Sprintf("\t%-30s %d\n", "readTimeout", self.readTimeout)
   132  	out += fmt.Sprintf("\t%-30s %d\n", "idleTimeout", self.idleTimeout)
   133  	out += fmt.Sprintf("\t%-30s %d\n", "pongTimeout", self.pongTimeout)
   134  	out += fmt.Sprintf("\t%-30s %d\n", "handshakeTimeout", self.handshakeTimeout)
   135  	out += fmt.Sprintf("\t%-30s %d\n", "readBufferSize", self.readBufferSize)
   136  	out += fmt.Sprintf("\t%-30s %d\n", "writeBufferSize", self.writeBufferSize)
   137  	out += fmt.Sprintf("\t%-30s %t\n", "enableCompression", self.enableCompression)
   138  	out += fmt.Sprintf("\t%-30s %s\n", "serverCert", self.serverCert)
   139  	out += fmt.Sprintf("\t%-30s %s\n", "key", self.key)
   140  	out += "}"
   141  	return out
   142  }