/
output.go
91 lines (78 loc) · 2.16 KB
/
output.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
package main
import(
"fmt"
"http"
"json"
"os"
)
type MessageType int
const (
_ = iota
MessageTypeStatus MessageType = 1
MessageTypeError MessageType = 2
)
type OutputWriter interface {
WriteError(step string, e os.Error)
WriteStatus(msg StatusMessage)
}
type PlainOutputWriter struct {
requestId int
w *http.ResponseWriter
}
func (p *PlainOutputWriter) WriteStatus(msg StatusMessage) {
(*(p.w)).Write([]byte(fmt.Sprintf("STATUS (%d): %d (%d/%d)\n", msg.requestId, msg.state, msg.bytesTransferred, msg.totalBytes)))
(*(p.w)).(http.Flusher).Flush()
}
func (p *PlainOutputWriter) WriteError(step string, e os.Error) {
(*(p.w)).WriteHeader(500)
(*(p.w)).Write([]byte("While " + step + ": "))
if e != nil {
(*(p.w)).Write([]byte(e.String()))
} else {
(*(p.w)).Write([]byte("No error string given!"))
}
(*(p.w)).(http.Flusher).Flush()
}
type JsonPOutputWriter struct {
PlainOutputWriter
// jsonPCallback string
}
func (j *JsonPOutputWriter) WriteJson(m map[string] string) {
(*(j.w)).Header().Add("Content-Type", "application/json")
m["requestId"] = fmt.Sprintf("%d", j.requestId)
encoded, e := json.MarshalForHTML(m)
if e != nil {
(*(j.w)).Write([]byte("An error occurred while marshalling the following JSON:"))
(*(j.w)).Write([]byte(e.String()))
return
}
// (*(j.w)).Write([]byte("<script type=\"text/javascript\">" + j.jsonPCallback + "("))
(*(j.w)).Write(encoded)
// (*(j.w)).Write([]byte(");</script>\n"))
}
func (j *JsonPOutputWriter) WriteError(step string, e os.Error) {
m := make(map[string] string)
m["type"] = fmt.Sprintf("%d", MessageTypeError)
m["step"] = step
if e != nil {
m["error"] = e.String()
} else {
m["error"] = ""
}
j.WriteJson(m)
}
func (j *JsonPOutputWriter) WriteStatus(msg StatusMessage) {
m := make(map[string] string)
m["state"] = fmt.Sprintf("%d", msg.state)
m["link"] = msg.link
m["title"] = msg.title
m["secondsRemaining"] = fmt.Sprintf("%d", msg.secondsRemaining)
m["bytesTransferred"] = fmt.Sprintf("%d", msg.bytesTransferred)
m["totalBytes"] = fmt.Sprintf("%d", msg.totalBytes)
if msg.error != nil {
m["error"] = msg.error.String()
m["step"] = msg.step
}
j.WriteJson(m)
(*(j.w)).(http.Flusher).Flush()
}