Skip to content

Commit

Permalink
Graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Dec 14, 2017
1 parent 0425089 commit 0ead6c1
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package cmd

import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"

"github.com/gin-gonic/gin"
"github.com/securityfirst/tent"
Expand All @@ -36,14 +40,31 @@ var runCmd = &cobra.Command{
os.Exit(1)
}
e := gin.Default()
srv := &http.Server{
Addr: fmt.Sprintf(":%v", config.Port),
Handler: e,
}
r, err := newRepo()
if err != nil {
log.Fatalf("Repo error: %s", err)
}

o := tent.New(r)
o.Register(e.Group("/v2"), config.Config)
e.Run(fmt.Sprintf(":%v", config.Port))

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
go func() {
if err := e.Run(fmt.Sprintf(":%v", config.Port)); err != nil {
log.Fatal(err)
}
}()

<-stop
log.Println("Shutting down the server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
srv.Shutdown(ctx)
},
}

Expand Down

0 comments on commit 0ead6c1

Please sign in to comment.