GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
2
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
frozendragon
/
minecraft-ui
Browse code
Fix bug so the update works.
master
1 parent
297288b
commit
a05671510d238ea7e63da3177373da94c46c4dd8
Simon Lindgren
authored
on 11 Mar 2018
Patch
Showing
3 changed files
etc/static/index.html
internal/resource/minecraft-status/rest.go
minecraft-ui
Ignore Space
Show notes
View
etc/static/index.html
<!DOCTYPE html> <html> <body> <h1>Status</h1> <p id="minecraft_status" style="display:inline"></p> <button type="btnStart" onclick="sendStatusChange(true)">Start</button> <button type="btnStop" onclick="sendStatusChange(false)">Stop</button> <h1>Logs</h1> <pre id="minecraft_log"> </pre> </body> </html> <script> function sendStatusChange(status) { var http = new XMLHttpRequest(); var url = "/minecraft-status"; var data = JSON.stringify( { status: status} ) http.open("POST", url, true); //Send the proper header information along with the request http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); // send the collected data as JSON http.send(data); } function refreshStatus() { fetch("/minecraft-status") .then(res => res.json()) .then((out) => { document.getElementById('minecraft_status').innerText = "has started: " + out.status; }) .catch(err => { throw err }); setTimeout(refreshStatus, 5000); } function refreshLog() { fetch("/minecraft-log") .then(res => res.json()) .then((out) => { document.getElementById('minecraft_log').innerText = out.log; }) .catch(err => { throw err }); setTimeout(refreshLog, 5000); } setTimeout(refreshStatus, 500); setTimeout(refreshLog, 500); </script>
<!DOCTYPE html> <html> <body> <h1>Status</h1> <button type="btnStart" onclick="sendStatusChange(true)">Start</button> <button type="btnStop" onclick="sendStatusChange(false)">Stop</button> <h1>Logs</h1> <pre id="minecraft_log"> </pre> </body> </html> <script> function sendStatusChange(status) { var http = new XMLHttpRequest(); var url = "/minecraft-status"; var data = JSON.stringify( { status: status} ) http.open("POST", url, true); //Send the proper header information along with the request http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); // send the collected data as JSON http.send(data); } function refreshLog() { fetch("/minecraft-log") .then(res => res.json()) .then((out) => { document.getElementById('minecraft_log').innerText = out.log; }) .catch(err => { throw err }); setTimeout(refreshLog, 500); } setTimeout(refreshLog, 500); </script>
Ignore Space
Show notes
View
internal/resource/minecraft-status/rest.go
package minecraft_status import ( "go.uber.org/zap" "git.ssns.se/git/frozendragon/minecraft-ui/internal/rest" "net/http" "os/exec" "fmt" "strings" ) type Resource struct { Logger *zap.Logger } type Request struct { Status bool `json:"status"` } type Response struct { Status bool `json:"status"` } func (r *Resource) Get(w http.ResponseWriter, req *http.Request) { out, _ := exec.Command("systemctl", "is-active", "minecraft").Output() //if err != nil { // fmt.Println(err) // w.WriteHeader(http.StatusInternalServerError) // w.Write([]byte(err.Error())) // return //} running := false if strings.TrimSpace(string(out)) == "active" { running = true } response := &Response{ Status: running, } rest.JSON(w, req, response) } func (r *Resource) Post(w http.ResponseWriter, req *http.Request) { var request Request if err := rest.DecodeJSON(req, &request); err != nil { fmt.Println(err) w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } status := "stop" if request.Status { status = "start" } _, err := exec.Command("systemctl", status, "minecraft").Output() if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } }
package minecraft_status import ( "go.uber.org/zap" "git.ssns.se/git/frozendragon/minecraft-ui/internal/rest" "net/http" "os/exec" "fmt" ) type Resource struct { Logger *zap.Logger } type Request struct { Status bool `json:"status"` } type Response struct { Status bool `json:"status"` } func (r *Resource) Get(w http.ResponseWriter, req *http.Request) { out, _ := exec.Command("systemctl", "is-active", "minecraft").Output() //if err != nil { // fmt.Println(err) // w.WriteHeader(http.StatusInternalServerError) // w.Write([]byte(err.Error())) // return //} running := false if string(out) == "active" { running = true } response := &Response{ Status: running, } rest.JSON(w, req, response) } func (r *Resource) Post(w http.ResponseWriter, req *http.Request) { var request Request if err := rest.DecodeJSON(req, &request); err != nil { fmt.Println(err) w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } status := "stop" if request.Status { status = "start" } _, err := exec.Command("systemctl", status, "minecraft").Output() if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } }
Ignore Space
Show notes
View
minecraft-ui
Not supported
Show line notes below