forked from tcp.direct/tcp.ac
Fix: init bug + graceful shutdown
This commit is contained in:
parent
10d6048f4b
commit
9ad7ce5116
@ -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
13
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())
|
||||
}
|
||||
|
13
router.go
13
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user