2022-05-15 12:43:42 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-05-15 15:59:30 +03:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2022-05-15 12:43:42 +03:00
|
|
|
"github.com/JustKato/FreePad/controllers/post"
|
2022-05-15 15:59:30 +03:00
|
|
|
"github.com/JustKato/FreePad/helper"
|
2022-05-15 12:43:42 +03:00
|
|
|
"github.com/JustKato/FreePad/types"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ApiRoutes(route *gin.RouterGroup) {
|
|
|
|
|
2022-05-15 20:59:46 +03:00
|
|
|
// Bind the rate limiter
|
|
|
|
helper.BindRateLimiter(route)
|
|
|
|
|
2022-05-15 12:43:42 +03:00
|
|
|
route.POST("/post", func(ctx *gin.Context) {
|
|
|
|
// Get the name of the post
|
|
|
|
postName := ctx.PostForm("name")
|
|
|
|
// Get the content of the post
|
|
|
|
postContent := ctx.PostForm("content")
|
|
|
|
|
|
|
|
// Create my post
|
|
|
|
myPost, err := post.Create(postName, postContent)
|
|
|
|
if err != nil {
|
2022-05-17 23:27:20 +03:00
|
|
|
fmt.Println("Error", err)
|
2022-05-15 12:43:42 +03:00
|
|
|
ctx.JSON(400, types.FreeError{
|
|
|
|
Error: err.Error(),
|
|
|
|
Message: "There has been an error processing your request",
|
|
|
|
})
|
2022-05-17 23:27:20 +03:00
|
|
|
return
|
2022-05-15 12:43:42 +03:00
|
|
|
}
|
|
|
|
|
2022-05-15 15:59:30 +03:00
|
|
|
ctx.JSON(200, gin.H{
|
|
|
|
"message": "Post succesfully created",
|
|
|
|
"post": myPost,
|
|
|
|
"link": fmt.Sprintf("%s/%s", helper.GetDomainBase(), url.QueryEscape(myPost.Name)),
|
|
|
|
})
|
2022-05-15 12:43:42 +03:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2022-05-15 19:01:29 +03:00
|
|
|
route.GET("/post", func(ctx *gin.Context) {
|
|
|
|
// Get the name of the post
|
|
|
|
postName := ctx.PostForm("name")
|
|
|
|
|
|
|
|
myPost, err := post.Retrieve(postName)
|
|
|
|
if err != nil {
|
2022-05-17 23:27:20 +03:00
|
|
|
fmt.Println("Error", err)
|
2022-05-15 19:01:29 +03:00
|
|
|
ctx.JSON(400, types.FreeError{
|
|
|
|
Error: err.Error(),
|
|
|
|
Message: "There has been an error processing your request",
|
|
|
|
})
|
2022-05-17 23:27:20 +03:00
|
|
|
return
|
2022-05-15 19:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the post list
|
|
|
|
ctx.JSON(200, myPost)
|
|
|
|
})
|
|
|
|
|
2022-05-15 12:43:42 +03:00
|
|
|
route.GET("/posts", func(ctx *gin.Context) {
|
|
|
|
// Return the post list
|
|
|
|
ctx.JSON(200, post.GetPostList())
|
|
|
|
})
|
|
|
|
|
2022-05-15 19:01:29 +03:00
|
|
|
// Add in health checks
|
|
|
|
route.GET("/health", healthCheck)
|
2022-05-17 03:20:31 +03:00
|
|
|
|
2022-05-15 19:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func healthCheck(ctx *gin.Context) {
|
|
|
|
ctx.JSON(200, gin.H{
|
|
|
|
"message": "Healthy",
|
|
|
|
})
|
2022-05-15 12:43:42 +03:00
|
|
|
}
|