diff options
| author | Bobby <[email protected]> | 2025-12-19 18:01:24 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-12-19 18:01:24 +0530 |
| commit | b1bfec1ce2987d9fe0cc52e5ae9115977fdf8c24 (patch) | |
| tree | 7080b7dc97522ffe0837a1e0b2965489d7e67664 /utils/shortcuts | |
| parent | 767297e28d47ee9cf3722054e41caa837f0e68d2 (diff) | |
| download | lain-b1bfec1ce2987d9fe0cc52e5ae9115977fdf8c24.tar.xz lain-b1bfec1ce2987d9fe0cc52e5ae9115977fdf8c24.zip | |
added utils, templates, routes, types, middleware, processors and a whole lot of things for a basic login page
Diffstat (limited to 'utils/shortcuts')
| -rw-r--r-- | utils/shortcuts/helpers.go | 50 | ||||
| -rw-r--r-- | utils/shortcuts/redirect.go | 23 | ||||
| -rw-r--r-- | utils/shortcuts/render.go | 38 |
3 files changed, 111 insertions, 0 deletions
diff --git a/utils/shortcuts/helpers.go b/utils/shortcuts/helpers.go new file mode 100644 index 0000000..8503a2d --- /dev/null +++ b/utils/shortcuts/helpers.go @@ -0,0 +1,50 @@ +package shortcuts + +import ( + "fmt" + "reflect" + "strings" +) + +func structValue(data any) (reflect.Value, error) { + v := reflect.ValueOf(data) + if v.Kind() == reflect.Pointer { + v = v.Elem() + } + + if v.Kind() != reflect.Struct { + return reflect.Value{}, fmt.Errorf( + "Render: unsupported bind type %T; must be struct or *struct", + data, + ) + } + + return v, nil +} + +func mapStruct(v reflect.Value) map[string]any { + t := v.Type() + result := make(map[string]any, v.NumField()) + + for i := 0; i < v.NumField(); i++ { + fieldType := t.Field(i) + if !fieldType.IsExported() { + continue + } + + key := fieldType.Name + if tag := fieldType.Tag.Get("json"); tag != "" && tag != "-" { + if idx := strings.IndexByte(tag, ','); idx >= 0 { + if idx > 0 { + key = tag[:idx] + } + } else { + key = tag + } + } + + result[key] = v.Field(i).Interface() + } + + return result +} diff --git a/utils/shortcuts/redirect.go b/utils/shortcuts/redirect.go new file mode 100644 index 0000000..aa760dc --- /dev/null +++ b/utils/shortcuts/redirect.go @@ -0,0 +1,23 @@ +package shortcuts + +import ( + "lain/utils/urls" + + "github.com/gofiber/fiber/v2" +) + +func Redirect(ctx *fiber.Ctx, routeName string) error { + path, ok := urls.GetFullPath(routeName) + if !ok { + return fiber.ErrNotFound + } + return ctx.Redirect(path) +} + +func RedirectWithStatus(ctx *fiber.Ctx, routeName string, statusCode int) error { + path, ok := urls.GetFullPath(routeName) + if !ok { + return fiber.ErrNotFound + } + return ctx.Redirect(path, statusCode) +} diff --git a/utils/shortcuts/render.go b/utils/shortcuts/render.go new file mode 100644 index 0000000..1efeb61 --- /dev/null +++ b/utils/shortcuts/render.go @@ -0,0 +1,38 @@ +package shortcuts + +import ( + "maps" + + "github.com/gofiber/fiber/v2" +) + +func Render(ctx *fiber.Ctx, template string, data any) error { + bind := make(fiber.Map) + + ctx.Context().VisitUserValues(func(key []byte, value any) { + bind[string(key)] = value + }) + + if data != nil { + switch v := data.(type) { + case map[string]any: + maps.Copy(bind, v) + case fiber.Map: + maps.Copy(bind, v) + default: + rv, err := structValue(data) + if err != nil { + return err + } + + maps.Copy(bind, mapStruct(rv)) + } + } + + return ctx.Render(template, bind) +} + +func RenderWithStatus(ctx *fiber.Ctx, template string, data any, statusCode int) error { + ctx.Status(statusCode) + return Render(ctx, template, data) +} |
