From a04faab2a7c031b95e6e7553f9921c3bada2bc08 Mon Sep 17 00:00:00 2001 From: Bobby <30593201+luciferreeves@users.noreply.github.com> Date: Tue, 20 Jan 2026 14:19:25 +0530 Subject: Implemented processors and add HTTPRequest and HTTPQueryParam types and Created utility functions for building request metadata. --- utils/meta/request.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ utils/meta/title.go | 13 +++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 utils/meta/request.go create mode 100644 utils/meta/title.go (limited to 'utils/meta') 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) +} -- cgit v1.2.3