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