option.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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) {
  46. r.RequestTimeOut = v
  47. }
  48. // SetResponseHeaderTimeout set response header timeout
  49. func (r *RequestOption) SetResponseHeaderTimeout(v time.Duration) {
  50. r.ResponseHeaderTimeout = v
  51. }
  52. // SetCert set request cert & key file
  53. func (r *RequestOption) SetCert(certFile, keyFile string) {
  54. r.CertFile = certFile
  55. r.KeyFile = keyFile
  56. }
  57. // SetHeader set header
  58. func (r *RequestOption) SetHeader(k, v string) {
  59. r.Headers[k] = v
  60. }
  61. // AcceptEncodingGZIP Accept-Encoding gzip
  62. func (r *RequestOption) AcceptEncodingGZIP() {
  63. r.Headers[AcceptEncoding] = "gzip, deflate, br"
  64. }
  65. // SetContentType set Content-Type
  66. func (r *RequestOption) SetContentType(v string) {
  67. r.Headers[ContentType] = v
  68. }
  69. // SetContentTypeJSON Content-Type json
  70. func (r *RequestOption) SetContentTypeJSON() {
  71. r.Headers[ContentType] = ContentTypeJSON
  72. }
  73. // SetContentTypeJSON Content-Type json
  74. func (r *RequestOption) SetContentTypeURL() {
  75. r.Headers[ContentType] = ContentTypeURL
  76. }
  77. // SetContentTypeJSON Content-Type json
  78. func (r *RequestOption) SetContentTypeXML() {
  79. r.Headers[ContentType] = ContentTypeXML
  80. }
  81. func DefaultServerOption() ServerOption {
  82. return ServerOption{
  83. ReadTimeout: readTimeout,
  84. ReadHeaderTimeout: readHeaderTimeout,
  85. WriteTimeout: writeTimeout,
  86. IdleTimeout: idleTimeout,
  87. MaxHeaderBytes: 1 << 20,
  88. ShutdownTimeout: shutTimeout,
  89. }
  90. }
  91. // SetReadTimeout set ReadTimeout
  92. func (s *ServerOption) SetReadTimeout(t time.Duration) {
  93. s.ReadTimeout = t
  94. }
  95. // SetReadHeaderTimeout set ReadHeaderTimeout
  96. func (s *ServerOption) SetReadHeaderTimeout(t time.Duration) {
  97. s.ReadHeaderTimeout = t
  98. }
  99. // SetWriteTimeout set WriteTimeout
  100. func (s *ServerOption) SetWriteTimeout(t time.Duration) {
  101. s.WriteTimeout = t
  102. }
  103. // SetIdleTimeout set IdleTimeout
  104. func (s *ServerOption) SetIdleTimeout(t time.Duration) {
  105. s.IdleTimeout = t
  106. }
  107. // SetMaxHeaderBytes set MaxHeaderBytes
  108. func (s *ServerOption) SetMaxHeaderBytes(n int) {
  109. s.MaxHeaderBytes = n
  110. }
  111. // SetShutdownTimeout set ShutdownTimeout
  112. func (s *ServerOption) SetShutdownTimeout(t time.Duration) {
  113. s.ShutdownTimeout = t
  114. }
  115. // SetCertFile set CertFile
  116. func (s *ServerOption) SetCertFile(path string) {
  117. s.CertFile = path
  118. }
  119. // SetKeyFile set KeyFile
  120. func (s *ServerOption) SetKeyFile(path string) {
  121. s.KeyFile = path
  122. }