-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (125 loc) · 3.2 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
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
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strconv"
"time"
)
var (
jobs = map[string]*Job{}
)
// Job struct
type Job struct {
ID string `json:"id"`
Type string `json:"type"`
Request interface{} `json:"request,omitempty"`
Response interface{} `json:"response,omitempty"`
}
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
http.Handle("/", http.FileServer(http.Dir("public")))
http.HandleFunc("/discover", withLog(handleDiscover))
http.HandleFunc("/requests", withLog(handleRequests))
http.HandleFunc("/requests/", withLog(handleRequest))
http.HandleFunc("/jobs", withLog(handleJobs))
http.HandleFunc("/jobs/", withLog(handleJob))
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
log.Printf("Listening on port %s...", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func withLog(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := httptest.NewRecorder()
fn(c, r)
log.Printf("[%d] %-4s %s\n", c.Code, r.Method, r.URL.Path)
for k, v := range c.HeaderMap {
w.Header()[k] = v
}
w.WriteHeader(c.Code)
c.Body.WriteTo(w)
}
}
func handleDiscover(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(map[string]interface{}{"type": "discover"})
r.Body = ioutil.NopCloser(bytes.NewReader(body))
handleRequests(w, r)
}
func handleRequests(w http.ResponseWriter, r *http.Request) {
var request struct {
Type string
Payload struct {
ID string `json:"id"`
}
}
json.NewDecoder(r.Body).Decode(&request)
defer r.Body.Close()
var jobID string
for {
hasher := md5.New()
hasher.Write([]byte(strconv.FormatInt(time.Now().Unix()*rand.Int63(), 10)))
jobID = hex.EncodeToString(hasher.Sum(nil))
if _, ok := jobs[jobID]; !ok {
break
}
}
jobs[jobID] = &Job{
ID: jobID,
Type: request.Type,
Request: request.Payload,
}
var response struct {
ID string `json:"id"`
}
response.ID = jobID
w.Header().Add("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(response)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
id := regexp.MustCompile("/requests/([^/]+)").FindStringSubmatch(r.URL.Path)[1]
job := jobs[id]
var response struct {
Type string `json:"type"`
Payload interface{} `json:"payload"`
}
response.Type = job.Type
response.Payload = job.Response
w.Header().Add("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(response)
}
func handleJobs(w http.ResponseWriter, r *http.Request) {
var response struct {
Jobs []*Job `json:"jobs"`
}
for _, job := range jobs {
if job.Response != nil {
continue
}
response.Jobs = append(response.Jobs, job)
}
w.Header().Add("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(response)
}
func handleJob(w http.ResponseWriter, r *http.Request) {
var request struct {
Payload interface{}
}
json.NewDecoder(r.Body).Decode(&request)
defer r.Body.Close()
id := regexp.MustCompile("/jobs/([^/]+)").FindStringSubmatch(r.URL.Path)[1]
job := jobs[id]
job.Response = request.Payload
}