From 20dd050863345efa84068460671bdc90bf983356 Mon Sep 17 00:00:00 2001 From: gutmet Date: Sun, 9 Apr 2023 09:57:26 +0200 Subject: [PATCH] set some default headers --- server.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index fcefbc2..0fd1344 100644 --- a/server.go +++ b/server.go @@ -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)