fix(stop): route all stops through StopController, SIGTERM graceful, second Ctrl+C forces exit

This commit is contained in:
aykhans committed 2026-10-20 13:30:43 +04:00
1 parent a41bdc4824
commit f84644c688
5 files changed
+176 -90

No files matched your search

+13 -14
View File
@@ -4,8 +4,6 @@ import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"charm.land/lipgloss/v2"
"go.aykhans.me/sarin/internal/config"
@@ -17,19 +15,22 @@ import (
func main() {
ctx, cancel := context.WithCancel(context.Background())
stopCtrl := sarin.NewStopController(cancel)
go listenForTermination(stopCtrl.Stop)
go stopCtrl.ListenSignals()
combinedConfig := config.ReadAllConfigs()
combinedConfig := config.ReadAllConfigs(ctx)
if ctx.Err() != nil {
return
}
combinedConfig.SetDefaults()
if *combinedConfig.ShowConfig {
if !combinedConfig.Print() {
if !combinedConfig.Print(ctx, stopCtrl) {
return
}
}
_ = utilsErr.MustHandle(combinedConfig.Validate(),
_ = utilsErr.MustHandle(combinedConfig.Validate(ctx),
utilsErr.OnType(func(err types.FieldValidationErrors) error {
for _, fieldErr := range err.Errors {
if fieldErr.Value == "" {
@@ -46,6 +47,9 @@ func main() {
return nil
}),
)
if ctx.Err() != nil {
return
}
srn, err := sarin.NewSarin(
ctx,
@@ -58,6 +62,9 @@ func main() {
*combinedConfig.DryRun, *combinedConfig.LogLevel, *combinedConfig.LogFile,
combinedConfig.Lua, combinedConfig.Js,
)
if ctx.Err() != nil {
return
}
_ = utilsErr.MustHandle(err,
utilsErr.OnType(func(err types.ProxyDialError) error {
fmt.Fprint(os.Stderr, lipgloss.Sprintln(config.StyleRed.Render("[PROXY] ")+err.Error()))
@@ -89,11 +96,3 @@ func main() {
srn.GetResponses().PrintTable()
}
}
func listenForTermination(stop func()) {
sigChan := make(chan os.Signal, 4)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
for range sigChan {
stop()
}
}