-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
119 lines (106 loc) · 2.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"sync"
"github.com/gorilla/mux"
)
type lock struct {
sync.Mutex
lockedPaths map[string]bool
}
func (l *lock) lockPath(p string) bool {
l.Lock()
defer l.Unlock()
if _, ok := l.lockedPaths[p]; ok {
return false
}
l.lockedPaths[p] = true
return true
}
func (l *lock) unlockPath(p string) {
l.Lock()
defer l.Unlock()
delete(l.lockedPaths, p)
}
func (l *lock) lockPathAndRespond(p string, w http.ResponseWriter) {
if ok := l.lockPath(p); !ok {
w.WriteHeader(http.StatusLocked)
fmt.Fprintf(w, "resource already locked\n")
return
}
fmt.Printf("just locked: %v\n", p)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
}
func (l *lock) unlockPathAndRespond(p string, w http.ResponseWriter) {
l.unlockPath(p)
fmt.Printf("unlocked : %v\n", p)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
}
func main() {
port, ok := os.LookupEnv("PORT")
if !ok {
port = "80"
}
l := lock{
lockedPaths: make(map[string]bool),
}
r := mux.NewRouter()
r.HandleFunc("/", handler(&l)).Methods("POST")
r.HandleFunc("/unlock/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
p := r.FormValue("path")
l.unlockPathAndRespond(p, w)
}).Methods("GET")
r.HandleFunc("/lock/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
p := r.FormValue("path")
l.lockPathAndRespond(p, w)
}).Methods("GET")
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "text/html")
for k := range l.lockedPaths {
fmt.Fprintf(w, "%s\n", k)
fmt.Fprintf(w, "<a href='./unlock/?path=%s'>Unlock</a>\n", url.QueryEscape(k))
fmt.Fprintf(w, "<br>\n")
}
}).Methods("GET")
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatalf("could not start server: %v\n", err)
}
}
func handler(l *lock) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
event := struct {
Type string `json:"type"`
Payload struct {
EvidencePath string `json:"evidencePath"`
} `json:"payload"`
}{}
err := decoder.Decode(&event)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "error decoding request: %v\n", err)
return
}
switch event.Type {
case "LOCK":
p := event.Payload.EvidencePath
l.lockPathAndRespond(p, w)
case "UNLOCK":
p := event.Payload.EvidencePath
l.unlockPathAndRespond(p, w)
default:
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "event type not known: %v\n", event.Type)
}
}
}