/
shutdown.go
51 lines (45 loc) · 1.01 KB
/
shutdown.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package manager
import (
"github.com/sirupsen/logrus"
"github.com/teris-io/shortid"
"log"
"net/http"
"time"
)
var restartID string
func Shut() func(w http.ResponseWriter, r *http.Request) {
Init()
return func(w http.ResponseWriter, r *http.Request) {
restartID, _ = shortid.Generate()
t.ExecuteTemplate(w, "shutdown", map[string]string{
"id": restartID,
})
}
}
func Cancel() func(w http.ResponseWriter, r *http.Request) {
Init()
return func(w http.ResponseWriter, r *http.Request) {
log.Println("cancel shutdown process")
restartID = ""
w.Write([]byte("ok"))
}
}
func close(s *http.Server) {
time.Sleep(10 * time.Second)
if restartID != "" {
restartID = ""
err := s.Close()
if err!= nil{
logrus.Error("Shutdown:%v", err)
}
}
}
func Down(server *http.Server) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("start shutdown process")
if restartID == "" {
http.Redirect(w, r, "/", 302)
}
go close(server)
}
}