github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/transport/http/options.go (about) 1 package http 2 3 import ( 4 "time" 5 ) 6 7 // Option is HTTP server option 8 type Option func(*options) 9 10 type options struct { 11 network string 12 address string 13 readTimeout time.Duration 14 writeTimeout time.Duration 15 } 16 17 // WithAddress with server address. 18 func WithAddress(addr string) Option { 19 return func(s *options) { 20 s.address = addr 21 } 22 } 23 24 // WithReadTimeout with read timeout. 25 func WithReadTimeout(timeout time.Duration) Option { 26 return func(s *options) { 27 s.readTimeout = timeout 28 } 29 } 30 31 // WithWriteTimeout with write timeout. 32 func WithWriteTimeout(timeout time.Duration) Option { 33 return func(s *options) { 34 s.writeTimeout = timeout 35 } 36 } 37 38 func newOptions(opt ...Option) options { 39 opts := options{ 40 network: "tcp", 41 address: ":9050", 42 readTimeout: 5 * time.Second, 43 writeTimeout: 5 * time.Second, 44 } 45 for _, o := range opt { 46 o(&opts) 47 } 48 49 return opts 50 }