From 5ad766f20c22b11fcda5bce74a4746afcf970cb5 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Tue, 6 Feb 2024 21:06:48 -0800 Subject: [PATCH] lets-go:9.2 session storage --- snippetbox/cmd/web/main.go | 9 +++++++++ snippetbox/cmd/web/routes.go | 10 ++++++---- snippetbox/db/initdb.d/init.sql | 8 ++++++++ snippetbox/go.mod | 2 ++ snippetbox/go.sum | 4 ++++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/snippetbox/cmd/web/main.go b/snippetbox/cmd/web/main.go index bc5f130..4e5b087 100644 --- a/snippetbox/cmd/web/main.go +++ b/snippetbox/cmd/web/main.go @@ -8,9 +8,12 @@ import ( "net/http" "regexp" "os" + "time" "snippetbox.chaosfem.tw/internal/models" + "github.com/alexedwards/scs/mysqlstore" + "github.com/alexedwards/scs/v2" "github.com/go-playground/form/v4" _ "github.com/go-sql-driver/mysql" ) @@ -21,6 +24,7 @@ type application struct { snippets *models.SnippetModel templateCache map[string]*template.Template formDecoder *form.Decoder + sessionManager *scs.SessionManager } // main it's the snippetbox webapp @@ -50,12 +54,17 @@ func main() { formDecoder := form.NewDecoder() + sessionManager := scs.New() + sessionManager.Store = mysqlstore.New(db) + sessionManager.Lifetime = 12 * time.Hour + // setup the application app := &application{ logger: logger, snippets: &models.SnippetModel{DB: db}, templateCache: templateCache, formDecoder: formDecoder, + sessionManager: sessionManager, } logger.Info("starting server", slog.String("addr", *addr)) diff --git a/snippetbox/cmd/web/routes.go b/snippetbox/cmd/web/routes.go index 16c0b3b..d2cb8c5 100644 --- a/snippetbox/cmd/web/routes.go +++ b/snippetbox/cmd/web/routes.go @@ -23,10 +23,12 @@ func (app *application) routes() http.Handler { fileServer := http.FileServer(http.Dir("./ui/static")) router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer)) - router.HandlerFunc(http.MethodGet, "/", app.home) - router.HandlerFunc(http.MethodGet, "/snippet/view/:id", app.snippetView) - router.HandlerFunc(http.MethodGet, "/snippet/create", app.snippetCreate) - router.HandlerFunc(http.MethodPost, "/snippet/create", app.snippetCreatePost) + dynamic := alice.New(app.sessionManager.LoadAndSave) + + router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home)) + router.Handler(http.MethodGet, "/snippet/view/:id", dynamic.ThenFunc(app.snippetView)) + router.Handler(http.MethodGet, "/snippet/create", dynamic.ThenFunc(app.snippetCreate)) + router.Handler(http.MethodPost, "/snippet/create", dynamic.ThenFunc(app.snippetCreatePost)) standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders) diff --git a/snippetbox/db/initdb.d/init.sql b/snippetbox/db/initdb.d/init.sql index 8f924f7..e7f49e3 100644 --- a/snippetbox/db/initdb.d/init.sql +++ b/snippetbox/db/initdb.d/init.sql @@ -12,3 +12,11 @@ CREATE USER 'web'; GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO 'web' IDENTIFIED BY 'dbpass'; CREATE INDEX idx_snippets_created ON snippets(created); + +CREATE TABLE sessions ( + token CHAR(43) PRIMARY KEY, + data BLOB NOT NULL, + expiry TIMESTAMP(6) NOT NULL +); + +CREATE INDEX sessions_expiry_idx on sessions(expiry); diff --git a/snippetbox/go.mod b/snippetbox/go.mod index 84b9d0d..ee02b0b 100644 --- a/snippetbox/go.mod +++ b/snippetbox/go.mod @@ -3,6 +3,8 @@ module snippetbox.chaosfem.tw go 1.21.4 require ( + github.com/alexedwards/scs/mysqlstore v0.0.0-20240203174419-a38e822451b6 // indirect + github.com/alexedwards/scs/v2 v2.7.0 // indirect github.com/go-playground/form/v4 v4.2.1 // indirect github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect diff --git a/snippetbox/go.sum b/snippetbox/go.sum index a9f8f45..5c3c4b5 100644 --- a/snippetbox/go.sum +++ b/snippetbox/go.sum @@ -1,3 +1,7 @@ +github.com/alexedwards/scs/mysqlstore v0.0.0-20240203174419-a38e822451b6 h1:npjiNTwvsVAwF+ukm1At6RbzCzFAsOInhgZWzaKulkk= +github.com/alexedwards/scs/mysqlstore v0.0.0-20240203174419-a38e822451b6/go.mod h1:p8jK3D80sw1PFrCSdlcJF1O75bp55HqbgDyyCLM0FrE= +github.com/alexedwards/scs/v2 v2.7.0 h1:DY4rqLCM7UIR9iwxFS0++z1NhTzQlKV30aMHkJCDWKw= +github.com/alexedwards/scs/v2 v2.7.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/form/v4 v4.2.1 h1:HjdRDKO0fftVMU5epjPW2SOREcZ6/wLUzEobqUGJuPw= github.com/go-playground/form/v4 v4.2.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U=