From e7f8e0aad3e935ac5fb9824aee79ad4f377eee2a Mon Sep 17 00:00:00 2001 From: "cryptoanthdm@gmail.com" Date: Mon, 1 Jul 2024 11:18:03 +0200 Subject: [PATCH] added Request to view helpers and changed to Setup() from init() --- bootstrap/app/conf/conf.go | 2 +- bootstrap/app/routes.go | 2 +- bootstrap/cmd/app/main.go | 5 +---- bootstrap/go.mod | 2 +- bootstrap/go.sum | 6 ++---- kit/kit.go | 7 ++++++- kit/middleware/middleware.go | 9 ++++++--- view/view.go | 11 ++++++++++- 8 files changed, 28 insertions(+), 16 deletions(-) diff --git a/bootstrap/app/conf/conf.go b/bootstrap/app/conf/conf.go index bcc5f2b..fcbc794 100644 --- a/bootstrap/app/conf/conf.go +++ b/bootstrap/app/conf/conf.go @@ -1,3 +1,3 @@ package conf -// Application config +// Here goes your application config diff --git a/bootstrap/app/routes.go b/bootstrap/app/routes.go index 2c04036..e6faa2a 100644 --- a/bootstrap/app/routes.go +++ b/bootstrap/app/routes.go @@ -17,7 +17,7 @@ import ( func InitializeMiddleware(router *chi.Mux) { router.Use(chimiddleware.Logger) router.Use(chimiddleware.Recoverer) - router.Use(middleware.WithRequestURL) + router.Use(middleware.WithRequest) } // Define your routes in here diff --git a/bootstrap/cmd/app/main.go b/bootstrap/cmd/app/main.go index 40b33ab..1447c35 100644 --- a/bootstrap/cmd/app/main.go +++ b/bootstrap/cmd/app/main.go @@ -14,10 +14,7 @@ import ( ) func main() { - if err := godotenv.Load(); err != nil { - log.Fatal(err) - } - + kit.Setup() router := chi.NewMux() app.InitializeMiddleware(router) diff --git a/bootstrap/go.mod b/bootstrap/go.mod index 6460c55..9c752c7 100644 --- a/bootstrap/go.mod +++ b/bootstrap/go.mod @@ -6,7 +6,7 @@ go 1.22.4 // replace github.com/anthdm/superkit => ../ require ( - github.com/a-h/templ v0.2.707 + github.com/a-h/templ v0.2.731 github.com/anthdm/superkit v0.0.0-20240622052611-30be5bb82e0d github.com/go-chi/chi/v5 v5.0.14 github.com/golang-jwt/jwt/v5 v5.2.1 diff --git a/bootstrap/go.sum b/bootstrap/go.sum index 9c42565..f9754cf 100644 --- a/bootstrap/go.sum +++ b/bootstrap/go.sum @@ -1,7 +1,5 @@ -github.com/a-h/templ v0.2.707 h1:T1Gkd2ugbRglZ9rYw/VBchWOSZVKmetDbBkm4YubM7U= -github.com/a-h/templ v0.2.707/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8= -github.com/anthdm/superkit v0.0.0-20240622052611-30be5bb82e0d h1:T0qegCdKTBwjk28Rcq81V1vYZl2grFjG9NWhPqIbRm0= -github.com/anthdm/superkit v0.0.0-20240622052611-30be5bb82e0d/go.mod h1:j8+yKABdHVnQ9UqxiE/trbu8CnJuU+gNqlMvfGD6nq4= +github.com/a-h/templ v0.2.731 h1:yiv4C7whSUsa36y65O06DPr/U/j3+WGB0RmvLOoVFXc= +github.com/a-h/templ v0.2.731/go.mod h1:IejA/ecDD0ul0dCvgCwp9t7bUZXVpGClEAdsqZQigi8= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi/v5 v5.0.14 h1:PyEwo2Vudraa0x/Wl6eDRRW2NXBvekgfxyydcM0WGE0= diff --git a/kit/kit.go b/kit/kit.go index c731db7..1f09b0e 100644 --- a/kit/kit.go +++ b/kit/kit.go @@ -4,12 +4,14 @@ import ( "context" "encoding/json" "fmt" + "log" "log/slog" "net/http" "os" "github.com/a-h/templ" "github.com/gorilla/sessions" + "github.com/joho/godotenv" ) var store *sessions.CookieStore @@ -166,7 +168,10 @@ func Env() string { // initialize the store here so the environment variables are // already initialized. Calling NewCookieStore() from outside of // a function scope won't work. -func init() { +func Setup() { + if err := godotenv.Load(); err != nil { + log.Fatal(err) + } appSecret := os.Getenv("SUPERKIT_SECRET") if len(appSecret) < 32 { // For security reasons we are calling os.Exit(1) here so Go's panic recover won't diff --git a/kit/middleware/middleware.go b/kit/middleware/middleware.go index 9cedfdb..e7c1f16 100644 --- a/kit/middleware/middleware.go +++ b/kit/middleware/middleware.go @@ -5,11 +5,14 @@ import ( "net/http" ) -type RequestURLKey struct{} +type ( + RequestKey struct{} + ResponseHeadersKey struct{} +) -func WithRequestURL(next http.Handler) http.Handler { +func WithRequest(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), RequestURLKey{}, r.URL) + ctx := context.WithValue(r.Context(), RequestKey{}, r) next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/view/view.go b/view/view.go index fa202f8..7ba5ee5 100644 --- a/view/view.go +++ b/view/view.go @@ -3,6 +3,7 @@ package view import ( "context" "fmt" + "net/http" "net/url" "github.com/anthdm/superkit/kit" @@ -40,5 +41,13 @@ func Auth(ctx context.Context) kit.Auth { // // view.URL(ctx).Path // => ex. /login func URL(ctx context.Context) *url.URL { - return getContextValue(ctx, middleware.RequestURLKey{}, &url.URL{}) + return getContextValue(ctx, middleware.RequestKey{}, &http.Request{}).URL +} + +// Request is a view helper that returns the current http request. +// The request can be accessed with: +// +// view.Request(ctx) +func Request(ctx context.Context) *http.Request { + return getContextValue(ctx, middleware.RequestKey{}, &http.Request{}) }