summaryrefslogtreecommitdiff
path: root/pkg/srv/config.go
blob: c02a9c7a80bc6d78d30f056777949a59d177fea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package srv

import (
	"crypto/tls"
	"net/http"
)

type Option func(*Config)

type Config struct {
	BindAddress string
	Mux         http.Handler
	TLS         *tls.Config
}

func WithMux(mux http.Handler) Option {
	return func(config *Config) {
		config.Mux = mux
	}
}

func NewConfig(addr string, options ...Option) *Config {
	if addr == "" {
		addr = ":0"
	}

	c := &Config{
		BindAddress: addr,
		Mux:         http.DefaultServeMux,
	}
	for _, option := range options {
		option(c)
	}
	return c
}

func (c *Config) Run(server *http.Server) error {
	if c.TLS != nil {
		return server.ListenAndServeTLS("", "")
	}
	return server.ListenAndServe()
}