fix tests and improvements

This commit is contained in:
2024-11-20 10:56:06 +01:00
parent 7eec729f87
commit 774d1d28a0
2 changed files with 76 additions and 64 deletions
+11 -2
View File
@@ -42,6 +42,13 @@ type (
}
)
const (
contentType string = "Content-Type"
headerJSON string = "application/json"
headerHTML_UTF8 string = "text/html; charset=utf-8"
headerPlain_UTF8 string = "text/plain; charset=utf-8"
)
func defaultEngine() *Engine {
return &Engine{
mux: http.NewServeMux(),
@@ -165,21 +172,23 @@ func (e *Engine) Static(path, dir string) {
}
func (c *Context) JSON(code int, data any) {
c.W.WriteHeader(code)
c.W.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(c.W)
if err := encoder.Encode(data); err != nil {
http.Error(c.W, err.Error(), http.StatusInternalServerError)
return
}
c.W.WriteHeader(code)
}
func (c *Context) HTML(code int, name string, td *TemplateData) {
c.W.WriteHeader(code)
c.W.Header().Set("Content-Type", "text/html; charset=utf-8")
err := c.E.Render.Template(c.W, name, td)
if err != nil {
http.Error(c.W, err.Error(), http.StatusInternalServerError)
return
}
c.W.WriteHeader(code)
}
func newLogger(level slog.Level) {