fix: serve Ctrl-C stop

This commit is contained in:
Kaden
2026-06-16 23:26:52 +08:00
parent 602f4dfa40
commit c85745b230
2 changed files with 14 additions and 2 deletions
+6 -1
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
)
@@ -61,7 +62,11 @@ func runServe(ctx context.Context, dir, addr string) error {
return err
}
case <-ctx.Done():
_ = srv.Close()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
_ = srv.Close()
}
if err := <-srvErr; err != nil && err != http.ErrServerClosed {
return err
}
+8 -1
View File
@@ -7,6 +7,7 @@ import (
"context"
"os"
"os/signal"
"syscall"
"github.com/tamnd/kage/cli"
"github.com/tamnd/kage/viewer"
@@ -18,7 +19,13 @@ func main() {
// thread; in the default build this is a harmless no-op.
viewer.LockMainThread()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
<-ctx.Done()
stop()
}()
os.Exit(cli.Execute(ctx))
}