-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.go
More file actions
157 lines (133 loc) · 3.5 KB
/
Copy pathweb.go
File metadata and controls
157 lines (133 loc) · 3.5 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"net/http"
"fmt"
"html/template"
"strconv"
"strings"
"log"
"time"
)
const (
homeTemplate = `
<html>
<head>
<meta charset="UTF-8">
<title>Minecraft shortcut</title>
<style>
div {
padding: 10px;
}
button {
height:150px;
width: 60%;
font-size:40px;
}
input {
height:100px;
width: 60%;
font-size:40px;
}
p {
font-size:40px;
}
</style>
</head>
<body>
<div><button type="button" onclick="location.href='/d'">Delete Link</button></div>
<div><button type="button" onclick="location.href='/a'">Add Link</button></div>
<div><button type="button" onclick="location.href='/stop'">Stop</button></div>
<div><button type="button" onclick="schedule()">Schedule</button></div>
<div><input type="text" id="duration"></div>
{{range .}}<p>{{.}}</p>{{end}}</div>
<script>
function schedule() {
var d = document.getElementById("duration").value;
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "/s?d=" + d, true);
xhttp.send();
}
</script>
</body>
</html>`
)
var (
alert = "Minecraft se va inchide in 1 minut. Salveaza !!!"
)
func renderTemplate(w http.ResponseWriter, message ...string) {
t, err := template.New("home").Parse(homeTemplate)
if err != nil {
fmt.Fprintln(w, "Error")
return
}
t.Execute(w, message)
}
func messageMiddleware(messages []string, next func(w http.ResponseWriter, req *http.Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if len(messages) > 0 {
renderTemplate(w, messages...)
return
}
next(w, req)
})
}
func addLinkHandler(w http.ResponseWriter, req *http.Request) {
err := addLink()
if err != nil {
renderTemplate(w, err.Error())
return
}
renderTemplate(w, fmt.Sprintf("Link successful added to desktop."))
}
func removeLinkHandler(w http.ResponseWriter, req *http.Request) {
err := removeLink()
if err != nil {
renderTemplate(w, err.Error())
return
}
renderTemplate(w, fmt.Sprintf("Link successful removed from desktop."))
}
func stopProcessHandler(w http.ResponseWriter, req *http.Request) {
pids, err := minecraftPIDs()
if err != nil {
renderTemplate(w, err.Error())
return
}
messages := stopProcesses(pids)
renderTemplate(w, messages...)
}
func scheduleHandler(w http.ResponseWriter, req *http.Request) {
// check and parse request parameter
dStr := req.URL.Query().Get("d")
d, err := strconv.Atoi(strings.TrimSpace(dStr))
if err != nil {
log.Printf("Cannot convert scheduled duration value '%s'", dStr)
return
}
// set Scheduler and the action taken after that duration
dm := time.Duration(d) * time.Minute
Scheduler.Reset(d, func() {
pids, err := minecraftPIDs()
if err != nil {
log.Println(err.Error())
return
}
stopProcesses(pids)
removeLink()
log.Printf("Scheduled Stop executed for pids: '%v'", pids)
}, alert)
log.Printf("Stop scheduled after '%v' for time: '%s'", dm, time.Now().Add(dm).Format("15:04"))
}
func homeHandler(w http.ResponseWriter, req *http.Request) {
msg := ""
if Scheduler.When() != nil {
msg = fmt.Sprintf("Stop scheduled at: '%s'", Scheduler.When().Format("15:04"))
}
renderTemplate(w, msg)
}