Skip to content

Commit 5d4338f

Browse files
committed
feat: refactor template handling and ensure proper response headers
1 parent 974d238 commit 5d4338f

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

internal/web/handlers.go

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -193,48 +193,22 @@ func (h *APIHandler) PreviewTemplateHandler(w http.ResponseWriter, r *http.Reque
193193

194194
var tpl *template.Template
195195
var err error
196-
if reqData.Content != "" {
197-
tpl, err = template.New(tmplName).Funcs(template.FuncMap{
198-
"image": func(imageSrc string) template.HTML {
199-
return template.HTML(fmt.Sprintf("<img src=\"%s\" style=\"max-width: 100%%; height: auto;\">",
200-
html.EscapeString("/api/images/"+imageSrc)))
201-
},
202-
"imageWithSize": func(imageSrc, width, height string) template.HTML {
203-
return template.HTML(fmt.Sprintf("<img src=\"%s\" width=\"%s\" height=\"%s\">",
204-
html.EscapeString("/api/images/"+imageSrc), html.EscapeString(width), html.EscapeString(height)))
205-
},
206-
"property": func(key string) template.HTML {
207-
return template.HTML(fmt.Sprintf("<code>PROPERTY(%s)</code>", key))
208-
},
209-
}).Parse(reqData.Content)
210-
if err != nil {
211-
http.Error(w, fmt.Sprintf("템플릿 파싱 실패: %v", err), http.StatusBadRequest)
212-
return
213-
}
214-
} else {
215-
filePath := filepath.Join(h.TemplateManager.BaseDir(), tmplName+".html")
216-
contentBytes, err := os.ReadFile(filePath)
217-
if err != nil {
218-
http.Error(w, fmt.Sprintf("템플릿을 찾을 수 없습니다: %s", tmplName), http.StatusNotFound)
219-
return
220-
}
221-
tpl, err = template.New(tmplName).Funcs(template.FuncMap{
222-
"image": func(imageSrc string) template.HTML {
223-
return template.HTML(fmt.Sprintf("<img src=\"%s\" alt=\"\" style=\"max-width: 100%%; height: auto;\">",
224-
html.EscapeString("/api/images/"+imageSrc)))
225-
},
226-
"imageWithSize": func(imageSrc, width, height string) template.HTML {
227-
return template.HTML(fmt.Sprintf("<img src=\"%s\" width=\"%s\" height=\"%s\">",
228-
html.EscapeString("/api/images/"+imageSrc), html.EscapeString(width), html.EscapeString(height)))
229-
},
230-
"property": func(key string) template.HTML {
231-
return template.HTML(fmt.Sprintf("<code>PROPERTY(%s)</code>", key))
232-
},
233-
}).Parse(string(contentBytes))
234-
if err != nil {
235-
http.Error(w, fmt.Sprintf("템플릿 파싱 실패: %v", err), http.StatusInternalServerError)
236-
return
237-
}
196+
tpl, err = template.New(tmplName).Funcs(template.FuncMap{
197+
"image": func(imageSrc string) template.HTML {
198+
return template.HTML(fmt.Sprintf("<img src=\"%s\" style=\"max-width: 100%%; height: auto;\">",
199+
html.EscapeString("/api/images/"+imageSrc)))
200+
},
201+
"imageWithSize": func(imageSrc, width, height string) template.HTML {
202+
return template.HTML(fmt.Sprintf("<img src=\"%s\" width=\"%s\" height=\"%s\">",
203+
html.EscapeString("/api/images/"+imageSrc), html.EscapeString(width), html.EscapeString(height)))
204+
},
205+
"property": func(key string) template.HTML {
206+
return template.HTML(fmt.Sprintf("<code>PROPERTY(%s)</code>", key))
207+
},
208+
}).Parse(reqData.Content)
209+
if err != nil {
210+
http.Error(w, fmt.Sprintf("템플릿 파싱 실패: %v", err), http.StatusBadRequest)
211+
return
238212
}
239213

240214
sampleData := map[string]interface{}{
@@ -249,8 +223,6 @@ func (h *APIHandler) PreviewTemplateHandler(w http.ResponseWriter, r *http.Reque
249223
return
250224
}
251225
renderedHTML := buf.String()
252-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
253-
w.WriteHeader(http.StatusOK)
254226
premail, err := premailer.NewPremailerFromString(renderedHTML, premailer.NewOptions())
255227
if err != nil {
256228
http.Error(w, fmt.Sprintf("프리메일러 생성 실패: %v", err), http.StatusInternalServerError)
@@ -261,6 +233,8 @@ func (h *APIHandler) PreviewTemplateHandler(w http.ResponseWriter, r *http.Reque
261233
http.Error(w, fmt.Sprintf("프리메일러 변환 실패: %v", err), http.StatusInternalServerError)
262234
return
263235
}
236+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
237+
w.WriteHeader(http.StatusOK)
264238
_, _ = w.Write([]byte(renderedHTML))
265239
}
266240

0 commit comments

Comments
 (0)