Taobin-Recipe-Manager/server/main.go

45 lines
736 B
Go
Raw Normal View History

2023-09-14 14:52:04 +07:00
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
2023-09-20 10:39:11 +07:00
s := NewServer()
2023-09-14 14:52:04 +07:00
serverCtx, serverStopCtx := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-sig
2023-09-20 10:42:14 +07:00
shutdownCtx, cancel := context.WithTimeout(serverCtx, 30*time.Second)
2023-09-14 14:52:04 +07:00
go func() {
<-shutdownCtx.Done()
if shutdownCtx.Err() == context.DeadlineExceeded {
log.Println("Shutdown timeout, force exit")
2023-09-20 10:42:14 +07:00
cancel()
2023-09-14 14:52:04 +07:00
}
}()
err := s.Shutdown(shutdownCtx)
if err != nil {
log.Fatal(err)
}
serverStopCtx()
}()
err := s.Run()
if err != nil {
log.Fatal(err)
}
<-serverCtx.Done()
}