From 21f9f78b3cf48d393cc8e21a130369e2877dde92 Mon Sep 17 00:00:00 2001 From: ds Date: Sun, 12 Jul 2026 18:42:37 +0000 Subject: [PATCH] set content-type for files --- microblog.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/microblog.go b/microblog.go index 9b12268..fa33a91 100644 --- a/microblog.go +++ b/microblog.go @@ -19,8 +19,29 @@ import ( const articlesDir = "data/articles" func main() { - http.HandleFunc("/", handlePage) - println("Server läuft auf http://localhost:8085") + //http.HandleFunc("/", handlePage) + 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) }