]> khamidou — GitHub mirrors - board.git/commitdiff
initial commit master
authorkarim hamidou <khamidou@gmail.com>
Thu, 21 Feb 2013 12:50:55 +0000 (13:50 +0100)
committerkarim hamidou <khamidou@gmail.com>
Thu, 21 Feb 2013 12:50:55 +0000 (13:50 +0100)
board.go [new file with mode: 0644]
templates/index.html [new file with mode: 0644]
templates/newpost.html [new file with mode: 0644]
templates/post.html [new file with mode: 0644]

diff --git a/board.go b/board.go
new file mode 100644 (file)
index 0000000..8113caa
--- /dev/null
+++ b/board.go
@@ -0,0 +1,96 @@
+package main
+
+import (
+    "fmt"
+    "time"
+    "net/http"
+    "strconv"
+    "net/url"
+    "html/template"
+    "encoding/json"
+    "os"
+)
+
+type Post struct {
+    Score int
+    Title string
+    Contents string
+    Replies []Post; // Post []replies;
+}
+
+type BoardData struct {
+    Posts []Post;
+}
+
+var bData *BoardData
+var templates = template.Must(template.ParseFiles("templates/newpost.html", "templates/index.html", "templates/post.html"))
+
+func renderTemplate(w http.ResponseWriter, name string, data interface{}) {
+    realName := name + ".html";
+    err := templates.ExecuteTemplate(w, realName, data)
+
+    if err != nil {
+        http.Error(w, err.Error(), http.StatusInternalServerError)
+    }
+}
+
+func rootHandler(w http.ResponseWriter, r *http.Request) {
+    if "GET" == r.Method {
+        w.Header().Set("Content-Type", "text/html");
+        renderTemplate(w, "index", bData);
+    }
+}
+
+func postHandler(w http.ResponseWriter, r *http.Request) {
+    if "GET" == r.Method {
+        w.Header().Set("Content-Type", "text/html");
+        queryForm, _ := url.ParseQuery(r.URL.RawQuery)
+        idparam := queryForm.Get("id")
+        if("" == idparam) {
+            http.Error(w, "Bad request", http.StatusBadRequest)
+            return;
+        }
+        id, _ := strconv.Atoi(idparam);
+
+        if id < len(bData.Posts) {
+            renderTemplate(w, "post", bData.Posts[id]);
+        } else {
+            http.Error(w, "Post not found", http.StatusNotFound)
+        }
+    }
+}
+
+func newpostHandler(w http.ResponseWriter, r *http.Request) {
+    if "GET" == r.Method {
+
+        w.Header().Set("Content-Type", "text/html");
+        renderTemplate(w, "newpost", nil);
+
+    } else if "POST" == r.Method {
+        p := Post{Score: 1, Title: r.FormValue("title"), Contents: r.FormValue("contents")};
+        bData.Posts = append(bData.Posts, p);
+
+        http.Redirect(w, r, "/post?id=" + string(len(bData.Posts)), 303);
+        fmt.Fprintf(os.Stdout, "/post?id=" + string(len(bData.Posts)));
+    }
+}
+
+func saveDb(filename string) {
+
+    for ;; {
+        time.Sleep(60000000000)
+        b, _ := json.Marshal(bData);
+        os.Stdout.Write(b);
+    }
+}
+
+func main() {
+    var boardData *BoardData = &BoardData{};
+    bData = boardData;
+    go saveDb("stuff");
+    http.HandleFunc("/newpost", newpostHandler)
+    http.HandleFunc("/post", postHandler)
+    http.HandleFunc("/", rootHandler)
+    fmt.Println("Listening on http://localhost:8080/");
+    http.ListenAndServe(":8080", nil)
+}
diff --git a/templates/index.html b/templates/index.html
new file mode 100644 (file)
index 0000000..2c3363c
--- /dev/null
@@ -0,0 +1,9 @@
+<ul>
+{{with .Posts}}
+    {{range $index, $element := .}}
+    <li>
+        <a href="/post?id={{ $index }}">{{$element.Title}}</a>
+    </li>
+    {{end}}
+{{end}}
+</ul>
diff --git a/templates/newpost.html b/templates/newpost.html
new file mode 100644 (file)
index 0000000..b649fd5
--- /dev/null
@@ -0,0 +1,13 @@
+<h2>New Post</h2>
+
+<form action="/newpost" method="POST">
+    <div>
+        <input type="text" name="title" value="Post title"></input>
+    </div>
+    <div>
+        <input type="textarea" name="contents" value="Contents"></input>
+    </div>
+    <div>
+        <input type="submit" value="Post"></input>
+    </div>
+</form>
diff --git a/templates/post.html b/templates/post.html
new file mode 100644 (file)
index 0000000..4153c30
--- /dev/null
@@ -0,0 +1,4 @@
+<h2>{{.Title}}</h2>
+<p>
+    {{.Contents}}
+</p>