feat: add native fnOS gateway socket and base path support
CI / verify (push) Canceled after 0s

This commit is contained in:
2026-10-02 14:59:57 +08:00
parent c5502b0f95
commit 419c205103
7 changed files with 69 additions and 9 deletions
+48 -1
View File
@@ -2,13 +2,16 @@ package app
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/havline/havline/internal/acme"
@@ -144,6 +147,9 @@ func New(cfg config.Config, staticFS fs.FS, migrationsDir string) (*App, error)
StaticFS: staticFS,
StartedAt: startedAt,
})
if prefix := strings.TrimSpace(cfg.GatewayPrefix); prefix != "" {
handler = stripPrefixIfPresent(prefix, handler)
}
server := &http.Server{
Addr: cfg.ListenAddr,
Handler: handler,
@@ -235,7 +241,33 @@ func (a *App) bootstrapNginx(ctx context.Context, proxySvc *service.ProxyService
}
func (a *App) Run() error {
return a.server.ListenAndServe()
errCh := make(chan error, 2)
go func() {
errCh <- a.server.ListenAndServe()
}()
if socket := strings.TrimSpace(a.cfg.GatewaySocket); socket != "" {
if err := os.Remove(socket); err != nil && !os.IsNotExist(err) {
return err
}
listener, err := net.Listen("unix", socket)
if err != nil {
return err
}
if err := os.Chmod(socket, 0o660); err != nil {
_ = listener.Close()
return err
}
go func() {
errCh <- a.server.Serve(listener)
}()
}
err := <-errCh
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}
func (a *App) Shutdown(ctx context.Context) error {
@@ -246,6 +278,9 @@ func (a *App) Shutdown(ctx context.Context) error {
if err := a.server.Shutdown(ctx); err != nil {
return err
}
if socket := strings.TrimSpace(a.cfg.GatewaySocket); socket != "" {
_ = os.Remove(socket)
}
if err := a.nginx.Stop(ctx); err != nil {
a.logger.Error("nginx stop failed", "module", "NGINX", "error", err.Error())
}
@@ -260,6 +295,18 @@ func (a *App) Shutdown(ctx context.Context) error {
return nil
}
func stripPrefixIfPresent(prefix string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, prefix) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix)
if r.URL.Path == "" {
r.URL.Path = "/"
}
}
next.ServeHTTP(w, r)
})
}
func ResolveMigrationsDir() (string, error) {
candidates := []string{
"migrations",