Проблема
Когда я хочу обслуживать статический HTML, MIME-тип ресурсов (CSS, JavaScript) также имеет значение text/html
, что приводит к ошибке.
Подход
Внутри ./assets/
- файлы index.html
, CSS и JavaScript.
const rootHtml = "./assets/index.html"
func main() {
http.HandleFunc("/", root)
log.Fatal(http.ListenAndServe(":3000", nil))
}
func root(w http.ResponseWriter, r *http.Request) {
if false {
http.Redirect(w, r, "/other-route/, http.StatusTemporaryRedirect)
} else {
http.ServeFile(w, r, rootHtml) // This should handle the index.html and all other resources
}
}
Каталог активов:
assets/
|-- js/
|-- css/
|-- index.html