restructure the API and update the admin script
This commit is contained in:
@ -5,8 +5,17 @@ import (
|
||||
"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
|
||||
const (
|
||||
COLOR_BLUE = "\033[34m"
|
||||
COLOR_YELLOW = "\033[33m"
|
||||
COLOR_RED = "\033[31m"
|
||||
COLOR_CYAN = "\033[36m"
|
||||
COLOR_RESET = "\033[0m"
|
||||
)
|
||||
|
||||
var (
|
||||
Debg = log.New(os.Stdout, COLOR_CYAN+"[debg]"+COLOR_RESET+" ", log.Ltime|log.Lshortfile).Printf
|
||||
Info = log.New(os.Stdout, COLOR_BLUE+"[info]"+COLOR_RESET+" ", log.Ltime|log.Lshortfile).Printf
|
||||
Warn = log.New(os.Stderr, COLOR_YELLOW+"[warn]"+COLOR_RESET+" ", log.Ltime|log.Lshortfile).Printf
|
||||
Fail = log.New(os.Stderr, COLOR_RED+"[fail]"+COLOR_RESET+" ", log.Ltime|log.Lshortfile).Printf
|
||||
)
|
||||
|
89
api/util/res.go
Normal file
89
api/util/res.go
Normal file
@ -0,0 +1,89 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/ngn13/website/api/config"
|
||||
"github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
func IP(c *fiber.Ctx) string {
|
||||
conf := c.Locals("config").(*config.Type)
|
||||
ip_header := conf.GetStr("ip_header")
|
||||
|
||||
if ip_header != "" && c.Get(ip_header) != "" {
|
||||
return strings.Clone(c.Get(ip_header))
|
||||
}
|
||||
|
||||
return c.IP()
|
||||
}
|
||||
|
||||
func Markdown(c *fiber.Ctx, raw []byte) error {
|
||||
exts := blackfriday.FencedCode
|
||||
exts |= blackfriday.NoEmptyLineBeforeBlock
|
||||
exts |= blackfriday.HardLineBreak
|
||||
|
||||
c.Set("Content-Type", "text/html; charset=utf-8")
|
||||
return c.Send(blackfriday.Run(raw, blackfriday.WithExtensions(exts)))
|
||||
}
|
||||
|
||||
func JSON(c *fiber.Ctx, code int, data fiber.Map) error {
|
||||
if data == nil {
|
||||
data = fiber.Map{}
|
||||
data["error"] = ""
|
||||
} else if _, ok := data["error"]; !ok {
|
||||
data["error"] = ""
|
||||
}
|
||||
|
||||
if data["error"] == 200 {
|
||||
Warn("200 response with an error at %s", c.Path())
|
||||
}
|
||||
|
||||
return c.Status(code).JSON(data)
|
||||
}
|
||||
|
||||
func ErrInternal(c *fiber.Ctx, err error) error {
|
||||
Warn("Internal server error at %s: %s", c.Path(), err.Error())
|
||||
|
||||
return JSON(c, http.StatusInternalServerError, fiber.Map{
|
||||
"error": "Server error",
|
||||
})
|
||||
}
|
||||
|
||||
func ErrExists(c *fiber.Ctx) error {
|
||||
return JSON(c, http.StatusConflict, fiber.Map{
|
||||
"error": "Entry already exists",
|
||||
})
|
||||
}
|
||||
|
||||
func ErrNotExist(c *fiber.Ctx) error {
|
||||
return JSON(c, http.StatusNotFound, fiber.Map{
|
||||
"error": "Entry does not exist",
|
||||
})
|
||||
}
|
||||
|
||||
func ErrBadReq(c *fiber.Ctx) error {
|
||||
return JSON(c, http.StatusBadRequest, fiber.Map{
|
||||
"error": "Provided data is invalid",
|
||||
})
|
||||
}
|
||||
|
||||
func ErrNotFound(c *fiber.Ctx) error {
|
||||
return JSON(c, http.StatusNotFound, fiber.Map{
|
||||
"error": "Endpoint not found",
|
||||
})
|
||||
}
|
||||
|
||||
func ErrBadJSON(c *fiber.Ctx) error {
|
||||
return JSON(c, http.StatusBadRequest, fiber.Map{
|
||||
"error": "Invalid JSON data",
|
||||
})
|
||||
}
|
||||
|
||||
func ErrAuth(c *fiber.Ctx) error {
|
||||
return JSON(c, http.StatusUnauthorized, fiber.Map{
|
||||
"error": "Authentication failed",
|
||||
})
|
||||
}
|
67
api/util/util.go
Normal file
67
api/util/util.go
Normal file
@ -0,0 +1,67 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Render(file string, data interface{}) ([]byte, error) {
|
||||
var (
|
||||
rendered *bytes.Buffer
|
||||
tmpl *template.Template
|
||||
content []byte
|
||||
err error
|
||||
)
|
||||
|
||||
if content, err = os.ReadFile(file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tmpl, err = template.New("template").Parse(string(content)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rendered = bytes.NewBuffer(nil)
|
||||
err = tmpl.Execute(rendered, data)
|
||||
|
||||
return rendered.Bytes(), err
|
||||
}
|
||||
|
||||
func GetDuration(d string) (time.Duration, error) {
|
||||
var (
|
||||
d_num uint64
|
||||
err error
|
||||
)
|
||||
|
||||
d_num_end := d[len(d)-1]
|
||||
d_num_str := strings.TrimSuffix(d, string(d_num_end))
|
||||
|
||||
if d_num, err = strconv.ParseUint(d_num_str, 10, 64); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
switch d_num_end {
|
||||
case 's':
|
||||
return time.Duration(d_num) * (time.Second), nil
|
||||
|
||||
case 'm':
|
||||
return time.Duration(d_num) * (time.Second * 60), nil
|
||||
|
||||
case 'h':
|
||||
return time.Duration(d_num) * ((time.Second * 60) * 60), nil
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("invalid time duration format")
|
||||
}
|
||||
|
||||
func GetSHA1(s string) string {
|
||||
hasher := sha1.New()
|
||||
return hex.EncodeToString(hasher.Sum([]byte(s)))
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"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, " ", ""))
|
||||
}
|
||||
|
||||
func CreateToken() string {
|
||||
s := make([]byte, 32)
|
||||
for i := 0; i < 32; i++ {
|
||||
s[i] = byte(65 + rand.Intn(25))
|
||||
}
|
||||
return string(s)
|
||||
}
|
||||
|
||||
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"))
|
||||
}
|
||||
|
||||
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"))
|
||||
}
|
||||
|
||||
func ErrBadJSON(c *fiber.Ctx) error {
|
||||
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"))
|
||||
}
|
||||
|
||||
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(""))
|
||||
}
|
Reference in New Issue
Block a user