Use context in Server

This commit is contained in:
SALLEYRON Julien 2018-03-14 13:14:03 +01:00 committed by Traefiker Bot
parent 526a04d4c8
commit f99363674b
5 changed files with 47 additions and 36 deletions

22
cmd/context.go Normal file
View file

@ -0,0 +1,22 @@
package cmd
import (
"context"
"os"
"os/signal"
"syscall"
)
// ContextWithSignal create a context cancelled when SIGINT or SIGTERM are notified
func ContextWithSignal(ctx context.Context) context.Context {
newCtx, cancel := context.WithCancel(ctx)
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-signals:
cancel()
}
}()
return newCtx
}