2018-12-08 23:15:39 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-22 20:04:32 +01:00
|
|
|
"html/template"
|
2018-12-08 23:15:39 +01:00
|
|
|
"net/http"
|
2018-12-10 19:42:28 +01:00
|
|
|
|
2018-12-31 15:42:05 +01:00
|
|
|
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/common"
|
2018-12-11 20:42:44 +01:00
|
|
|
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers"
|
|
|
|
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models"
|
2018-12-13 18:13:47 +01:00
|
|
|
"github.com/go-martini/martini"
|
|
|
|
"github.com/martini-contrib/render"
|
2018-12-08 23:15:39 +01:00
|
|
|
)
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func index(rend render.Render) {
|
2018-12-31 15:42:05 +01:00
|
|
|
posts, err := models.Posts.FindAll()
|
|
|
|
if err != nil {
|
|
|
|
rend.Error(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.HTML(http.StatusOK, "index", posts)
|
|
|
|
|
2018-12-10 19:42:28 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func write(rend render.Render) {
|
|
|
|
rend.HTML(http.StatusOK, "write", &models.Post{})
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
2018-12-31 15:42:05 +01:00
|
|
|
post, err := models.Posts.FindOne(params["id"])
|
|
|
|
if err != nil {
|
|
|
|
rend.Error(http.StatusNotFound)
|
2018-12-13 18:13:47 +01:00
|
|
|
return
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.HTML(http.StatusOK, "write", post)
|
2018-12-08 23:15:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 20:04:32 +01:00
|
|
|
func savePost(rend render.Render, w http.ResponseWriter, r *http.Request) {
|
2018-12-10 19:42:28 +01:00
|
|
|
id := r.FormValue("id")
|
|
|
|
title := r.FormValue("title")
|
2018-12-22 20:04:32 +01:00
|
|
|
contentMd := r.FormValue("content")
|
|
|
|
contentHtml := helpers.MarkdownToHtml(contentMd)
|
2018-12-10 19:42:28 +01:00
|
|
|
|
2018-12-11 20:42:44 +01:00
|
|
|
if id != "" {
|
2018-12-31 15:42:05 +01:00
|
|
|
if err := models.Posts.Update(id, title, contentHtml, contentMd); err != nil {
|
|
|
|
rend.Error(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2018-12-11 20:42:44 +01:00
|
|
|
} else {
|
2018-12-31 15:42:05 +01:00
|
|
|
|
|
|
|
if _, err := models.Posts.Create(helpers.GenerateId(), title, contentHtml, contentMd); err != nil {
|
|
|
|
rend.Error(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.Redirect("/")
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
2018-12-31 15:42:05 +01:00
|
|
|
if err := models.Posts.DeletebyId(params["id"]); err != nil {
|
|
|
|
rend.Error(http.StatusNotFound)
|
|
|
|
return
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.Redirect("/")
|
2018-12-10 19:42:28 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 20:04:32 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-12-08 23:15:39 +01:00
|
|
|
func main() {
|
2018-12-31 15:42:05 +01:00
|
|
|
common.ConnectDB()
|
2018-12-08 23:15:39 +01:00
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
fmt.Println("Listening port 3000")
|
|
|
|
m := martini.Classic()
|
|
|
|
|
2018-12-22 20:04:32 +01:00
|
|
|
unescapeFuncMap := template.FuncMap{"unescape": unescape}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
m.Use(render.Renderer(render.Options{
|
|
|
|
Directory: "views",
|
|
|
|
Layout: "layout",
|
|
|
|
Extensions: []string{".html"},
|
2018-12-22 20:04:32 +01:00
|
|
|
Funcs: []template.FuncMap{unescapeFuncMap},
|
2018-12-13 18:13:47 +01:00
|
|
|
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)
|
2018-12-22 20:04:32 +01:00
|
|
|
m.Post("/getHtml", getHtmlPost)
|
2018-12-08 23:15:39 +01:00
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
m.Run()
|
2018-12-08 23:15:39 +01:00
|
|
|
}
|