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)