From 96f4ebc833871a1831cf0805dd9f5d2bf7fd8592 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 9 Dec 2012 18:35:02 -0500 Subject: [PATCH] Added preliminary web interface stuff --- templates/index.html | 1 + web/web.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 templates/index.html create mode 100644 web/web.go diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..573611a --- /dev/null +++ b/templates/index.html @@ -0,0 +1 @@ +Here's the stuff I can do! diff --git a/web/web.go b/web/web.go new file mode 100644 index 0000000..9971550 --- /dev/null +++ b/web/web.go @@ -0,0 +1,39 @@ +package web + +import ( + "html/template" + "log" + "net/http" +) + +// Define the address which this service will respond +var addr string = "0.0.0.0:6969" + +// path to the template files +var tmplPath string = "templates" + +// Return an index of the web functions available +func indexHandler(w http.ResponseWriter, r *http.Request) { + renderTemplate(w, r, "index", nil) +} + +// Try to load and execute a template for the given site +func renderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, data interface{}) { + t, err = template.ParseFiles(filepath.Join(tmplPath, tmpl+".html")) + if err != nil { + http.Error(w, "Could not load templates.", http.StatusInternalServerError) + log.Println(err) + return + } + + err = t.Execute(w, data) + if err != nil { + http.Error(w, "Could not load templates.", http.StatusInternalServerError) + log.Println(err) + } +} + +func main() { + http.HandleFunc("/", indexHandler) + log.Fatal(http.ListenAndServe(addr, nil)) +}