2022-05-19 00:40:34 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-05-20 00:46:14 +03:00
|
|
|
"net/url"
|
2022-05-20 01:40:21 +03:00
|
|
|
"time"
|
2022-05-20 00:46:14 +03:00
|
|
|
|
2022-05-19 00:40:34 +03:00
|
|
|
"github.com/JustKato/FreePad/lib/helper"
|
2022-05-20 00:46:14 +03:00
|
|
|
"github.com/JustKato/FreePad/lib/objects"
|
2022-05-19 00:40:34 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-05-20 00:46:14 +03:00
|
|
|
"github.com/mrz1836/go-sanitize"
|
2022-05-19 00:40:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func HomeRoutes(router *gin.Engine) {
|
|
|
|
|
|
|
|
router.GET("/", func(c *gin.Context) {
|
|
|
|
c.HTML(200, "index.html", gin.H{
|
|
|
|
"title": "HomePage",
|
|
|
|
"domain_base": helper.GetDomainBase(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.GET("/:post", func(c *gin.Context) {
|
|
|
|
// Get the post we are looking for.
|
|
|
|
postName := c.Param("post")
|
|
|
|
|
2022-05-20 00:46:14 +03:00
|
|
|
// Sanitize the postName
|
|
|
|
newPostName, err := url.QueryUnescape(postName)
|
|
|
|
if err == nil {
|
|
|
|
postName = newPostName
|
|
|
|
}
|
|
|
|
postName = sanitize.AlphaNumeric(postName, true)
|
|
|
|
|
|
|
|
post := objects.GetPost(postName)
|
|
|
|
|
2022-05-19 00:40:34 +03:00
|
|
|
c.HTML(200, "page.html", gin.H{
|
2022-05-20 01:40:21 +03:00
|
|
|
"title": postName,
|
|
|
|
"post_content": post.Content,
|
|
|
|
"last_modified": post.LastModified,
|
|
|
|
"domain_base": helper.GetDomainBase(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.POST("/:post", func(c *gin.Context) {
|
|
|
|
// Get the post we are looking for.
|
|
|
|
postName := c.Param("post")
|
|
|
|
postContent := c.PostForm("content")
|
|
|
|
|
|
|
|
// Sanitize the postName
|
|
|
|
newPostName, err := url.QueryUnescape(postName)
|
|
|
|
if err == nil {
|
|
|
|
postName = newPostName
|
|
|
|
}
|
|
|
|
postName = sanitize.AlphaNumeric(postName, true)
|
|
|
|
|
|
|
|
p := objects.Post{
|
|
|
|
Name: postName,
|
|
|
|
Content: postContent,
|
|
|
|
LastModified: time.Now().Format("02/01/2006 03:04:05 PM"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the post
|
|
|
|
err = objects.WritePost(p)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, gin.H{
|
|
|
|
"error": err,
|
|
|
|
})
|
|
|
|
|
|
|
|
// End
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the success message
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"pad": p,
|
2022-05-19 00:40:34 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|