added Request to view helpers and changed to Setup() from init()

This commit is contained in:
cryptoanthdm@gmail.com 2024-07-01 11:18:03 +02:00
parent b1ee5f8fa6
commit e7f8e0aad3
8 changed files with 28 additions and 16 deletions

View file

@ -1,3 +1,3 @@
package conf
// Application config
// Here goes your application config

View file

@ -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

View file

@ -14,10 +14,7 @@ import (
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
kit.Setup()
router := chi.NewMux()
app.InitializeMiddleware(router)

View file

@ -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

View file

@ -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=

View file

@ -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

View file

@ -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))
})
}

View file

@ -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{})
}