config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package cproxy
  2. import (
  3. "net/http"
  4. "time"
  5. )
  6. func New(options ...option) http.Handler {
  7. var this configuration
  8. Options.apply(options...)(&this)
  9. return newHandler(this.Filter, this.ClientConnector, this.ServerConnector, this.Monitor)
  10. }
  11. var Options singleton
  12. type singleton struct{}
  13. type option func(*configuration)
  14. type configuration struct {
  15. DialTimeout time.Duration
  16. Filter Filter
  17. DialAddress string
  18. Dialer Dialer
  19. LogConnections bool
  20. ProxyProtocol bool
  21. Initializer initializer
  22. ClientConnector clientConnector
  23. ServerConnector serverConnector
  24. Monitor monitor
  25. Logger logger
  26. }
  27. func (singleton) DialTimeout(value time.Duration) option {
  28. return func(this *configuration) { this.DialTimeout = value }
  29. }
  30. func (singleton) Filter(value Filter) option {
  31. return func(this *configuration) { this.Filter = value }
  32. }
  33. func (singleton) ClientConnector(value clientConnector) option {
  34. return func(this *configuration) { this.ClientConnector = value }
  35. }
  36. func (singleton) DialAddress(value string) option {
  37. return func(this *configuration) { this.DialAddress = value }
  38. }
  39. func (singleton) Dialer(value Dialer) option {
  40. return func(this *configuration) { this.Dialer = value }
  41. }
  42. func (singleton) LogConnections(value bool) option {
  43. return func(this *configuration) { this.LogConnections = value }
  44. }
  45. func (singleton) ProxyProtocol(value bool) option {
  46. return func(this *configuration) { this.ProxyProtocol = value }
  47. }
  48. func (singleton) Initializer(value initializer) option {
  49. return func(this *configuration) { this.Initializer = value }
  50. }
  51. func (singleton) ServerConnector(value serverConnector) option {
  52. return func(this *configuration) { this.ServerConnector = value }
  53. }
  54. func (singleton) Monitor(value monitor) option {
  55. return func(this *configuration) { this.Monitor = value }
  56. }
  57. func (singleton) Logger(value logger) option {
  58. return func(this *configuration) { this.Logger = value }
  59. }
  60. func (singleton) apply(options ...option) option {
  61. return func(this *configuration) {
  62. for _, item := range Options.defaults(options...) {
  63. item(this)
  64. }
  65. if this.Dialer == nil {
  66. this.Dialer = newDialer(this)
  67. }
  68. this.Dialer = newRoutingDialer(this)
  69. if this.ProxyProtocol {
  70. this.Initializer = newProxyProtocolInitializer()
  71. }
  72. if this.Initializer == nil {
  73. this.Initializer = nop{}
  74. }
  75. this.Initializer = newLoggingInitializer(this)
  76. if this.ServerConnector == nil {
  77. this.ServerConnector = newServerConnector(this.Dialer, this.Initializer)
  78. }
  79. }
  80. }
  81. func (singleton) defaults(options ...option) []option {
  82. return append([]option{
  83. Options.DialTimeout(time.Second * 10),
  84. Options.Filter(newFilter()),
  85. Options.ClientConnector(newClientConnector()),
  86. Options.Initializer(nop{}),
  87. Options.Monitor(nop{}),
  88. Options.Logger(nop{}),
  89. }, options...)
  90. }
  91. type nop struct{}
  92. func (nop) Measure(int) {}
  93. func (nop) Printf(string, ...interface{}) {}
  94. func (nop) Initialize(Socket, Socket) bool { return true }