v5.0 cleanup

This commit is contained in:
ngn
2024-10-06 17:30:25 +03:00
parent 38f51935b9
commit d5e875ae53
29 changed files with 711 additions and 427 deletions

12
api/util/log.go Normal file
View File

@ -0,0 +1,12 @@
package util
import (
"log"
"os"
)
var (
Info = log.New(os.Stdout, "\033[34m[info]\033[0m ", log.Ltime|log.Lshortfile).Printf
Warn = log.New(os.Stderr, "\033[33m[warn]\033[0m ", log.Ltime|log.Lshortfile).Printf
Fail = log.New(os.Stderr, "\033[31m[fail]\033[0m ", log.Ltime|log.Lshortfile).Printf
)

View File

@ -1,7 +1,8 @@
package util
import (
"log"
"crypto/sha512"
"fmt"
"math/rand"
"net/http"
"strings"
@ -9,6 +10,11 @@ import (
"github.com/gofiber/fiber/v2"
)
func GetSHA512(s string) string {
hasher := sha512.New()
return fmt.Sprintf("%x", hasher.Sum([]byte(s)))
}
func TitleToID(name string) string {
return strings.ToLower(strings.ReplaceAll(name, " ", ""))
}
@ -21,15 +27,6 @@ func CreateToken() string {
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
}
return false
}
func ErrorJSON(error string) fiber.Map {
return fiber.Map{
"error": error,
@ -48,10 +45,14 @@ func ErrServer(c *fiber.Ctx) error {
return c.Status(http.StatusInternalServerError).JSON(ErrorJSON("Server error"))
}
func ErrExists(c *fiber.Ctx) error {
func ErrEntryExists(c *fiber.Ctx) error {
return c.Status(http.StatusConflict).JSON(ErrorJSON("Entry already exists"))
}
func ErrEntryNotExists(c *fiber.Ctx) error {
return c.Status(http.StatusNotFound).JSON(ErrorJSON("Entry does not exist"))
}
func ErrBadData(c *fiber.Ctx) error {
return c.Status(http.StatusBadRequest).JSON(ErrorJSON("Provided data is invalid"))
}
@ -64,6 +65,10 @@ func ErrAuth(c *fiber.Ctx) error {
return c.Status(http.StatusUnauthorized).JSON(ErrorJSON("Authentication failed"))
}
func ErrNotFound(c *fiber.Ctx) error {
return c.Status(http.StatusNotFound).JSON(ErrorJSON("Requested endpoint not found"))
}
func NoError(c *fiber.Ctx) error {
return c.Status(http.StatusOK).JSON(ErrorJSON(""))
}