// renderTemplate renders the named template and returns the rendered HTML to // the client. func renderTemplate(w http.ResponseWriter, name string, data interface{}) { w.Header().Set("Content-Type", "text/html; charset=utf8") err := mpresources.GetTemplates().ExecuteTemplate(w, name, data) if err != nil { serverError(w, err) } }
// httpError sends an HTTP error code to the client followed by an HTML error // page explaining the error. func httpError(w http.ResponseWriter, h *HTTPError) { w.Header().Set("Content-Type", "text/html; charset=utf8") w.WriteHeader(h.Status) err := mpresources.GetTemplates().ExecuteTemplate(w, "error.html", h) if err != nil { log.Printf("Internal error when rendering error.html: %s\n", err.Error()) } }
func main() { flag.Parse() source := *dbSource if source == "" { source = os.Getenv("MPDBSOURCE") if source == "" { fmt.Println("Please specify a non-empty -dbsource flag or set the MPDBSOURCE environment variable.") os.Exit(1) } } resDir := *resourceDir if resDir == "" { resDir = os.Getenv("MPRESDIR") } mpdb.DBSource = source mpresources.SetResourceDir(resDir) mpresources.GetTemplates() // Check that the templates load correctly err := mpdb.InitDB(*debug, *testdata) if err != nil { log.Printf("Database error during startup: %s\n", err) os.Exit(1) } listenAddr := fmt.Sprintf("%s:%d", *host, *port) m := mphandlers.CreateMux() app := http.Handler(m) if *debug { app = mphandlers.LoggingHandler{Handler: app} log.Printf("Listening on %s\n", listenAddr) } err = http.ListenAndServe(listenAddr, app) if err != nil { log.Printf("Server error in HTTP listener: %s\n", err) os.Exit(1) } }