You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
779 B
33 lines
779 B
5 years ago
|
package webserver
|
||
|
|
||
|
type Server struct {
|
||
|
Timeout time.Duration
|
||
|
Port int
|
||
|
ListenAddress string
|
||
|
}
|
||
|
type Option func(*Server)
|
||
|
|
||
|
func Timeout(d time.Duration) Option {
|
||
|
return func(s *Server) { s.Timeout = d }
|
||
|
}
|
||
|
func Port(p int) Option {
|
||
|
return func(s *Server) { s.Port = p }
|
||
|
}
|
||
|
|
||
|
func New(opts ...Option) *Server {
|
||
|
s := &Server{ // initialise with default values
|
||
|
Timeout: 500*time.Millisecond,
|
||
|
Port: 0, // uses a random port on the host
|
||
|
ListenAddress: "http://localhost",
|
||
|
}
|
||
|
// apply all options
|
||
|
for _, opt := range opts {
|
||
|
opt(s)
|
||
|
}
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// usage examples from outside the package:
|
||
|
s := webserver.New() // uses all the default options
|
||
|
s := webserver.New(webserver.Timeout(time.Second), webserver.Port(8080))
|