Installing and setting up a sessions package

This commit is contained in:
Muyao CHEN 2024-06-28 15:30:00 +02:00
parent 9911144aff
commit 8ec31267c6
6 changed files with 38 additions and 3 deletions

View File

@ -7,13 +7,30 @@ import (
"go-udemy-web-1/pkg/render" "go-udemy-web-1/pkg/render"
"log" "log"
"net/http" "net/http"
"time"
"github.com/alexedwards/scs/v2"
) )
const portNumber = ":8080" const portNumber = ":8080"
var (
app config.AppConfig
session *scs.SessionManager
)
// main is the main application function // main is the main application function
func main() { func main() {
var app config.AppConfig // change this to true when in production
app.InProduction = false
session = scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
session.Cookie.SameSite = http.SameSiteLaxMode
session.Cookie.Secure = app.InProduction
app.Session = session
tc, err := render.CreateTemplateCache() tc, err := render.CreateTemplateCache()
if err != nil { if err != nil {

View File

@ -7,6 +7,7 @@ import (
"github.com/justinas/nosurf" "github.com/justinas/nosurf"
) )
// WriteToConsole writes a log when user hits a page
func WriteToConsole(next http.Handler) http.Handler { func WriteToConsole(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Hit the page %s\n", r.URL.String()) fmt.Printf("Hit the page %s\n", r.URL.String())
@ -15,15 +16,21 @@ func WriteToConsole(next http.Handler) http.Handler {
}) })
} }
// NoSurf adds CSRF protection to all POST requests
func NoSurf(next http.Handler) http.Handler { func NoSurf(next http.Handler) http.Handler {
csrfHandler := nosurf.New(next) csrfHandler := nosurf.New(next)
csrfHandler.SetBaseCookie(http.Cookie{ csrfHandler.SetBaseCookie(http.Cookie{
HttpOnly: true, HttpOnly: true,
Path: "/", Path: "/",
Secure: false, Secure: app.InProduction,
SameSite: http.SameSiteLaxMode, SameSite: http.SameSiteLaxMode,
}) })
return csrfHandler return csrfHandler
} }
// SessionLoad loads and saves the session on every request
func SessionLoad(next http.Handler) http.Handler {
return session.LoadAndSave(next)
}

View File

@ -15,6 +15,7 @@ func routes(app *config.AppConfig) http.Handler {
mux.Use(middleware.Recoverer) mux.Use(middleware.Recoverer)
mux.Use(WriteToConsole) mux.Use(WriteToConsole)
mux.Use(NoSurf) mux.Use(NoSurf)
mux.Use(SessionLoad)
mux.Get("/", handlers.Repo.Home) mux.Get("/", handlers.Repo.Home)
mux.Get("/about", handlers.Repo.About) mux.Get("/about", handlers.Repo.About)

2
go.mod
View File

@ -5,3 +5,5 @@ go 1.21.0
require github.com/go-chi/chi/v5 v5.0.14 require github.com/go-chi/chi/v5 v5.0.14
require github.com/justinas/nosurf v1.1.1 require github.com/justinas/nosurf v1.1.1
require github.com/alexedwards/scs/v2 v2.8.0 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/alexedwards/scs/v2 v2.8.0 h1:h31yUYoycPuL0zt14c0gd+oqxfRwIj6SOjHdKRZxhEw=
github.com/alexedwards/scs/v2 v2.8.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/go-chi/chi/v5 v5.0.14 h1:PyEwo2Vudraa0x/Wl6eDRRW2NXBvekgfxyydcM0WGE0= github.com/go-chi/chi/v5 v5.0.14 h1:PyEwo2Vudraa0x/Wl6eDRRW2NXBvekgfxyydcM0WGE0=
github.com/go-chi/chi/v5 v5.0.14/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/chi/v5 v5.0.14/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/justinas/nosurf v1.1.1 h1:92Aw44hjSK4MxJeMSyDa7jwuI9GR2J/JCQiaKvXXSlk= github.com/justinas/nosurf v1.1.1 h1:92Aw44hjSK4MxJeMSyDa7jwuI9GR2J/JCQiaKvXXSlk=

View File

@ -1,9 +1,15 @@
package config package config
import "html/template" import (
"html/template"
"github.com/alexedwards/scs/v2"
)
// AppConfig holds the application config // AppConfig holds the application config
type AppConfig struct { type AppConfig struct {
TemplateCahce map[string]*template.Template TemplateCahce map[string]*template.Template
UseCache bool UseCache bool
InProduction bool
Session *scs.SessionManager
} }