Skip to content

Commit

Permalink
Specify mimeType explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
HashidaTKS committed Sep 24, 2024
1 parent 5505183 commit c14231b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/tools/https_server/https_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,39 @@ import (
"flag"
"fmt"
"net/http"
"path/filepath"
)

func customFileServer(fs http.FileSystem) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
ext := filepath.Ext(path)

mimeTypes := map[string]string{
".js": "application/javascript; charset=UTF-8",
".mjs": "application/javascript; charset=UTF-8",
".css": "text/css; charset=UTF-8",
".html": "text/html; charset=UTF-8",
".json": "application/json; charset=UTF-8",
}

if mimeType, ok := mimeTypes[ext]; ok {
w.Header().Set("Content-Type", mimeType)
}

http.FileServer(fs).ServeHTTP(w, r)
})
}

func main() {
var (
root = flag.String("root", "web/", "root path")
)
flag.Parse()
http.Handle("/", http.FileServer(http.Dir(*root)))

fs := http.Dir(*root)
http.Handle("/", customFileServer(fs))

err := http.ListenAndServeTLS(":10041", "cert.pem", "key.pem", nil)
if err != nil {
fmt.Printf("ERROR : %s", err)
Expand Down

0 comments on commit c14231b

Please sign in to comment.