-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
201 lines (163 loc) · 3.87 KB
/
render.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package htmlc
import (
"bytes"
"errors"
"io"
"os/exec"
"sync"
"time"
regex "github.com/tkdeng/goregex"
"github.com/tkdeng/goutil"
)
type ExsEngine struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
out <-chan []byte
running *bool
mu sync.Mutex
}
type Map map[string]any
var ErrorStopped error = errors.New("elixir cmd stopped running")
var ErrorTimeout error = errors.New("request timed out")
func Engine(file string) (*ExsEngine, error) {
cmd := exec.Command(`iex`, file)
stdin, err := cmd.StdinPipe()
if err != nil {
return &ExsEngine{}, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
stdin.Close()
return &ExsEngine{}, err
}
out := make(chan []byte)
running := true
ready := false
go func() {
for {
if !running {
break
}
buf := make([]byte, 1024*1024)
n, err := stdout.Read(buf)
if err == io.EOF {
break
}
if err != nil {
continue
}
if !ready {
continue
}
buf = buf[:n]
if regex.CompRE2(`(?im)^\s*iex\s*\(.*?\)\s*>`).Match(buf) || regex.CompRE2(`(?im)^\s*nil`).Match(buf) {
continue
}
hasOut := false
regex.CompRE2(`~c"((?:\\"|.)*?)"`).RepFunc(buf, func(data func(int) []byte) []byte {
if hasOut {
return nil
}
out <- bytes.ReplaceAll(data(1), []byte("\\n"), []byte("\n"))
hasOut = true
return nil
})
if !hasOut {
out <- []byte("<h1>Error 500</h1><h2>Elixir Template Error!</h2>")
}
}
running = false
stdin.Close()
stdout.Close()
}()
err = cmd.Start()
if err != nil {
running = false
stdin.Close()
stdout.Close()
return &ExsEngine{}, err
}
time.Sleep(1 * time.Second)
ready = true
return &ExsEngine{
cmd: cmd,
stdin: stdin,
stdout: stdout,
out: out,
running: &running,
}, nil
}
func (exs *ExsEngine) Render(name string, args Map, layout ...string) ([]byte, error) {
if !*exs.running {
return []byte{}, ErrorStopped
}
lay := []byte("layout")
if len(layout) != 0 {
lay = EscapeExsArgs([]byte(layout[0]), '"')
}
in := regex.JoinBytes(
`App.render "`, EscapeExsArgs([]byte(name), '"'), '"',
`, "`, lay, `", `, renderExsMap(args), '\n',
)
in = append(in, []byte("\n")...)
var out []byte
go func() {
exs.mu.Lock()
defer exs.mu.Unlock()
exs.stdin.Write(in)
out = <-exs.out
time.Sleep(10 * time.Nanosecond)
}()
start := time.Now().UnixMilli()
for len(out) == 0 && time.Now().UnixMilli()-start < 10*1000 {
time.Sleep(10 * time.Nanosecond)
}
if len(out) == 0 {
return []byte{}, ErrorTimeout
}
return out, nil
}
func renderExsMap(args Map) []byte {
buf := []byte("%{")
for key, val := range args {
buf = append(buf, []byte(key+": ")...)
if v, ok := val.(string); ok {
buf = regex.JoinBytes(buf, '"', EscapeExsArgs([]byte(v), '"'), '"', ',')
} else if v, ok := val.([]byte); ok {
buf = regex.JoinBytes(buf, '"', EscapeExsArgs(v, '"'), '"', ',')
} else if v, ok := val.(Map); ok {
buf = regex.JoinBytes(buf, renderExsMap(v), ',')
} else if v, ok := val.([]any); ok {
buf = regex.JoinBytes(buf, renderExsArray(v), ',')
} else {
buf = regex.JoinBytes(buf, goutil.ToType[[]byte](val), ',')
}
}
if buf[len(buf)-1] == ',' {
buf = buf[:len(buf)-1]
}
buf = append(buf, '}')
return buf
}
func renderExsArray(args []any) []byte {
buf := []byte("[")
for _, val := range args {
if v, ok := val.(string); ok {
buf = regex.JoinBytes(buf, '"', EscapeExsArgs([]byte(v), '"'), '"', ',')
} else if v, ok := val.([]byte); ok {
buf = regex.JoinBytes(buf, '"', EscapeExsArgs(v, '"'), '"', ',')
} else if v, ok := val.(Map); ok {
buf = regex.JoinBytes(buf, renderExsMap(v), ',')
} else if v, ok := val.([]any); ok {
buf = regex.JoinBytes(buf, renderExsArray(v), ',')
} else {
buf = regex.JoinBytes(buf, goutil.ToType[[]byte](v), ',')
}
}
if buf[len(buf)-1] == ',' {
buf = buf[:len(buf)-1]
}
buf = append(buf, ']')
return buf
}