forked from tcp.direct/tcp.ac
1
0
Fork 0

Fix: init bug + graceful shutdown

This commit is contained in:
kayos@tcp.direct 2022-07-10 01:09:29 -07:00
parent 10d6048f4b
commit 9ad7ce5116
Signed by untrusted user: kayos
GPG Key ID: 4B841471B4BEE979
3 changed files with 32 additions and 7 deletions

View File

@ -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)

13
main.go
View File

@ -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())
}

View File

@ -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
}