diff options
| author | 2026-05-02 19:47:34 +0700 | |
|---|---|---|
| committer | 2026-05-02 19:47:34 +0700 | |
| commit | 179cf888d20537a5f6e581256be95ad1e2f92d6b (patch) | |
| tree | 72637341bf1e074252c2acc57ce122f2b235978a /internal | |
| parent | 03ff00d2e2e59f695bb14f346b4cc8fdbfc5d12a (diff) | |
| download | web-179cf888d20537a5f6e581256be95ad1e2f92d6b.tar.gz web-179cf888d20537a5f6e581256be95ad1e2f92d6b.zip | |
feat: enable unix listeners
refactor: parametrize listener
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/router/router.go | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/internal/router/router.go b/internal/router/router.go index 5230e74..2b45761 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -7,8 +7,11 @@ import ( "fmt" "html/template" "log/slog" + "net" "net/url" + "os" "slices" + "strconv" "strings" "time" @@ -112,6 +115,7 @@ type Router struct { templatedRoutes map[string]map[string]Route templatedPathMatcher *PathMatcher canonicalEndpoint string + endpoint config.EndpointConfig } func NewRouter(cfg *config.Config) (*Router, error) { @@ -256,7 +260,7 @@ func NewRouter(cfg *config.Config) (*Router, error) { templatedRoutes := make(map[string]map[string]Route) templatedPathMatcher := NewPathMatcher() - return &Router{supplements, app, templatedRoutes, templatedPathMatcher, cfg.CanonicalEndpoint}, nil + return &Router{supplements, app, templatedRoutes, templatedPathMatcher, cfg.CanonicalEndpoint, cfg.Endpoint}, nil } func (r *Router) InitRoutes() (err error) { @@ -382,10 +386,30 @@ func (r *Router) InitRoutes() (err error) { return nil } -func (r *Router) Listen(endpoint string) error { - if err := r.app.Listen(endpoint); err != nil { - return fmt.Errorf("error while running fiber server: %w", err) +func (r *Router) Listen() error { + switch r.endpoint.Type { + case "unix": + unixConfig := r.endpoint.Config.(*config.UnixConfig) + endpoint, _ := strings.CutPrefix(unixConfig.Path, "unix://") + ln, err := net.Listen("unix", endpoint) + if err != nil { + return fmt.Errorf("error while initializing unix listener: %w", err) + } + chmod, _ := strconv.ParseUint(unixConfig.Chmod[1:], 8, 32) + os.Chmod(unixConfig.Path, os.FileMode(chmod)) + if err := r.app.Listener(ln); err != nil { + return fmt.Errorf("error while running fiber server: %w", err) + } + case "http": + httpConfig := r.endpoint.Config.(*config.HttpConfig) + fmt.Print(httpConfig) + if err := r.app.Listen(httpConfig.ListenOn); err != nil { + return fmt.Errorf("error while running fiber server: %w", err) + } + default: + return fmt.Errorf("error with initializing fiber server: invalid endpoint type (supported are unix and http)") } + return nil } @@ -409,6 +433,12 @@ func (r *Router) Close() (err error) { } slog.Debug("closing page cache") r.supplements.PageCache.Close() + if r.endpoint.Type == "unix" { + slog.Debug("closing unix socket") + if err = os.Remove(r.endpoint.Config.(*config.UnixConfig).Path); err != nil { + allErrors = append(allErrors, fmt.Errorf("fail to close unix socket: %w", err)) + } + } return errors.Join(allErrors...) } |