lets-go:9.2 session storage

This commit is contained in:
tamsin johnson 2024-02-06 21:06:48 -08:00
parent 4e40ef8ed9
commit 5ad766f20c
5 changed files with 29 additions and 4 deletions

View File

@ -8,9 +8,12 @@ import (
"net/http" "net/http"
"regexp" "regexp"
"os" "os"
"time"
"snippetbox.chaosfem.tw/internal/models" "snippetbox.chaosfem.tw/internal/models"
"github.com/alexedwards/scs/mysqlstore"
"github.com/alexedwards/scs/v2"
"github.com/go-playground/form/v4" "github.com/go-playground/form/v4"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -21,6 +24,7 @@ type application struct {
snippets *models.SnippetModel snippets *models.SnippetModel
templateCache map[string]*template.Template templateCache map[string]*template.Template
formDecoder *form.Decoder formDecoder *form.Decoder
sessionManager *scs.SessionManager
} }
// main it's the snippetbox webapp // main it's the snippetbox webapp
@ -50,12 +54,17 @@ func main() {
formDecoder := form.NewDecoder() formDecoder := form.NewDecoder()
sessionManager := scs.New()
sessionManager.Store = mysqlstore.New(db)
sessionManager.Lifetime = 12 * time.Hour
// setup the application // setup the application
app := &application{ app := &application{
logger: logger, logger: logger,
snippets: &models.SnippetModel{DB: db}, snippets: &models.SnippetModel{DB: db},
templateCache: templateCache, templateCache: templateCache,
formDecoder: formDecoder, formDecoder: formDecoder,
sessionManager: sessionManager,
} }
logger.Info("starting server", slog.String("addr", *addr)) logger.Info("starting server", slog.String("addr", *addr))

View File

@ -23,10 +23,12 @@ func (app *application) routes() http.Handler {
fileServer := http.FileServer(http.Dir("./ui/static")) fileServer := http.FileServer(http.Dir("./ui/static"))
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer)) router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
router.HandlerFunc(http.MethodGet, "/", app.home) dynamic := alice.New(app.sessionManager.LoadAndSave)
router.HandlerFunc(http.MethodGet, "/snippet/view/:id", app.snippetView)
router.HandlerFunc(http.MethodGet, "/snippet/create", app.snippetCreate) router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
router.HandlerFunc(http.MethodPost, "/snippet/create", app.snippetCreatePost) 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) standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders)

View File

@ -12,3 +12,11 @@ CREATE USER 'web';
GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO 'web' IDENTIFIED BY 'dbpass'; GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO 'web' IDENTIFIED BY 'dbpass';
CREATE INDEX idx_snippets_created ON snippets(created); 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);

View File

@ -3,6 +3,8 @@ module snippetbox.chaosfem.tw
go 1.21.4 go 1.21.4
require ( 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-playground/form/v4 v4.2.1 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect

View File

@ -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/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 h1:HjdRDKO0fftVMU5epjPW2SOREcZ6/wLUzEobqUGJuPw=
github.com/go-playground/form/v4 v4.2.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U= github.com/go-playground/form/v4 v4.2.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U=