diff --git a/config/misc.go b/config/misc.go index 8c4ca0e..2cccd75 100644 --- a/config/misc.go +++ b/config/misc.go @@ -14,10 +14,19 @@ func PrintBanner() { println("tcp.ac\n") return } + gitr := "" + brn := "" + if gitrev, ok := binInfo["vcs.revision"]; ok { + gitr = gitrev[:7] + } + if vt, ok := binInfo["vcs.time"]; ok { + brn = vt + } + p := termenv.ColorProfile() bnr, _ := squish.UnpackStr(Banner) - gr := termenv.String(binInfo["vcs.revision"][:7]).Foreground(termenv.ANSIBrightGreen).String() - born := termenv.String(binInfo["vcs.time"]).Foreground(p.Color("#1e9575")).String() + gr := termenv.String(gitr).Foreground(termenv.ANSIBrightGreen).String() + born := termenv.String(brn).Foreground(p.Color("#1e9575")).String() out := strings.Replace(bnr, "$gitrev$", gr, 1) out = strings.Replace(out, "$date$", born, 1) cout := termenv.String(out) diff --git a/main.go b/main.go index 9d6639a..dd3aec4 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,12 @@ package main import ( + "context" + "net/http" "os" "os/signal" "syscall" + "time" "github.com/rs/zerolog/log" @@ -30,13 +33,19 @@ func makeDirectories() { } } -func wait() { +func wait(hs *http.Server) { c := make(chan os.Signal, 5) signal.Notify(c, os.Interrupt, syscall.SIGTERM) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) for { select { case <-c: log.Warn().Msg("Interrupt detected, shutting down gracefully...") + if err := hs.Shutdown(ctx); err != nil { + cancel() + } + log.Print("fin.") + cancel() return } } @@ -59,5 +68,5 @@ func main() { } }() go serveTermbin() - wait() + wait(httpRouter()) } diff --git a/router.go b/router.go index 6af52cb..9fbbef6 100644 --- a/router.go +++ b/router.go @@ -3,6 +3,7 @@ package main import ( "encoding/base64" "io" + "net/http" "time" "github.com/gin-contrib/gzip" @@ -31,7 +32,7 @@ func urlPost(c *gin.Context) { return } -func httpRouter() *gin.Engine { +func httpRouter() *http.Server { if !config.Trace { log.Debug().Caller().Msg("running gin in release mode, enable trace to run gin in debug mode") gin.SetMode(gin.ReleaseMode) @@ -85,7 +86,13 @@ func httpRouter() *gin.Engine { log.Info().Str("Host", config.HTTPBind). Str("Port", config.HTTPPort). Msg("done; tcp.ac is live.") - go router.Run(config.HTTPBind + ":" + config.HTTPPort) - return router + srv := &http.Server{ + Addr: config.HTTPBind + ":" + config.HTTPPort, + Handler: router, + } + + go srv.ListenAndServe() + + return srv }