option.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package xhttp
  2. import (
  3. "time"
  4. )
  5. // RequestOption HTTP request option
  6. type RequestOption struct {
  7. RequestTimeOut time.Duration
  8. ResponseHeaderTimeout time.Duration
  9. Headers map[string]string
  10. CertFile string
  11. KeyFile string
  12. }
  13. // ServerOption serve option
  14. type ServerOption struct {
  15. CertFile string
  16. KeyFile string
  17. // ReadHeaderTimeout is the amount of time allowed to read request headers
  18. ReadHeaderTimeout time.Duration
  19. // ReadTimeout is the maximum duration for reading the entire
  20. // request, including the body.
  21. ReadTimeout time.Duration
  22. // WriteTimeout is the maximum duration before timing out
  23. // writes of the response.
  24. WriteTimeout time.Duration
  25. // IdleTimeout is the maximum amount of time to wait for the
  26. // next request when keep-alives are enabled.
  27. IdleTimeout time.Duration
  28. // MaxHeaderBytes controls the maximum number of bytes the
  29. // server will read parsing the request header's keys and
  30. // values, including the request line. It does not limit the
  31. // size of the request body.
  32. // If zero, DefaultMaxHeaderBytes is used.
  33. MaxHeaderBytes int
  34. ShutdownTimeout time.Duration
  35. }
  36. // DefaultRequestOption default request option
  37. func DefaultRequestOption() RequestOption {
  38. return RequestOption{
  39. Headers: map[string]string{},
  40. RequestTimeOut: 60 * time.Second,
  41. ResponseHeaderTimeout: 60 * time.Second,
  42. }
  43. }
  44. // SetTimeOut set request timeout
  45. func (r *RequestOption) SetTimeOut(v time.Duration) *RequestOption {
  46. r.RequestTimeOut = v
  47. return r
  48. }
  49. // SetResponseHeaderTimeout set response header timeout
  50. func (r *RequestOption) SetResponseHeaderTimeout(v time.Duration) *RequestOption {
  51. r.ResponseHeaderTimeout = v
  52. return r
  53. }
  54. // SetCert set request cert & key file
  55. func (r *RequestOption) SetCert(certFile, keyFile string) *RequestOption {
  56. r.CertFile = certFile
  57. r.KeyFile = keyFile
  58. return r
  59. }
  60. // SetHeader set header
  61. func (r *RequestOption) SetHeader(k, v string) *RequestOption {
  62. r.Headers[k] = v
  63. return r
  64. }
  65. // AcceptEncodingGZIP Accept-Encoding gzip
  66. func (r *RequestOption) AcceptEncodingGZIP() *RequestOption {
  67. r.Headers[AcceptEncoding] = "gzip, deflate, br"
  68. return r
  69. }
  70. // SetContentType set Content-Type
  71. func (r *RequestOption) SetContentType(v string) *RequestOption {
  72. r.Headers[ContentType] = v
  73. return r
  74. }
  75. // SetContentTypeJSON Content-Type json
  76. func (r *RequestOption) SetContentTypeJSON() *RequestOption {
  77. r.Headers[ContentType] = ContentTypeJSON
  78. return r
  79. }
  80. // SetContentTypeJSON Content-Type json
  81. func (r *RequestOption) SetContentTypeURL() *RequestOption {
  82. r.Headers[ContentType] = ContentTypeURL
  83. return r
  84. }
  85. // SetContentTypeJSON Content-Type json
  86. func (r *RequestOption) SetContentTypeXML() *RequestOption {
  87. r.Headers[ContentType] = ContentTypeXML
  88. return r
  89. }
  90. func DefaultServerOption() ServerOption {
  91. return ServerOption{
  92. ReadTimeout: readTimeout,
  93. ReadHeaderTimeout: readHeaderTimeout,
  94. WriteTimeout: writeTimeout,
  95. IdleTimeout: idleTimeout,
  96. MaxHeaderBytes: 1 << 20,
  97. ShutdownTimeout: shutTimeout,
  98. }
  99. }
  100. // SetReadTimeout set ReadTimeout
  101. func (s *ServerOption) SetReadTimeout(t time.Duration) *ServerOption {
  102. s.ReadTimeout = t
  103. return s
  104. }
  105. // SetReadHeaderTimeout set ReadHeaderTimeout
  106. func (s *ServerOption) SetReadHeaderTimeout(t time.Duration) *ServerOption {
  107. s.ReadHeaderTimeout = t
  108. return s
  109. }
  110. // SetWriteTimeout set WriteTimeout
  111. func (s *ServerOption) SetWriteTimeout(t time.Duration) *ServerOption {
  112. s.WriteTimeout = t
  113. return s
  114. }
  115. // SetIdleTimeout set IdleTimeout
  116. func (s *ServerOption) SetIdleTimeout(t time.Duration) *ServerOption {
  117. s.IdleTimeout = t
  118. return s
  119. }
  120. // SetMaxHeaderBytes set MaxHeaderBytes
  121. func (s *ServerOption) SetMaxHeaderBytes(n int) *ServerOption {
  122. s.MaxHeaderBytes = n
  123. return s
  124. }
  125. // SetShutdownTimeout set ShutdownTimeout
  126. func (s *ServerOption) SetShutdownTimeout(t time.Duration) *ServerOption {
  127. s.ShutdownTimeout = t
  128. return s
  129. }
  130. // SetCertFile set CertFile
  131. func (s *ServerOption) SetCertFile(path string) *ServerOption {
  132. s.CertFile = path
  133. return s
  134. }
  135. // SetKeyFile set KeyFile
  136. func (s *ServerOption) SetKeyFile(path string) *ServerOption {
  137. s.KeyFile = path
  138. return s
  139. }