summaryrefslogtreecommitdiff
path: root/nexus/utils/meta
diff options
context:
space:
mode:
Diffstat (limited to 'nexus/utils/meta')
-rw-r--r--nexus/utils/meta/account.go15
-rw-r--r--nexus/utils/meta/body.go11
-rw-r--r--nexus/utils/meta/defaults.go8
-rw-r--r--nexus/utils/meta/messages.go5
-rw-r--r--nexus/utils/meta/request.go108
-rw-r--r--nexus/utils/meta/session.go16
-rw-r--r--nexus/utils/meta/title.go7
7 files changed, 170 insertions, 0 deletions
diff --git a/nexus/utils/meta/account.go b/nexus/utils/meta/account.go
new file mode 100644
index 0000000..a881b36
--- /dev/null
+++ b/nexus/utils/meta/account.go
@@ -0,0 +1,15 @@
+package meta
+
+import (
+ "nexus/models"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Account(context *fiber.Ctx) *models.Account {
+ account, ok := context.Locals(AccountKey).(*models.Account)
+ if !ok {
+ return nil
+ }
+ return account
+}
diff --git a/nexus/utils/meta/body.go b/nexus/utils/meta/body.go
new file mode 100644
index 0000000..bc63546
--- /dev/null
+++ b/nexus/utils/meta/body.go
@@ -0,0 +1,11 @@
+package meta
+
+import "github.com/gofiber/fiber/v2"
+
+func Body[T any](context *fiber.Ctx) (T, error) {
+ var body T
+ if err := context.BodyParser(&body); err != nil {
+ return body, err
+ }
+ return body, nil
+}
diff --git a/nexus/utils/meta/defaults.go b/nexus/utils/meta/defaults.go
new file mode 100644
index 0000000..8f0e27a
--- /dev/null
+++ b/nexus/utils/meta/defaults.go
@@ -0,0 +1,8 @@
+package meta
+
+const (
+ LogPrefix = "Meta"
+ RequestKey = "Request"
+ AccountKey = "Account"
+ SessionKey = "Session"
+)
diff --git a/nexus/utils/meta/messages.go b/nexus/utils/meta/messages.go
new file mode 100644
index 0000000..a188b46
--- /dev/null
+++ b/nexus/utils/meta/messages.go
@@ -0,0 +1,5 @@
+package meta
+
+const (
+ RequestContextMissing = "Request context missing in fiber locals."
+)
diff --git a/nexus/utils/meta/request.go b/nexus/utils/meta/request.go
new file mode 100644
index 0000000..e9c649d
--- /dev/null
+++ b/nexus/utils/meta/request.go
@@ -0,0 +1,108 @@
+package meta
+
+import (
+ "nexus/utils/logger"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+type Param struct {
+ Key string
+ Value string
+}
+
+type RequestInfo struct {
+ Path string
+ Method string
+ Query []Param
+ Params []Param
+ Headers []Param
+ QueryString string
+ IP string
+ URL string
+}
+
+type RequestData struct {
+ RequestInfo
+ Context *fiber.Ctx
+}
+
+func BuildRequest(context *fiber.Ctx) RequestInfo {
+ return RequestInfo{
+ Path: context.Path(),
+ Method: context.Method(),
+ Query: buildQueryParams(context),
+ Params: buildRouteParams(context),
+ Headers: buildHeaders(context),
+ QueryString: string(context.Request().URI().QueryString()),
+ IP: context.IP(),
+ URL: context.OriginalURL(),
+ }
+}
+
+func Request(context *fiber.Ctx) *RequestData {
+ data, ok := context.Locals(RequestKey).(RequestInfo)
+ if !ok {
+ logger.Errorf(LogPrefix, RequestContextMissing)
+ return nil
+ }
+
+ return &RequestData{
+ RequestInfo: data,
+ Context: context,
+ }
+}
+
+func (self *RequestData) Param(key string) string {
+ if self == nil || self.Context == nil {
+ return ""
+ }
+ return self.Context.Params(key)
+}
+
+func (self *RequestData) Query(key string) string {
+ if self == nil {
+ return ""
+ }
+ return findParam(self.RequestInfo.Query, key)
+}
+
+func (self *RequestData) Header(key string) string {
+ if self == nil {
+ return ""
+ }
+ return findParam(self.RequestInfo.Headers, key)
+}
+
+func buildQueryParams(context *fiber.Ctx) []Param {
+ params := make([]Param, 0)
+ context.Request().URI().QueryArgs().VisitAll(func(name []byte, value []byte) {
+ params = append(params, Param{Key: string(name), Value: string(value)})
+ })
+ return params
+}
+
+func buildRouteParams(context *fiber.Ctx) []Param {
+ params := make([]Param, 0)
+ for name, value := range context.AllParams() {
+ params = append(params, Param{Key: name, Value: value})
+ }
+ return params
+}
+
+func buildHeaders(context *fiber.Ctx) []Param {
+ params := make([]Param, 0)
+ context.Request().Header.VisitAll(func(name []byte, value []byte) {
+ params = append(params, Param{Key: string(name), Value: string(value)})
+ })
+ return params
+}
+
+func findParam(params []Param, key string) string {
+ for _, param := range params {
+ if param.Key == key {
+ return param.Value
+ }
+ }
+ return ""
+}
diff --git a/nexus/utils/meta/session.go b/nexus/utils/meta/session.go
new file mode 100644
index 0000000..ccc7adf
--- /dev/null
+++ b/nexus/utils/meta/session.go
@@ -0,0 +1,16 @@
+package meta
+
+import (
+ "nexus/sessions"
+
+ "github.com/gofiber/fiber/v2"
+ "github.com/gofiber/fiber/v2/middleware/session"
+)
+
+func Session(context *fiber.Ctx) *session.Session {
+ sess, ok := context.Locals(sessions.SessionLocalKey).(*session.Session)
+ if !ok {
+ return nil
+ }
+ return sess
+}
diff --git a/nexus/utils/meta/title.go b/nexus/utils/meta/title.go
new file mode 100644
index 0000000..3e6506f
--- /dev/null
+++ b/nexus/utils/meta/title.go
@@ -0,0 +1,7 @@
+package meta
+
+import "github.com/gofiber/fiber/v2"
+
+func SetPageTitle(context *fiber.Ctx, title string) {
+ context.Locals("Title", title)
+}