-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.go
111 lines (96 loc) · 2.65 KB
/
Renderer.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
package hypernova_go
import (
"net/http"
"bytes"
"fmt"
"encoding/json"
"log"
//"io/ioutil"
)
func fallbackHtml(viewName string, data ReactProps) string {
var buffer bytes.Buffer
jsonString, _ := json.Marshal(data)
buffer.WriteString(`<div data-hypernova-key="`)
buffer.WriteString(viewName)
buffer.WriteString(`"></div><script type="application/json" data-hypernova-key="`)
buffer.WriteString(viewName)
buffer.WriteString(`"><!--`)
buffer.Write(jsonString)
buffer.WriteString(`}--></script>`)
return buffer.String()
}
func toHtml(resp HypernovaResponse) string {
var buffer bytes.Buffer
for _, jobResponse := range resp.Results {
buffer.WriteString(jobResponse.Html)
}
return buffer.String()
}
type Config struct{}
type Renderer struct {
Url string
Plugins []Plugin
Config Config
}
func (r Renderer) Render(jobs Jobs) (html string, err error) {
jobMap := make(map[string]Job)
for name, job := range jobs {
jobMap[name] = job
for _, plugin := range r.Plugins {
jobMap[name] = Job{
Name: name,
Data: plugin.getViewData(name, jobMap[name].Data),
}
}
}
for _, plugin := range r.Plugins {
jobMap = plugin.prepareRequest(jobMap)
}
shouldSendRequest := true
for _, plugin := range r.Plugins {
shouldSendRequest = shouldSendRequest && plugin.shouldSendRequest(jobMap)
}
if !shouldSendRequest {
results := make(map[string]JobResponse)
for name, jobResp := range jobs {
results[name] = JobResponse{
Html: fallbackHtml(name, jobResp.Data),
}
}
return toHtml(HypernovaResponse{Results: results, Success: true}), nil
}
for _, plugin := range r.Plugins {
plugin.willSendRequest(jobMap)
}
jobMapJsonString, err := json.Marshal(jobMap)
fmt.Println("request: ", string(jobMapJsonString))
resp, err := http.Post(r.Url, "application/json", bytes.NewBuffer(jobMapJsonString))
defer resp.Body.Close()
originalJobResponse := new(HypernovaResponse)
err = json.NewDecoder(resp.Body).Decode(originalJobResponse)
if err != nil {
for _, plugin := range r.Plugins {
plugin.onError(err, jobs)
}
log.Fatal(err)
// TODO fallback to client render
return "", err
}
successfulJobs := make(map[string]Job)
failedJobs := make(map[string]Job)
for name, jobResp := range (*originalJobResponse).Results {
if jobResp.Success {
successfulJobs[name] = jobs[name]
} else {
failedJobs[name] = jobs[name]
}
}
for _, plugin := range r.Plugins {
plugin.onSuccess(*originalJobResponse, successfulJobs)
}
currentJobResponse := *originalJobResponse
for _, plugin := range r.Plugins {
currentJobResponse = plugin.afterResponse(currentJobResponse, *originalJobResponse)
}
return toHtml(currentJobResponse), nil
}