set some default headers

This commit is contained in:
gutmet 2023-04-09 09:57:26 +02:00
parent 0b670c1194
commit 20dd050863

View File

@ -9,8 +9,25 @@ import (
"path/filepath"
)
func setContentType(w http.ResponseWriter, ext string) {
var t string
switch ext {
case ".html":
t = "text/html"
case ".js":
t = "text/javascript"
default:
t = ""
}
if t != "" {
w.Header().Add("Content-Type", t)
}
}
func viewHandler(root string, w http.ResponseWriter, r *http.Request) {
file := filepath.Join(root, filepath.FromSlash(r.URL.Path[1:]))
relativePath := filepath.FromSlash(r.URL.Path[1:])
file := filepath.Join(root, relativePath)
setContentType(w, filepath.Ext(relativePath))
info, err := os.Stat(file)
if err != nil {
fmt.Fprint(w, err)