From fa141110137caafd40ab62d33fe8acc3048d4c84 Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 13 Mar 2025 09:16:39 -0600 Subject: refactor: extract cfg package --- pkg/cfg/cfg.go | 34 ++++++++++++++++++++++++++++++++++ pkg/cfg/mux.go | 9 +++++++++ pkg/cfg/option.go | 3 +++ pkg/srv/config.go | 42 ------------------------------------------ pkg/srv/srv.go | 12 +++++++----- 5 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 pkg/cfg/cfg.go create mode 100644 pkg/cfg/mux.go create mode 100644 pkg/cfg/option.go delete mode 100644 pkg/srv/config.go (limited to 'pkg') diff --git a/pkg/cfg/cfg.go b/pkg/cfg/cfg.go new file mode 100644 index 0000000..0d7a642 --- /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 0000000..694ee9e --- /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 0000000..0f3e87d --- /dev/null +++ b/pkg/cfg/option.go @@ -0,0 +1,3 @@ +package cfg + +type Option func(*Config) diff --git a/pkg/srv/config.go b/pkg/srv/config.go deleted file mode 100644 index c02a9c7..0000000 --- a/pkg/srv/config.go +++ /dev/null @@ -1,42 +0,0 @@ -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() -} diff --git a/pkg/srv/srv.go b/pkg/srv/srv.go index a2be363..a6261ef 100644 --- a/pkg/srv/srv.go +++ b/pkg/srv/srv.go @@ -4,13 +4,15 @@ import ( "log" "net/http" "time" + + "gitlab.com/mokhax/spike/pkg/cfg" ) -func New(cfg *Config) *http.Server { +func New(c *cfg.Config) *http.Server { return &http.Server{ - Addr: cfg.BindAddress, - Handler: cfg.Mux, - TLSConfig: cfg.TLS, + Addr: c.BindAddress, + Handler: c.Mux, + TLSConfig: c.TLS, ReadHeaderTimeout: 10 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 2 * time.Minute, @@ -19,6 +21,6 @@ func New(cfg *Config) *http.Server { } } -func Run(c *Config) error { +func Run(c *cfg.Config) error { return c.Run(New(c)) } -- cgit v1.2.3