API redesign, better styling for the blog page

This commit is contained in:
ngn
2024-07-24 01:15:37 +03:00
parent 51b9214a5c
commit 6400f1fa95
16 changed files with 622 additions and 558 deletions

View File

@ -4,57 +4,66 @@ import (
"log"
"math/rand"
"net/http"
"strings"
"github.com/gofiber/fiber/v2"
)
var charlist []rune = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func TitleToID(name string) string {
return strings.ToLower(strings.ReplaceAll(name, " ", ""))
}
func CreateToken() string {
b := make([]rune, 20)
for i := range b {
b[i] = charlist[rand.Intn(len(charlist))]
}
return string(b)
s := make([]byte, 32)
for i := 0; i < 32; i++ {
s[i] = byte(65 + rand.Intn(25))
}
return string(s)
}
func ErrorCheck(err error, c *fiber.Ctx) bool{
if err != nil {
log.Printf("Server error: '%s' on %s\n", err, c.Path())
return true
}
func ErrorCheck(err error, c *fiber.Ctx) bool {
if err != nil {
log.Printf("Server error: '%s' on %s\n", err, c.Path())
return true
}
return false
return false
}
func ErrorJSON(error string) fiber.Map{
return fiber.Map {
"error": error,
}
func ErrorJSON(error string) fiber.Map {
return fiber.Map{
"error": error,
}
}
func GetIP(c *fiber.Ctx) string {
if c.Get("X-Real-IP") != "" {
return strings.Clone(c.Get("X-Real-IP"))
}
return c.IP()
}
func ErrServer(c *fiber.Ctx) error {
return c.Status(http.StatusInternalServerError).JSON(ErrorJSON("Server error"))
return c.Status(http.StatusInternalServerError).JSON(ErrorJSON("Server error"))
}
func ErrExists(c *fiber.Ctx) error {
return c.Status(http.StatusConflict).JSON(ErrorJSON("Entry already exists"))
return c.Status(http.StatusConflict).JSON(ErrorJSON("Entry already exists"))
}
func ErrBadData(c *fiber.Ctx) error {
return c.Status(http.StatusBadRequest).JSON(ErrorJSON("Provided data is invalid"))
return c.Status(http.StatusBadRequest).JSON(ErrorJSON("Provided data is invalid"))
}
func ErrBadJSON(c *fiber.Ctx) error {
return c.Status(http.StatusBadRequest).JSON(ErrorJSON("Bad JSON data"))
return c.Status(http.StatusBadRequest).JSON(ErrorJSON("Bad JSON data"))
}
func ErrAuth(c *fiber.Ctx) error {
return c.Status(http.StatusUnauthorized).JSON(ErrorJSON("Authentication failed"))
return c.Status(http.StatusUnauthorized).JSON(ErrorJSON("Authentication failed"))
}
func NoError(c *fiber.Ctx) error {
return c.Status(http.StatusOK).JSON(ErrorJSON(""))
return c.Status(http.StatusOK).JSON(ErrorJSON(""))
}