From: karim hamidou Date: Thu, 21 Feb 2013 12:50:55 +0000 (+0100) Subject: initial commit X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;p=board.git initial commit --- diff --git a/board.go b/board.go new file mode 100644 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 index 0000000..2c3363c --- /dev/null +++ b/templates/index.html @@ -0,0 +1,9 @@ + diff --git a/templates/newpost.html b/templates/newpost.html new file mode 100644 index 0000000..b649fd5 --- /dev/null +++ b/templates/newpost.html @@ -0,0 +1,13 @@ +

New Post

+ +
+
+ +
+
+ +
+
+ +
+
diff --git a/templates/post.html b/templates/post.html new file mode 100644 index 0000000..4153c30 --- /dev/null +++ b/templates/post.html @@ -0,0 +1,4 @@ +

{{.Title}}

+

+ {{.Contents}} +