shutdown hook

This commit is contained in:
Michael Quigley 2022-07-27 11:31:01 -04:00
parent cfa603ea0c
commit ad93c613b5
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -2,17 +2,25 @@ package main
import ( import (
"github.com/openziti-test-kitchen/zrok/http" "github.com/openziti-test-kitchen/zrok/http"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/tunnel" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/tunnel"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os"
"os/signal"
"syscall"
) )
var httpCmd = &cobra.Command{ var httpCmd = &cobra.Command{
Use: "http <endpoint>", Use: "http <endpoint>",
Short: "Start an http terminator", Short: "Start an http terminator",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) { Run: handleHttp,
}
func handleHttp(_ *cobra.Command, args []string) {
idCfg, err := zrokdir.IdentityConfigFile() idCfg, err := zrokdir.IdentityConfigFile()
if err != nil { if err != nil {
panic(err) panic(err)
@ -38,8 +46,19 @@ var httpCmd = &cobra.Command{
} }
cfg.Service = resp.Payload.Service cfg.Service = resp.Payload.Service
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cleanupHttp(cfg, zrok)
os.Exit(1)
}()
if err := http.Run(cfg); err != nil { if err := http.Run(cfg); err != nil {
panic(err) panic(err)
} }
}, }
func cleanupHttp(cfg *http.Config, zrok *rest_client_zrok.Zrok) {
logrus.Infof("shutting down '%v'", cfg.Service)
} }