blog/render.go

42 lines
1.0 KiB
Go

// render.go
package main
import (
"net/http"
"path/filepath"
"strings"
)
// RenderPage writes the content with the correct Content-Type.
func RenderPage(w http.ResponseWriter, content []byte, file ContentFile) {
if file.IsMarkdown {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
} else {
// Determine MIME type by extension
ext := strings.ToLower(filepath.Ext(file.OriginalPath))
switch ext {
case ".css":
w.Header().Set("Content-Type", "text/css")
case ".js":
w.Header().Set("Content-Type", "application/javascript")
case ".png":
w.Header().Set("Content-Type", "image/png")
case ".jpg", ".jpeg":
w.Header().Set("Content-Type", "image/jpeg")
case ".gif":
w.Header().Set("Content-Type", "image/gif")
case ".svg":
w.Header().Set("Content-Type", "image/svg+xml")
default:
w.Header().Set("Content-Type", "text/plain")
}
}
w.Write(content)
}
// RenderError handles error states
func RenderError(w http.ResponseWriter, statusCode int, message string) {
http.Error(w, message, statusCode)
}