From 7ff4cac4fc23060a56b9c33a3453c2d26629b699 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Tue, 11 Jun 2024 20:59:02 +0200 Subject: feat: Add server listen configuration --- pkg/config/config.go | 18 ++++++++++++++++++ pkg/worker/http.go | 49 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 6 deletions(-) (limited to 'pkg') diff --git a/pkg/config/config.go b/pkg/config/config.go index 3e539f7..0e85b5a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -32,6 +32,7 @@ type ( configuration struct { Scan *scan RootReadme string + ListenAddr string Repositories []*GitRepositoryConfiguration } @@ -49,6 +50,7 @@ type ( // information. ConfigurationRepository struct { rootReadme string + listenAddr string repositories []*GitRepositoryConfiguration } ) @@ -66,6 +68,7 @@ func LoadConfigurationRepository(configPath string) (*ConfigurationRepository, e repo := &ConfigurationRepository{ rootReadme: config.RootReadme, + listenAddr: config.ListenAddr, repositories: config.Repositories, } @@ -85,6 +88,10 @@ func (c *ConfigurationRepository) GetRootReadme() string { return c.rootReadme } +func (c *ConfigurationRepository) GetListenAddr() string { + return c.listenAddr +} + // GetByName returns configuration of repository for a given name. // It returns nil if there is not match for it. func (c *ConfigurationRepository) GetByName(name string) *GitRepositoryConfiguration { @@ -157,6 +164,11 @@ func parse(r io.Reader) (*configuration, error) { return nil, err } + err = setListenAddr(block, &config.ListenAddr) + if err != nil { + return nil, err + } + err = setRepositories(block, &config.Repositories) if err != nil { return nil, err @@ -215,6 +227,7 @@ func defaultConfiguration() *configuration { return &configuration{ Scan: defaultScan(), RootReadme: "", + ListenAddr: "http//0.0.0.0:8080", Repositories: make([]*GitRepositoryConfiguration, 0), } } @@ -241,6 +254,11 @@ func setRootReadme(block scfg.Block, readme *string) error { return setString(scanDir, readme) } +func setListenAddr(block scfg.Block, listenAddr *string) error { + scanDir := block.Get("listen-addr") + return setString(scanDir, listenAddr) +} + func setScan(block scfg.Block, scan *scan) error { scanDir := block.Get("scan") if scanDir == nil { diff --git a/pkg/worker/http.go b/pkg/worker/http.go index 973775e..c4f9950 100644 --- a/pkg/worker/http.go +++ b/pkg/worker/http.go @@ -2,24 +2,41 @@ package worker import ( "context" + "errors" + "net" "net/http" + "net/url" +) + +var ( + UnsupportedSchemeErr = errors.New("Ivalid schema, only tcp and unix supported") ) type ServerTask struct { - server *http.Server + addr string + handler http.Handler } -func NewServerTask(server *http.Server) *ServerTask { +func NewServerTask(addr string, handler http.Handler) *ServerTask { return &ServerTask{ - server: server, + addr: addr, + handler: handler, } } -func (self *ServerTask) Start(ctx context.Context) error { +func (s *ServerTask) Start(ctx context.Context) error { done := make(chan error) + listen, err := getListen(s.addr) + if err != nil { + return err + } + server := &http.Server{ + Handler: s.handler, + } + go func() { - done <- self.server.ListenAndServe() + done <- server.Serve(listen) }() select { @@ -33,6 +50,26 @@ func (self *ServerTask) Start(ctx context.Context) error { // shutdown, and return its error, which is most cases, but not limited, is // context.Canceled. case <-ctx.Done(): - return self.server.Shutdown(ctx) + return server.Shutdown(ctx) + } +} + +func getListen(addr string) (net.Listener, error) { + u, err := url.Parse(addr) + if err != nil { + return nil, err + } + + switch u.Scheme { + case "tcp": + return net.Listen(u.Scheme, u.Host) + case "unix": + host, err := url.JoinPath("/", u.Host, u.Path) + if err != nil { + return nil, err + } + return net.Listen(u.Scheme, host) + default: + return nil, UnsupportedSchemeErr } } -- cgit v1.2.3