-
Notifications
You must be signed in to change notification settings - Fork 6
/
serve-to-machines.go
51 lines (43 loc) · 1.07 KB
/
serve-to-machines.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
package now
import (
"fmt"
"json"
"github.com/hoisie/web.go"
)
func jsonNext(ctx *web.Context) string {
if thing := GetNext(); thing != "" {
out, _ := json.Marshal(thing)
ctx.SetHeader("Content-Type", "application/json", true)
return fmt.Sprintf("%s", out)
}
ctx.StartResponse(418) // I'm a teapot
return ""
}
func jsonLater(ctx *web.Context) {
if in, ok := ctx.Request.Params["thing"]; ok {
for _, thing := range in {
AddThing(thing)
}
ctx.StartResponse(204) // Success, no comment
} else {
ctx.StartResponse(500) // Vague error message
}
}
func jsonPop(ctx *web.Context) string {
if _, ok := ctx.Request.Params["done"]; ok {
Pop()
return jsonNext(ctx)
}
ctx.StartResponse(400)
return ""
}
func jsonPushNext(ctx *web.Context) string {
if thing, ok := ctx.Request.Params["thing"]; ok {
if len(thing) > 1 {
ctx.StartResponse(400)
return ""
}
PushNext(thing[0])
}
return jsonNext(ctx)
}