-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (90 loc) · 2.28 KB
/
main.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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"test/models"
"time"
)
type CommandRun struct {
name string
time time.Time
err error
}
type MachineStatus struct {
models.InstantSuccessMachine
command_history []CommandRun
}
// this runs a server that simulates a simple machine
// use GET /state to get the current state
// use POST /commands with a simple texyt body of the command name like "Stop" lets you issue a command
// use GET /commands to get a list of commands issued in the past
func main() {
env := MachineStatus{}
http.HandleFunc("/state", env.MachineState)
http.HandleFunc("/commands", env.MachineCommands)
http.ListenAndServe(":8090", nil)
}
func (m *MachineStatus) MachineState(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(405), 405)
return
}
fmt.Fprintf(w, "%s", m.GetState())
}
func (m *MachineStatus) MachineCommands(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading body: %v", err)
http.Error(w, "can't read body", http.StatusBadRequest)
return
}
switch string(body[:]) {
case "Abort":
err = models.Abort(m)
case "Clear":
err = models.Clear(m)
case "Reset":
err = models.Reset(m)
case "Stop":
err = models.Stop(m)
case "Start":
err = models.Start(m)
case "Hold":
err = models.Hold(m)
case "Unhold":
err = models.Unhold(m)
case "Suspend":
err = models.Suspend(m)
case "Unsuspend":
err = models.Unsuspend(m)
default:
log.Printf("Invalid command name: \"%s\"\n", string(body[:]))
http.Error(w,
fmt.Sprintf("Invalid command name: \"%s\"\n", string(body[:])),
http.StatusBadRequest)
}
m.command_history = append(m.command_history, CommandRun{name: string(body[:]), time: time.Now(), err: err})
if err != nil {
log.Printf("Command error: %v", err)
http.Error(w,
fmt.Sprintf("Command error: %s\n", err),
http.StatusBadRequest)
return
}
} else if r.Method == "GET" {
for _, v := range m.command_history {
if v.err == nil {
fmt.Fprintf(w, "%s %s\n", v.time, v.name)
} else {
fmt.Fprintf(w, "%s %s: %s\n", v.time, v.name, v.err)
}
}
return
} else {
http.Error(w, http.StatusText(405), 405)
return
}
}