func execTemplate(page string, w http.ResponseWriter, view interface{}) { pagedata, err := staticfiles.Asset(htmlBase + page) if err != nil { http.Error(w, "Page not found", 404) return } templatedata, err := staticfiles.Asset(htmlBase + "template.html") if err != nil { http.Error(w, "Template not found", 404) return } t := template.New("template").Funcs(funcMap) t, err = t.Parse(string(templatedata)) if err != nil { log.Printf("Error parsing %s: %v", page, err) return } t, err = t.Parse(string(pagedata)) if err != nil { log.Printf("Error parsing %s: %v", page, err) return } // t, err := t.ParseFiles(htmlBase+page, htmlBase+"template.html") // if err != nil { // log.Printf("Error parsing %s: %v", page, err) // return // } err = t.ExecuteTemplate(w, "template", view) if err != nil { log.Printf("Error executing template for %s: %v", page, err) } }
// StaticfilesHandler handles finding static files for the server. func StaticfilesHandler(w http.ResponseWriter, r *http.Request) { uri := r.URL.RequestURI() if strings.HasPrefix(uri, "/") { uri = uri[1:] } data, err := staticfiles.Asset(uri) if err != nil { http.Error(w, "File not found", 404) return } if strings.HasSuffix(uri, ".css") { w.Header().Add("content-type", "text/css") } else if strings.HasSuffix(uri, ".js") { w.Header().Add("content-type", "text/javascript") } else if strings.HasSuffix(uri, ".woff") { w.Header().Add("content-type", "application/font-woff") } if _, err = w.Write(data); err != nil { log.Println(err) http.Error(w, err.Error(), 500) } }
// CatchAllHandler is a http handler which is meant to catch empty and non existing pages. func CatchAllHandler(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" || r.URL.Path == "" { w.Header().Set("Content-Type", "text/html; charset=utf-8") if auth.IsApprovedUser(r) { http.Redirect(w, r, pages.HOMEPAGE, 307) return } data, err := staticfiles.Asset(htmlBase + "index.html") if err != nil { http.Error(w, "Page not found", 404) return } if _, err = w.Write(data); err != nil { log.Println(err) http.Error(w, err.Error(), 404) } // index, err := os.Open(htmlBase + "index.html") // if err != nil { // log.Fatal(err) // } // //err :=indextemplate.Execute(w, nil) // _, err = io.Copy(w, index) // if err != nil { // log.Println("Error sending frontpage:", err) // } } else { http.Error(w, "This is not the page you are looking for!\n", 404) } }