I followed the Go Writing Web Applications tutorial , but for some reason I was having trouble getting a CSS and JS service application. If I run my static page without a Go server, the CSS of the page works fine. On the other hand, when I start the Go server, CSS just doesn't work.
This is what my HTML 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 tag body
:
<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 app to serve the CSS and JavaScript I need?
Since then, the problem has been resolved, here is the main working one:
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 use:
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)
}
}