/
web.go
145 lines (128 loc) · 3.25 KB
/
web.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
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
package main
import (
"fmt"
"net/http"
"os"
"time"
"io/ioutil"
"encoding/json"
"html/template"
)
func main() {
http.HandleFunc("/", statusResponse)
ip := os.Getenv("OPENSHIFT_GO_IP")
port := os.Getenv("OPENSHIFT_GO_PORT")
if ip == "" || port == "" {
panic("Please set OPENSHIFT_GO_IP and OPENSHIFT_GO_PORT.")
}
if os.Getenv("OPENSHIFT_DATA_DIR") == "" {
panic("Please use OPENSHIFT_DATA_DIR.")
}
bind := fmt.Sprintf("%s:%s", ip, port)
fmt.Printf("listening on %s...", bind)
err := http.ListenAndServe(bind, nil)
if err != nil {
panic(err)
}
}
type Lockfile struct {
IsLocked bool `json:"is_locked"`
HeldBy string `json:"locker"`
ModificationTime string `json:"modification_time"`
}
func getLockfilePath() string {
return os.Getenv("OPENSHIFT_DATA_DIR") + "/lock";
}
func getLockfile() Lockfile {
path := getLockfilePath()
if _, err := os.Stat(path); os.IsNotExist(err) {
return Lockfile{
IsLocked: false,
HeldBy: "",
ModificationTime: "",
};
}
chars, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
var lockfile Lockfile
if err := json.Unmarshal(chars, &lockfile); err != nil {
panic(err)
}
return lockfile
}
func writeLockfile(lockfile Lockfile) {
path := getLockfilePath()
bytes, err := json.Marshal(&lockfile)
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(path, bytes, 0600); err != nil {
panic(err)
}
}
func currentTimeString() string {
pragueTime, err := time.LoadLocation("Europe/Prague")
if err != nil {
panic(err)
}
return time.Now().In(pragueTime).String()
}
func isValidLocker(locker string) bool {
return len(locker) >= 1 && len(locker) <= 50;
}
func statusResponse(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, "<!doctype html>\n")
fmt.Fprintf(res, "<html>\n")
fmt.Fprintf(res, "<body>\n")
lockfile := getLockfile()
switch req.Method {
case "POST":
switch req.PostFormValue("operation") {
case "lock":
if lockfile.IsLocked {
fmt.Fprintf(res, "cannot lock, sorry<br>\n")
} else {
locker := req.PostFormValue("locker")
if isValidLocker(locker) {
lockfile.IsLocked = true
lockfile.HeldBy = req.PostFormValue("locker")
lockfile.ModificationTime = currentTimeString()
writeLockfile(lockfile)
} else {
fmt.Fprintf(res, "you have terrible taste in names<br>\n")
}
}
case "release":
lockfile.IsLocked = false
lockfile.ModificationTime = currentTimeString()
writeLockfile(lockfile)
}
}
template, err := template.New("locked_greeting").Parse(
`{{define "LockStatusPage"}}
{{if .IsLocked}}
<b>{{.HeldBy}}</b> has the lock since <b>{{.ModificationTime}}</b><br>
<form method='POST'>
<input type='hidden' name='operation' value='release'>
<input type='submit' value='release lock'>
</form>
{{else}}
unlocked right now, since <b>{{.ModificationTime}}</b><br>
<form method='POST'>
<input type='hidden' name='operation' value='lock'>
<input type='text' name='locker' placeholder='My Glorious Name'>
<input type='submit' value='grab lock'>
</form>
{{end}}
{{end}}`)
if err != nil {
panic(err)
}
if err := template.ExecuteTemplate(res, "LockStatusPage", lockfile); err != nil {
panic(err)
}
fmt.Fprintf(res, "</body>\n")
fmt.Fprintf(res, "</html>\n")
}