package xhttp import ( "time" ) // RequestOption HTTP request option type RequestOption struct { RequestTimeOut time.Duration ResponseHeaderTimeout time.Duration Headers map[string]string CertFile string KeyFile string } // ServerOption serve option type ServerOption struct { CertFile string KeyFile string // ReadHeaderTimeout is the amount of time allowed to read request headers ReadHeaderTimeout time.Duration // ReadTimeout is the maximum duration for reading the entire // request, including the body. ReadTimeout time.Duration // WriteTimeout is the maximum duration before timing out // writes of the response. WriteTimeout time.Duration // IdleTimeout is the maximum amount of time to wait for the // next request when keep-alives are enabled. IdleTimeout time.Duration // 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. MaxHeaderBytes int ShutdownTimeout time.Duration } // DefaultRequestOption default request option func DefaultRequestOption() RequestOption { return RequestOption{ Headers: map[string]string{}, RequestTimeOut: 60 * time.Second, ResponseHeaderTimeout: 60 * time.Second, } } // SetTimeOut set request timeout func (r *RequestOption) SetTimeOut(v time.Duration) { r.RequestTimeOut = v } // SetResponseHeaderTimeout set response header timeout func (r *RequestOption) SetResponseHeaderTimeout(v time.Duration) { r.ResponseHeaderTimeout = v } // SetCert set request cert & key file func (r *RequestOption) SetCert(certFile, keyFile string) { r.CertFile = certFile r.KeyFile = keyFile } // SetHeader set header func (r *RequestOption) SetHeader(k, v string) { r.Headers[k] = v } // AcceptEncodingGZIP Accept-Encoding gzip func (r *RequestOption) AcceptEncodingGZIP() { r.Headers[AcceptEncoding] = "gzip, deflate, br" } // SetContentType set Content-Type func (r *RequestOption) SetContentType(v string) { r.Headers[ContentType] = v } // SetContentTypeJSON Content-Type json func (r *RequestOption) SetContentTypeJSON() { r.Headers[ContentType] = ContentTypeJSON } // SetContentTypeJSON Content-Type json func (r *RequestOption) SetContentTypeURL() { r.Headers[ContentType] = ContentTypeURL } // SetContentTypeJSON Content-Type json func (r *RequestOption) SetContentTypeXML() { r.Headers[ContentType] = ContentTypeXML } func DefaultServerOption() ServerOption { return ServerOption{ ReadTimeout: readTimeout, ReadHeaderTimeout: readHeaderTimeout, WriteTimeout: writeTimeout, IdleTimeout: idleTimeout, MaxHeaderBytes: 1 << 20, ShutdownTimeout: shutTimeout, } } // SetReadTimeout set ReadTimeout func (s *ServerOption) SetReadTimeout(t time.Duration) { s.ReadTimeout = t } // SetReadHeaderTimeout set ReadHeaderTimeout func (s *ServerOption) SetReadHeaderTimeout(t time.Duration) { s.ReadHeaderTimeout = t } // SetWriteTimeout set WriteTimeout func (s *ServerOption) SetWriteTimeout(t time.Duration) { s.WriteTimeout = t } // SetIdleTimeout set IdleTimeout func (s *ServerOption) SetIdleTimeout(t time.Duration) { s.IdleTimeout = t } // SetMaxHeaderBytes set MaxHeaderBytes func (s *ServerOption) SetMaxHeaderBytes(n int) { s.MaxHeaderBytes = n } // SetShutdownTimeout set ShutdownTimeout func (s *ServerOption) SetShutdownTimeout(t time.Duration) { s.ShutdownTimeout = t } // SetCertFile set CertFile func (s *ServerOption) SetCertFile(path string) { s.CertFile = path } // SetKeyFile set KeyFile func (s *ServerOption) SetKeyFile(path string) { s.KeyFile = path }