set content-type for files

This commit is contained in:
ds
2026-07-12 18:42:37 +00:00
parent 9b5a7083a7
commit 21f9f78b3c
+23 -2
View File
@@ -19,8 +19,29 @@ import (
const articlesDir = "data/articles" const articlesDir = "data/articles"
func main() { func main() {
http.HandleFunc("/", handlePage) //http.HandleFunc("/", handlePage)
println("Server läuft auf http://localhost:8085") println("Server startet http://localhost:8085")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
ext := filepath.Ext(path)
switch ext {
case ".css":
w.Header().Set("Content-Type", "text/css")
case ".jpg", ".jpeg":
w.Header().Set("Content-Type", "image/jpeg")
case ".png":
w.Header().Set("Content-Type", "image/png")
case ".gif":
w.Header().Set("Content-Type", "image/gif")
default:
handlePage
}
http.ServeFile(w, r, "."+path)
})
http.ListenAndServe(":8085", nil) http.ListenAndServe(":8085", nil)
} }