2022-05-19 00:40:34 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-06-01 13:26:54 +03:00
|
|
|
"net/http"
|
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-06-01 13:26:54 +03:00
|
|
|
if postName == `views_storage.json` {
|
|
|
|
// Redirect the user to the homepage as this is a reserved keyword
|
|
|
|
c.Redirect(http.StatusPermanentRedirect, "/")
|
|
|
|
// Do not proceed further
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-21 16:17:25 +03:00
|
|
|
// Get the maximum pad size, so that we may notify the client-side to match server-side
|
|
|
|
maximumPadSize := helper.GetMaximumPadSize()
|
|
|
|
|
2022-05-20 00:46:14 +03:00
|
|
|
// Sanitize the postName
|
|
|
|
newPostName, err := url.QueryUnescape(postName)
|
|
|
|
if err == nil {
|
|
|
|
postName = newPostName
|
|
|
|
}
|
2022-06-01 18:07:33 +03:00
|
|
|
postName = sanitize.XSS(sanitize.SingleLine(postName))
|
2022-05-20 00:46:14 +03:00
|
|
|
|
|
|
|
post := objects.GetPost(postName)
|
|
|
|
|
2022-05-19 00:40:34 +03:00
|
|
|
c.HTML(200, "page.html", gin.H{
|
2022-05-21 16:17:25 +03:00
|
|
|
"title": postName,
|
|
|
|
"post_content": post.Content,
|
|
|
|
"maximumPadSize": maximumPadSize,
|
|
|
|
"last_modified": post.LastModified,
|
2022-06-01 13:26:54 +03:00
|
|
|
"views": post.Views,
|
2022-05-21 16:17:25 +03:00
|
|
|
"domain_base": helper.GetDomainBase(),
|
2022-05-20 01:40:21 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-06-01 18:07:33 +03:00
|
|
|
postName = sanitize.XSS(sanitize.SingleLine(postName))
|
2022-05-20 01:40:21 +03:00
|
|
|
|
|
|
|
p := objects.Post{
|
|
|
|
Name: postName,
|
|
|
|
Content: postContent,
|
2022-06-01 13:26:54 +03:00
|
|
|
Views: 0, // This can just be ignored
|
2022-05-20 01:40:21 +03:00
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|