Commits

Miki Tebeka committed 2242491

Desktop webapp example

  • Participants
  • Parent commits e9e88f9

Comments (0)

Files changed (1)

+// Example of simple "Guest Book" desktop web application
+package main
+
+import (
+	"fmt"
+	"html/template"
+	"net/http"
+	"os/exec"
+	"time"
+)
+
+const (
+	port = 8765
+)
+
+// Guest list stored in memory for simplicity, on real application we'll
+// probably want some persistence
+var guestList []string
+
+// indexHandler serves the main page
+func indexHandler(w http.ResponseWriter, req *http.Request) {
+	t := template.New("index.html")
+	t, err := t.Parse(indexHTML)
+	if err != nil {
+		message := fmt.Sprintf("bad template: %s", err)
+		http.Error(w, message, http.StatusInternalServerError)
+	}
+
+	t.Execute(w, guestList)
+
+}
+
+// addHandler add a name to the names list, then redirects back to /
+func addHandler(w http.ResponseWriter, req *http.Request) {
+	guest := req.FormValue("name")
+	if len(guest) > 0 {
+		guestList = append(guestList, guest)
+	}
+
+	http.Redirect(w, req, "/", http.StatusFound)
+}
+
+// openBrowser waits one second and then open web browser on us
+func openBrowser() {
+	time.Sleep(time.Second)
+	url := fmt.Sprintf("http://localhost:%d", port)
+	exec.Command("start", url).Start()
+}
+
+func main() {
+	http.HandleFunc("/", indexHandler)
+	http.HandleFunc("/add", addHandler)
+	go openBrowser()
+	http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
+}
+
+var indexHTML = `
+<!DOCTYPE html>
+<html>
+    <head>
+		<title>Guest Book ::Web GUI</title>
+    </head>
+    <body>
+		<h1>Guest Book :: Web GUI</h1>
+		<form action="/add" method="post">
+		Name: <input name="name" id="name" /><submit value="Sign Guest Book">
+		</form>
+		<hr />
+		<h4>Previous Guests</h4>
+		<ul>
+			{{range .}}
+			<li>{{.}}</li>
+			{{end}}
+		</ul>
+	</body>
+	<script>
+		document.getElementById('name').focus();
+	</script>
+</html>
+`