FreePad/lib/routes/routes_home.go

93 lines
2.0 KiB
Go
Raw Normal View History

2022-05-19 00:40:34 +03:00
package routes
import (
"net/http"
"net/url"
2022-05-20 01:40:21 +03:00
"time"
2022-05-19 00:40:34 +03:00
"github.com/JustKato/FreePad/lib/helper"
"github.com/JustKato/FreePad/lib/objects"
2022-05-19 00:40:34 +03:00
"github.com/gin-gonic/gin"
"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")
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
}
// Get the maximum pad size, so that we may notify the client-side to match server-side
maximumPadSize := helper.GetMaximumPadSize()
// 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-06-03 23:15:20 +03:00
post := objects.GetPost(postName, true)
2022-05-19 00:40:34 +03:00
c.HTML(200, "page.html", gin.H{
"title": postName,
"post_content": post.Content,
"maximumPadSize": maximumPadSize,
"last_modified": post.LastModified,
"views": post.Views,
"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,
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
})
})
}