diff options
| author | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
| commit | 9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch) | |
| tree | da520b923b5e6758d5457b6233dd6671fc640914 /nexus/utils/shortcuts | |
| parent | 65a143a0871c35989b7c7ea6723d39a0585c089e (diff) | |
| download | echoes-of-vaelun-main.tar.xz echoes-of-vaelun-main.zip | |
Diffstat (limited to 'nexus/utils/shortcuts')
| -rw-r--r-- | nexus/utils/shortcuts/errors.go | 20 | ||||
| -rw-r--r-- | nexus/utils/shortcuts/flash.go | 36 | ||||
| -rw-r--r-- | nexus/utils/shortcuts/json.go | 12 | ||||
| -rw-r--r-- | nexus/utils/shortcuts/messages.go | 5 | ||||
| -rw-r--r-- | nexus/utils/shortcuts/redirect.go | 11 | ||||
| -rw-r--r-- | nexus/utils/shortcuts/render.go | 103 | ||||
| -rw-r--r-- | nexus/utils/shortcuts/token.go | 16 |
7 files changed, 203 insertions, 0 deletions
diff --git a/nexus/utils/shortcuts/errors.go b/nexus/utils/shortcuts/errors.go new file mode 100644 index 0000000..cdb2ad2 --- /dev/null +++ b/nexus/utils/shortcuts/errors.go @@ -0,0 +1,20 @@ +package shortcuts
+
+import "github.com/gofiber/fiber/v2"
+
+func RouteError(context *fiber.Ctx, err *fiber.Error) error {
+ if isAPIRequest(context) {
+ return context.Status(err.Code).JSON(fiber.Map{
+ "error": err.Message,
+ })
+ }
+ return RenderWithStatus(context, "errors/error", err, err.Code)
+}
+
+func ServiceError(code int, message string) *fiber.Error {
+ return fiber.NewError(code, message)
+}
+
+func isAPIRequest(context *fiber.Ctx) bool {
+ return len(context.Path()) >= 4 && context.Path()[:4] == "/api"
+}
diff --git a/nexus/utils/shortcuts/flash.go b/nexus/utils/shortcuts/flash.go new file mode 100644 index 0000000..c82b37d --- /dev/null +++ b/nexus/utils/shortcuts/flash.go @@ -0,0 +1,36 @@ +package shortcuts
+
+import (
+ "nexus/sessions"
+ "nexus/utils/collections"
+
+ "github.com/gofiber/fiber/v2"
+ "github.com/gofiber/fiber/v2/middleware/session"
+)
+
+func Flash(context *fiber.Ctx, data collections.Record[string, any]) error {
+ sess, ok := context.Locals(sessions.SessionLocalKey).(*session.Session)
+ if !ok {
+ return nil
+ }
+ return sessions.SetFlash(sess, data)
+}
+
+func ConsumeFlash(context *fiber.Ctx) collections.Record[string, any] {
+ sess, ok := context.Locals(sessions.SessionLocalKey).(*session.Session)
+ if !ok {
+ return nil
+ }
+ data := sessions.GetFlash(sess)
+ if data != nil {
+ _ = sessions.ClearFlash(sess)
+ }
+ return data
+}
+
+func RedirectWithFlash(context *fiber.Ctx, routeName string, data collections.Record[string, any]) error {
+ if err := Flash(context, data); err != nil {
+ return err
+ }
+ return Redirect(context, routeName)
+}
diff --git a/nexus/utils/shortcuts/json.go b/nexus/utils/shortcuts/json.go new file mode 100644 index 0000000..210d1a9 --- /dev/null +++ b/nexus/utils/shortcuts/json.go @@ -0,0 +1,12 @@ +package shortcuts
+
+import "github.com/gofiber/fiber/v2"
+
+func JSON(context *fiber.Ctx, data any) error {
+ return context.JSON(data)
+}
+
+func Created(context *fiber.Ctx, data any) error {
+ context.Status(fiber.StatusCreated)
+ return context.JSON(data)
+}
diff --git a/nexus/utils/shortcuts/messages.go b/nexus/utils/shortcuts/messages.go new file mode 100644 index 0000000..819e536 --- /dev/null +++ b/nexus/utils/shortcuts/messages.go @@ -0,0 +1,5 @@ +package shortcuts
+
+const (
+ UnsupportedBindType = "Unsupported data type for binding. Only struct, collections.Record[string, any] are supported."
+)
diff --git a/nexus/utils/shortcuts/redirect.go b/nexus/utils/shortcuts/redirect.go new file mode 100644 index 0000000..8999b7c --- /dev/null +++ b/nexus/utils/shortcuts/redirect.go @@ -0,0 +1,11 @@ +package shortcuts
+
+import "github.com/gofiber/fiber/v2"
+
+func Redirect(context *fiber.Ctx, path string) error {
+ return context.Redirect(path)
+}
+
+func RedirectWithStatus(context *fiber.Ctx, path string, statusCode int) error {
+ return context.Redirect(path, statusCode)
+}
diff --git a/nexus/utils/shortcuts/render.go b/nexus/utils/shortcuts/render.go new file mode 100644 index 0000000..5f3678f --- /dev/null +++ b/nexus/utils/shortcuts/render.go @@ -0,0 +1,103 @@ +package shortcuts
+
+import (
+ "maps"
+ "nexus/utils/collections"
+ "reflect"
+ "strings"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Render(context *fiber.Ctx, templateName string, data any) error {
+ templateData := make(collections.Record[string, any])
+
+ if flash := ConsumeFlash(context); flash != nil {
+ maps.Copy(templateData, flash)
+ }
+
+ mergeContextValues(context, templateData)
+
+ if data != nil {
+ if mergeError := mergeBindData(templateData, data); mergeError != nil {
+ return mergeError
+ }
+ }
+
+ return context.Render(templateName, templateData)
+}
+
+func RenderWithStatus(context *fiber.Ctx, templateName string, data any, statusCode int) error {
+ context.Status(statusCode)
+ return Render(context, templateName, data)
+}
+
+func NoContent(context *fiber.Ctx) error {
+ return context.SendStatus(fiber.StatusNoContent)
+}
+
+func mergeContextValues(context *fiber.Ctx, target collections.Record[string, any]) {
+ context.Context().VisitUserValuesAll(func(key any, value any) {
+ switch typedKey := key.(type) {
+ case string:
+ if typedKey != "" {
+ target[typedKey] = value
+ }
+ case []byte:
+ if len(typedKey) > 0 {
+ target[string(typedKey)] = value
+ }
+ }
+ })
+}
+
+func mergeBindData(target collections.Record[string, any], data any) error {
+ normalized, err := normalizeToMap(data)
+ if err != nil {
+ return err
+ }
+ maps.Copy(target, normalized)
+ return nil
+}
+
+func normalizeToMap(data any) (collections.Record[string, any], error) {
+ switch v := data.(type) {
+ case collections.Record[string, any]:
+ return v, nil
+ default:
+ return convertStructToMap(data)
+ }
+}
+
+func convertStructToMap(data any) (collections.Record[string, any], error) {
+ v := reflect.ValueOf(data)
+ if v.Kind() == reflect.Pointer {
+ v = v.Elem()
+ }
+ if v.Kind() != reflect.Struct {
+ return nil, fiber.NewError(fiber.StatusInternalServerError, UnsupportedBindType)
+ }
+
+ t := v.Type()
+ result := make(collections.Record[string, any], t.NumField())
+
+ for i := range t.NumField() {
+ field := t.Field(i)
+ if !field.IsExported() {
+ continue
+ }
+
+ key := field.Name
+ if tag := field.Tag.Get("json"); tag != "" && tag != "-" {
+ if idx := strings.IndexByte(tag, ','); idx > 0 {
+ key = tag[:idx]
+ } else if idx < 0 {
+ key = tag
+ }
+ }
+
+ result[key] = v.Field(i).Interface()
+ }
+
+ return result, nil
+}
diff --git a/nexus/utils/shortcuts/token.go b/nexus/utils/shortcuts/token.go new file mode 100644 index 0000000..be9990f --- /dev/null +++ b/nexus/utils/shortcuts/token.go @@ -0,0 +1,16 @@ +package shortcuts
+
+import (
+ "strings"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func BearerToken(context *fiber.Ctx) string {
+ authHeader := context.Get("Authorization")
+ parts := strings.SplitN(authHeader, " ", 2)
+ if len(parts) != 2 || parts[0] != "Bearer" {
+ return ""
+ }
+ return parts[1]
+}
|
