Compare commits

...

4 commits

Author SHA1 Message Date
cryptoanthdm@gmail.com
e7f8e0aad3 added Request to view helpers and changed to Setup() from init() 2024-07-01 11:18:03 +02:00
cryptoanthdm@gmail.com
b1ee5f8fa6 added license 2024-07-01 09:23:26 +02:00
cryptoanthdm@gmail.com
948574f8b5 moved pkg to root 2024-07-01 09:19:56 +02:00
anthdm
c5ddb2bc18 Added pkg folder for custom packages 2024-07-01 09:07:35 +02:00
12 changed files with 53 additions and 17 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 @anthdm and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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=

1
bootstrap/pkg/.keep Normal file
View file

@ -0,0 +1 @@
Add your packages aka custom libraries here.

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/anthdm/superkit
go 1.22.0
require (
github.com/a-h/templ v0.2.707
github.com/a-h/templ v0.2.731
github.com/gorilla/sessions v1.3.0
github.com/stretchr/testify v1.9.0
)

2
go.sum
View file

@ -1,5 +1,7 @@
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/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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=

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