2023-11-12 17:43:23 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2024-10-06 17:30:25 +03:00
|
|
|
"crypto/sha512"
|
|
|
|
"fmt"
|
2023-11-12 17:43:23 +03:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2024-07-24 01:15:37 +03:00
|
|
|
"strings"
|
2023-11-12 17:43:23 +03:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-10-06 17:30:25 +03:00
|
|
|
func GetSHA512(s string) string {
|
|
|
|
hasher := sha512.New()
|
|
|
|
return fmt.Sprintf("%x", hasher.Sum([]byte(s)))
|
|
|
|
}
|
|
|
|
|
2024-07-24 01:15:37 +03:00
|
|
|
func TitleToID(name string) string {
|
|
|
|
return strings.ToLower(strings.ReplaceAll(name, " ", ""))
|
|
|
|
}
|
2023-11-12 17:43:23 +03:00
|
|
|
|
|
|
|
func CreateToken() string {
|
2024-07-24 01:15:37 +03:00
|
|
|
s := make([]byte, 32)
|
|
|
|
for i := 0; i < 32; i++ {
|
|
|
|
s[i] = byte(65 + rand.Intn(25))
|
|
|
|
}
|
|
|
|
return string(s)
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
2024-07-24 01:15:37 +03:00
|
|
|
func ErrorJSON(error string) fiber.Map {
|
|
|
|
return fiber.Map{
|
|
|
|
"error": error,
|
|
|
|
}
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
2024-07-24 01:15:37 +03:00
|
|
|
func GetIP(c *fiber.Ctx) string {
|
|
|
|
if c.Get("X-Real-IP") != "" {
|
|
|
|
return strings.Clone(c.Get("X-Real-IP"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.IP()
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrServer(c *fiber.Ctx) error {
|
2024-07-24 01:15:37 +03:00
|
|
|
return c.Status(http.StatusInternalServerError).JSON(ErrorJSON("Server error"))
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 17:30:25 +03:00
|
|
|
func ErrEntryExists(c *fiber.Ctx) error {
|
2024-07-24 01:15:37 +03:00
|
|
|
return c.Status(http.StatusConflict).JSON(ErrorJSON("Entry already exists"))
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 17:30:25 +03:00
|
|
|
func ErrEntryNotExists(c *fiber.Ctx) error {
|
|
|
|
return c.Status(http.StatusNotFound).JSON(ErrorJSON("Entry does not exist"))
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:43:23 +03:00
|
|
|
func ErrBadData(c *fiber.Ctx) error {
|
2024-07-24 01:15:37 +03:00
|
|
|
return c.Status(http.StatusBadRequest).JSON(ErrorJSON("Provided data is invalid"))
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrBadJSON(c *fiber.Ctx) error {
|
2024-07-24 01:15:37 +03:00
|
|
|
return c.Status(http.StatusBadRequest).JSON(ErrorJSON("Bad JSON data"))
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrAuth(c *fiber.Ctx) error {
|
2024-07-24 01:15:37 +03:00
|
|
|
return c.Status(http.StatusUnauthorized).JSON(ErrorJSON("Authentication failed"))
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 17:30:25 +03:00
|
|
|
func ErrNotFound(c *fiber.Ctx) error {
|
|
|
|
return c.Status(http.StatusNotFound).JSON(ErrorJSON("Requested endpoint not found"))
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:43:23 +03:00
|
|
|
func NoError(c *fiber.Ctx) error {
|
2024-07-24 01:15:37 +03:00
|
|
|
return c.Status(http.StatusOK).JSON(ErrorJSON(""))
|
2023-11-12 17:43:23 +03:00
|
|
|
}
|