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. --- cafe/main.go | 7 +++++++ config/env.go | 10 ++++++---- processors/metadata.go | 17 ++++++++++++++++ processors/processors.go | 8 ++++++++ processors/request.go | 12 ++++++++++++ templates/pages/home.django | 7 ++++--- types/http.go | 15 +++++++++++++++ utils/meta/request.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ utils/meta/title.go | 13 +++++++++++++ 9 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 processors/metadata.go create mode 100644 processors/processors.go create mode 100644 processors/request.go create mode 100644 utils/meta/request.go create mode 100644 utils/meta/title.go diff --git a/cafe/main.go b/cafe/main.go index a249587..716b5ef 100644 --- a/cafe/main.go +++ b/cafe/main.go @@ -2,8 +2,10 @@ package main import ( "cafe/config" + "cafe/processors" "cafe/router" "cafe/tags" + "cafe/utils/env" "fmt" "log" @@ -16,6 +18,10 @@ import ( ) func main() { + if config.Server.AppSecret == env.Defaults(&config.Server).AppSecret { + log.Println("Warning: AppSecret is set to a default value which is not secure. Please set a strong random secret in your APP_SECRET environment variable or .env file.") + } + tags.Initialize() engine := django.New("./templates", ".django") engine.Reload(config.Server.DevMode) @@ -32,6 +38,7 @@ func main() { })) app.Use(cors.New()) + processors.Initialize(app) router.Initialize(app) address := fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port) diff --git a/config/env.go b/config/env.go index 4e6b50f..dd9eb70 100644 --- a/config/env.go +++ b/config/env.go @@ -1,10 +1,12 @@ package config type server struct { - Host string `env:"SERVER_HOST" default:"localhost"` - Port int `env:"SERVER_PORT" default:"8080"` - AppSecret string `env:"APP_SECRET" default:"mysecret"` - DevMode bool `env:"DEV_MODE" default:"true"` + Host string `env:"SERVER_HOST" default:"localhost"` + Port int `env:"SERVER_PORT" default:"8080"` + AppSecret string `env:"APP_SECRET" default:"mysecret"` + AppName string `env:"APP_NAME" default:"Shifoo's Cafe"` + AppDescription string `env:"APP_DESCRIPTION" default:"A cozy place for close friends"` + DevMode bool `env:"DEV_MODE" default:"true"` } type database struct { diff --git a/processors/metadata.go b/processors/metadata.go new file mode 100644 index 0000000..9f2fbfb --- /dev/null +++ b/processors/metadata.go @@ -0,0 +1,17 @@ +package processors + +import ( + "cafe/config" + + "github.com/gofiber/fiber/v2" +) + +const defaultTitle = "Shifoo's Cafe" + +func metadata(ctx *fiber.Ctx) error { + ctx.Locals("Title", defaultTitle) + ctx.Locals("AppName", config.Server.AppName) + ctx.Locals("AppDescription", config.Server.AppDescription) + + return ctx.Next() +} diff --git a/processors/processors.go b/processors/processors.go new file mode 100644 index 0000000..d56cbde --- /dev/null +++ b/processors/processors.go @@ -0,0 +1,8 @@ +package processors + +import "github.com/gofiber/fiber/v2" + +func Initialize(app *fiber.App) { + app.Use(metadata) + app.Use(request) +} diff --git a/processors/request.go b/processors/request.go new file mode 100644 index 0000000..f63a3dc --- /dev/null +++ b/processors/request.go @@ -0,0 +1,12 @@ +package processors + +import ( + "cafe/utils/meta" + + "github.com/gofiber/fiber/v2" +) + +func request(ctx *fiber.Ctx) error { + ctx.Locals("Request", meta.BuildRequest(ctx)) + return ctx.Next() +} diff --git a/templates/pages/home.django b/templates/pages/home.django index eef2f81..7ceeae7 100644 --- a/templates/pages/home.django +++ b/templates/pages/home.django @@ -1,12 +1,13 @@ {% extends "layouts/base.django" %} -{% block title %}Home - Shifoo's Cafe{% endblock %} +{% block title %}{{ Title }}{% endblock %} {% block content %}
-

Welcome to Shifoo's Cafe

-

A cozy place for close friends

+

{{ AppName }}

+

{{ AppDescription }}

+

Request: {{ Request.Method }} {{ Request.Path }}

{% endblock %} \ No newline at end of file diff --git a/types/http.go b/types/http.go index bc85d99..5fac0e8 100644 --- a/types/http.go +++ b/types/http.go @@ -11,3 +11,18 @@ const ( OPTIONS HTTPMethod = "OPTIONS" HEAD HTTPMethod = "HEAD" ) + +type HTTPQueryParam struct { + Key string + Value string +} + +type HTTPRequest struct { + Path string + Method string + Query []HTTPQueryParam + Params []HTTPQueryParam + QueryString string + IP string + URL string +} 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