2018-12-08 23:15:39 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2018-12-10 19:42:28 +01:00
|
|
|
|
|
|
|
"github.com/blog/models"
|
2018-12-08 23:15:39 +01:00
|
|
|
)
|
|
|
|
|
2018-12-10 19:42:28 +01:00
|
|
|
var posts map[string]*models.Post
|
|
|
|
|
2018-12-08 23:15:39 +01:00
|
|
|
func index(w http.ResponseWriter, r *http.Request) {
|
2018-12-10 19:42:28 +01:00
|
|
|
fmt.Println(posts)
|
|
|
|
t, err := template.ParseFiles(
|
|
|
|
"views/write.html",
|
|
|
|
"views/header.html",
|
|
|
|
"views/footer.html",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, err.Error())
|
|
|
|
}
|
|
|
|
t.ExecuteTemplate(w, "write", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func write(w http.ResponseWriter, r *http.Request) {
|
2018-12-08 23:15:39 +01:00
|
|
|
t, err := template.ParseFiles(
|
|
|
|
"views/index.html",
|
|
|
|
"views/header.html",
|
|
|
|
"views/footer.html",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, err.Error())
|
|
|
|
}
|
|
|
|
t.ExecuteTemplate(w, "index", nil)
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:42:28 +01:00
|
|
|
func savePost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := r.FormValue("id")
|
|
|
|
title := r.FormValue("title")
|
|
|
|
content := r.FormValue("content")
|
|
|
|
|
|
|
|
post := models.NewPost(id, title, content)
|
|
|
|
post[post.Id] = post
|
|
|
|
http.Redirect(w, r, "/", 302)
|
|
|
|
}
|
|
|
|
|
2018-12-08 23:15:39 +01:00
|
|
|
func main() {
|
2018-12-10 19:42:28 +01:00
|
|
|
posts = make(map[string]*models.Post, 0)
|
2018-12-08 23:15:39 +01:00
|
|
|
fmt.Println("Listening port 8080")
|
|
|
|
|
2018-12-10 19:42:28 +01:00
|
|
|
http.Handle("/bower_components/", http.StripPrefix("/bower_components/", http.FileServer(http.Dir("./bower_components/"))))
|
2018-12-08 23:15:39 +01:00
|
|
|
http.HandleFunc("/", index)
|
2018-12-10 19:42:28 +01:00
|
|
|
http.HandleFunc("/write", write)
|
|
|
|
http.HandleFunc("/savePost", savePost)
|
2018-12-08 23:15:39 +01:00
|
|
|
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|