mirror of
https://github.com/tenox7/wrp.git
synced 2025-02-19 07:31:26 +00:00
fixed handling interrupts and shutdown
This commit is contained in:
parent
7067d2cdf8
commit
bb84d43d31
22
wrp.go
22
wrp.go
@ -18,8 +18,11 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chromedp/cdproto/emulation"
|
"github.com/chromedp/cdproto/emulation"
|
||||||
@ -246,7 +249,11 @@ func (w wrpReq) capture(c string, out http.ResponseWriter) {
|
|||||||
|
|
||||||
func haltServer(out http.ResponseWriter, req *http.Request) {
|
func haltServer(out http.ResponseWriter, req *http.Request) {
|
||||||
log.Printf("%s Shutdown Request for %s\n", req.RemoteAddr, req.URL.Path)
|
log.Printf("%s Shutdown Request for %s\n", req.RemoteAddr, req.URL.Path)
|
||||||
defer cancel()
|
out.Header().Set("Content-Type", "text/text")
|
||||||
|
fmt.Fprintf(out, "Shutting down WRP...\n")
|
||||||
|
out.(http.Flusher).Flush()
|
||||||
|
time.Sleep(time.Second * 2)
|
||||||
|
cancel()
|
||||||
srv.Shutdown(context.Background())
|
srv.Shutdown(context.Background())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,8 +274,8 @@ func main() {
|
|||||||
chromedp.Flag("headless", headless),
|
chromedp.Flag("headless", headless),
|
||||||
chromedp.Flag("hide-scrollbars", false),
|
chromedp.Flag("hide-scrollbars", false),
|
||||||
)
|
)
|
||||||
actx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
actx, acancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||||
defer cancel()
|
defer acancel()
|
||||||
if debug {
|
if debug {
|
||||||
ctx, cancel = chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
|
ctx, cancel = chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
|
||||||
} else {
|
} else {
|
||||||
@ -276,6 +283,15 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
c := make(chan os.Signal)
|
||||||
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-c
|
||||||
|
log.Printf("Interrupt - shutting down.")
|
||||||
|
cancel()
|
||||||
|
srv.Shutdown(context.Background())
|
||||||
|
os.Exit(1)
|
||||||
|
}()
|
||||||
http.HandleFunc("/", pageServer)
|
http.HandleFunc("/", pageServer)
|
||||||
http.HandleFunc("/map/", mapServer)
|
http.HandleFunc("/map/", mapServer)
|
||||||
http.HandleFunc("/img/", imgServer)
|
http.HandleFunc("/img/", imgServer)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user