fix: provide content type for any other result

This commit is contained in:
Reinaldy Rafli 2023-05-05 22:29:56 +07:00
parent 4a4552b944
commit 2323cf3f89
Signed by: aldy505
GPG Key ID: A3F8A7E23DA2AD94
1 changed files with 3 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
var baseDirectory string
func handler(w http.ResponseWriter, r *http.Request) {
log.Printf("Incoming request: %s %s", r.Method, r.URL.Path)
filePath := path.Join(baseDirectory, r.URL.Path)
file, err := os.Stat(filePath)
if err == nil && !file.IsDir() {
@ -29,6 +30,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", t)
http.ServeFile(w, r, gz)
} else {
w.Header().Add("Content-Type", t)
http.ServeFile(w, r, filePath)
}
} else {
@ -37,6 +39,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
file, err := os.Stat(index)
if err == nil && !file.IsDir() {
// index.html exists
w.Header().Add("Content-Type", "text/html")
http.ServeFile(w, r, index)
} else {
// index.html does not exist