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 | |
| 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.
| -rw-r--r-- | cafe/main.go | 7 | ||||
| -rw-r--r-- | config/env.go | 10 | ||||
| -rw-r--r-- | processors/metadata.go | 17 | ||||
| -rw-r--r-- | processors/processors.go | 8 | ||||
| -rw-r--r-- | processors/request.go | 12 | ||||
| -rw-r--r-- | templates/pages/home.django | 7 | ||||
| -rw-r--r-- | types/http.go | 15 | ||||
| -rw-r--r-- | utils/meta/request.go | 47 | ||||
| -rw-r--r-- | utils/meta/title.go | 13 |
9 files changed, 129 insertions, 7 deletions
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 %} <div class="min-h-screen flex items-center justify-center"> <div class="text-center"> - <h1 class="text-4xl font-bold mb-4">Welcome to Shifoo's Cafe</h1> - <p class="text-gray-400">A cozy place for close friends</p> + <h1 class="text-4xl font-bold mb-4">{{ AppName }}</h1> + <p class="text-gray-400">{{ AppDescription }}</p> + <p class="text-sm text-gray-500 mt-4">Request: {{ Request.Method }} {{ Request.Path }}</p> </div> </div> {% 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) +} |
