Skip to content

Commit cfb7393

Browse files
committed
initialize html temp once, use type structs for variables
1 parent 7d1abe2 commit cfb7393

File tree

1 file changed

+18
-9
lines changed
  • packages/orchestrator/internal/proxy

1 file changed

+18
-9
lines changed

packages/orchestrator/internal/proxy/proxy.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ var proxyBrowser502PageHtml string
2323

2424
var browserIdentityKeywords = []string{
2525
"mozilla", "chrome", "safari", "firefox", "edge", "opera", "msie",
26+
var browserTemplate = template.Must(template.New("template").Parse(proxyBrowser502PageHtml))
27+
28+
type htmlTemplateData struct {
29+
sandboxId string
30+
sandboxHost string
31+
sandboxPort string
32+
}
33+
34+
type jsonTemplateData struct {
35+
error string
36+
sandbox_id string
37+
port uint64
2638
}
2739

2840
type SandboxProxy struct {
@@ -157,12 +169,9 @@ func (p *SandboxProxy) proxyHandler(transport *http.Transport) func(w http.Respo
157169

158170
func (p *SandboxProxy) buildHtmlClosedPortError(sandboxId string, host string, port uint64) ([]byte, error) {
159171
htmlResponse := new(bytes.Buffer)
160-
htmlTmpl, err := template.New("template").Parse(proxyBrowser502PageHtml)
161-
if err != nil {
162-
return nil, err
163-
}
172+
htmlVars := htmlTemplateData{sandboxId: sandboxId, sandboxHost: host, sandboxPort: strconv.FormatUint(port, 10)}
164173

165-
err = htmlTmpl.Execute(htmlResponse, map[string]string{"sandboxId": sandboxId, "sandboxHost": host, "sandboxPort": fmt.Sprintf("%d", port)})
174+
err := browserTemplate.Execute(htmlResponse, htmlVars)
166175
if err != nil {
167176
return nil, err
168177
}
@@ -171,10 +180,10 @@ func (p *SandboxProxy) buildHtmlClosedPortError(sandboxId string, host string, p
171180
}
172181

173182
func (p *SandboxProxy) buildJsonClosedPortError(sandboxId string, port uint64) []byte {
174-
response := map[string]interface{}{
175-
"error": "The sandbox is running but port is not open",
176-
"sandbox_id": sandboxId,
177-
"port": port,
183+
response := jsonTemplateData{
184+
error: "The sandbox is running but port is not open",
185+
sandbox_id: sandboxId,
186+
port: port,
178187
}
179188

180189
responseBytes, _ := json.Marshal(response)

0 commit comments

Comments
 (0)