package main import ( "fmt" "html/template" "net/http" "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers" "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models" "github.com/go-martini/martini" "github.com/martini-contrib/render" ) var posts map[string]*models.Post func index(rend render.Render) { rend.HTML(http.StatusOK, "index", posts) } func write(rend render.Render) { rend.HTML(http.StatusOK, "write", &models.Post{}) } func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) { id := params["id"] post, found := posts[id] if !found { rend.Redirect("/") return } rend.HTML(http.StatusOK, "write", post) } func savePost(rend render.Render, w http.ResponseWriter, r *http.Request) { id := r.FormValue("id") title := r.FormValue("title") contentMd := r.FormValue("content") contentHtml := helpers.MarkdownToHtml(contentMd) var post *models.Post if id != "" { post = posts[id] post.Title = title post.ContentHtml = contentHtml post.ContentMd = contentMd } else { post := models.NewPost(helpers.GenerateId(), title, contentHtml, contentMd) posts[post.Id] = post } rend.Redirect("/") } func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) { id := params["id"] if id == "" { http.NotFound(w, r) } delete(posts, id) rend.Redirect("/") } func getHtmlPost(rend render.Render, w http.ResponseWriter, r *http.Request) { md := r.FormValue("md") rend.JSON(http.StatusOK, map[string]interface{}{ "html": helpers.MarkdownToHtml(md), }) } func unescape(s string) interface{} { return template.HTML(s) } func main() { posts = make(map[string]*models.Post, 0) fmt.Println("Listening port 3000") m := martini.Classic() unescapeFuncMap := template.FuncMap{"unescape": unescape} m.Use(render.Renderer(render.Options{ Directory: "views", Layout: "layout", Extensions: []string{".html"}, Funcs: []template.FuncMap{unescapeFuncMap}, Charset: "UTF-8", IndentJSON: true, })) staticOptions := martini.StaticOptions{Prefix: "bower_components"} m.Use(martini.Static("bower_components", staticOptions)) m.Get("/", index) m.Get("/write", write) m.Get("/edit/:id", edit) m.Post("/savePost", savePost) m.Get("/deletePost/:id", deletePost) m.Post("/getHtml", getHtmlPost) m.Run() }