diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-11 20:59:02 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-11 21:00:12 +0200 |
commit | 7ff4cac4fc23060a56b9c33a3453c2d26629b699 (patch) | |
tree | 0fb0dcc9b11b27d54395d425e4cb3049fba0c398 | |
parent | 2fcc394c53f995750b52ad06153041f61f0a0c55 (diff) | |
download | cerrado-0.0.1.tar.gz cerrado-0.0.1.tar.bz2 cerrado-0.0.1.zip |
feat: Add server listen configurationv0.0.1
-rw-r--r-- | config.example.scfg | 5 | ||||
-rw-r--r-- | main.go | 6 | ||||
-rw-r--r-- | pkg/config/config.go | 18 | ||||
-rw-r--r-- | pkg/worker/http.go | 49 |
4 files changed, 68 insertions, 10 deletions
diff --git a/config.example.scfg b/config.example.scfg index 3961e51..9de249b 100644 --- a/config.example.scfg +++ b/config.example.scfg @@ -1,4 +1,9 @@ +# for tcp biding +# listen-addr tcp://localhost:8080 +listen-addr unix://var/run/cerrado.sock + root-readme /srv/git/README.md + scan /srv/git/ { public true } @@ -4,7 +4,6 @@ import ( "context" "flag" "log/slog" - "net/http" "os" "os/signal" "time" @@ -16,7 +15,6 @@ import ( ) func main() { - ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) defer stop() if err := run(ctx); err != nil { @@ -27,7 +25,7 @@ func main() { func run(ctx context.Context) error { var ( - configPath = flag.String("config", "config.example.scfg", "File path for the configuration file") + configPath = flag.String("config", "/etc/cerrado.scfg", "File path for the configuration file") ) flag.Parse() @@ -46,7 +44,7 @@ func run(ctx context.Context) error { return err } - serverTask := worker.NewServerTask(&http.Server{Handler: handler, Addr: "0.0.0.0:8080"}) + serverTask := worker.NewServerTask(configRepo.GetListenAddr(), handler) pool := worker.NewTaskPool() pool.AddTask("http-server", 5*time.Second, serverTask) 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 } } |