Skip to content

Commit

Permalink
Backend: handle interrupt signals.
Browse files Browse the repository at this point in the history
  • Loading branch information
paveloom committed Sep 18, 2024
1 parent 60e6cf4 commit 3cfa26c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions backend/flowey.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
)

func run() error {
Expand All @@ -19,9 +22,27 @@ func run() error {
return err
}
log.Printf("listening at %v", address)
return server.Serve(listener)

sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)

errs := make(chan error, 1)
go func() {
errs <- server.Serve(listener)
}()

select {
case <-sigint:
fmt.Print("\rinterrupting...\n")
case err := <-errs:
log.Printf("failed to serve: %w", err)
}

return server.Shutdown(context.Background())
}

func main() {
log.Fatal(run())
if err := run(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 3cfa26c

Please sign in to comment.