I followed the Go Writing Web Applications tutorial but for whatever reason I am having trouble getting the app to serve CSS and JS. If I run my static page without the Go server the page CSS works fine. When I run the Go server on the other hand the CSS just doesn't work.
Here is what my HTML sort of looks like:
<link rel="stylesheet" href="../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../assets/css/custom.css">
then under the body
tag:
<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
My file tree looks like this:
go-affect/
├── data
│?? └── …
├── static
│?? ├── css
│?? │?? └── …
│?? └── js
│?? │?? └── …
├── tmpl
│?? ├── edit.html
│?? ├── index.html
│?? └── view.html
└── main.go
How do I get my Go application to serve the CSS and JavaScript I need?
EDIT:
The problem has since been solved, here is the working main:
func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/index/", makeHandler(indexHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
Here is an example of the handlers I am using:
func indexHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: title}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…