diff options
| author | mo khan <mo@mokhan.ca> | 2025-03-13 09:16:39 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-03-13 09:16:39 -0600 |
| commit | fa141110137caafd40ab62d33fe8acc3048d4c84 (patch) | |
| tree | 269219ff5984935e8a1b0182aa536c5eb4ed3d0f /pkg/cfg | |
| parent | 6ad85c4bd167c9f114c1d2574f4887826689b76d (diff) | |
refactor: extract cfg package
Diffstat (limited to 'pkg/cfg')
| -rw-r--r-- | pkg/cfg/cfg.go | 34 | ||||
| -rw-r--r-- | pkg/cfg/mux.go | 9 | ||||
| -rw-r--r-- | pkg/cfg/option.go | 3 |
3 files changed, 46 insertions, 0 deletions
diff --git a/pkg/cfg/cfg.go b/pkg/cfg/cfg.go new file mode 100644 index 00000000..0d7a6427 --- /dev/null +++ b/pkg/cfg/cfg.go @@ -0,0 +1,34 @@ +package cfg + +import ( + "crypto/tls" + "net/http" +) + +type Config struct { + BindAddress string + Mux http.Handler + TLS *tls.Config +} + +func New(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() +} diff --git a/pkg/cfg/mux.go b/pkg/cfg/mux.go new file mode 100644 index 00000000..694ee9e7 --- /dev/null +++ b/pkg/cfg/mux.go @@ -0,0 +1,9 @@ +package cfg + +import "net/http" + +func WithMux(mux http.Handler) Option { + return func(config *Config) { + config.Mux = mux + } +} diff --git a/pkg/cfg/option.go b/pkg/cfg/option.go new file mode 100644 index 00000000..0f3e87d8 --- /dev/null +++ b/pkg/cfg/option.go @@ -0,0 +1,3 @@ +package cfg + +type Option func(*Config) |
