diff options
| author | Bobby <[email protected]> | 2026-01-20 14:19:25 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-01-20 14:19:25 +0530 |
| commit | a04faab2a7c031b95e6e7553f9921c3bada2bc08 (patch) | |
| tree | 91c77399eac06aeb7417ed367123321f223bcfd7 /utils | |
| parent | 97459ee6173299aa352ac29b2a77dd8df7e3ab3d (diff) | |
| download | cafe-a04faab2a7c031b95e6e7553f9921c3bada2bc08.tar.xz cafe-a04faab2a7c031b95e6e7553f9921c3bada2bc08.zip | |
Implemented processors and add HTTPRequest and HTTPQueryParam types and Created utility functions for building request metadata.
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/meta/request.go | 47 | ||||
| -rw-r--r-- | utils/meta/title.go | 13 |
2 files changed, 60 insertions, 0 deletions
diff --git a/utils/meta/request.go b/utils/meta/request.go new file mode 100644 index 0000000..9d289e0 --- /dev/null +++ b/utils/meta/request.go @@ -0,0 +1,47 @@ +package meta + +import ( + "cafe/types" + + "github.com/gofiber/fiber/v2" +) + +func BuildRequest(context *fiber.Ctx) types.HTTPRequest { + return types.HTTPRequest{ + Path: context.Path(), + Method: context.Method(), + Query: buildQueryParams(context), + Params: buildRouteParams(context), + QueryString: string(context.Request().URI().QueryString()), + IP: context.IP(), + URL: context.OriginalURL(), + } +} + +func buildQueryParams(context *fiber.Ctx) []types.HTTPQueryParam { + params := make([]types.HTTPQueryParam, 0) + args := context.Request().URI().QueryArgs() + + args.VisitAll(transformQueryParam(¶ms)) + return params +} + +func buildRouteParams(context *fiber.Ctx) []types.HTTPQueryParam { + params := make([]types.HTTPQueryParam, 0) + for key, value := range context.AllParams() { + params = append(params, types.HTTPQueryParam{ + Key: key, + Value: value, + }) + } + return params +} + +func transformQueryParam(params *[]types.HTTPQueryParam) func(key, value []byte) { + return func(key, value []byte) { + *params = append(*params, types.HTTPQueryParam{ + Key: string(key), + Value: string(value), + }) + } +} diff --git a/utils/meta/title.go b/utils/meta/title.go new file mode 100644 index 0000000..1de7d70 --- /dev/null +++ b/utils/meta/title.go @@ -0,0 +1,13 @@ +package meta + +import ( + "cafe/config" + "fmt" + + "github.com/gofiber/fiber/v2" +) + +func SetPageTitle(context *fiber.Ctx, title string) { + title = fmt.Sprintf("%s | %s", title, config.Server.AppName) + context.Locals("Title", title) +} |
