From c9cdc558a63b20aa101361965eadc35d01b6cb44 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 5 Sep 2022 19:36:22 +0200 Subject: [PATCH] store sessions in db --- go.mod | 7 ++++++- go.sum | 6 ++++++ main.go | 22 +++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index aec6967..0fcda4e 100644 --- a/go.mod +++ b/go.mod @@ -9,4 +9,9 @@ require ( golang.org/x/text v0.3.7 ) -require github.com/gorilla/securecookie v1.1.1 // indirect +require ( + git.milar.in/milarin/envvars/v2 v2.0.0 // indirect + github.com/go-sql-driver/mysql v1.6.0 // indirect + github.com/gorilla/securecookie v1.1.1 // indirect + github.com/srinathgs/mysqlstore v0.0.0-20200417050510-9cbb9420fc4c // indirect +) diff --git a/go.sum b/go.sum index 6b3f7a5..06c5ffe 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,16 @@ +git.milar.in/milarin/envvars/v2 v2.0.0 h1:DWRQCWaHqzDD8NGpSgv5tYLuF9A/dVFPAtTvz3oiIqE= +git.milar.in/milarin/envvars/v2 v2.0.0/go.mod h1:HkdEi+gG2lJSmVq547bTlQV4qQ0hO333bE8IrE0B9yY= git.milar.in/milarin/slices v0.0.4 h1:z92jgsKcnLPLfgXkTVCzH2XXesfXzhe0Osx+PkfCHVI= git.milar.in/milarin/slices v0.0.4/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI= github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= +github.com/srinathgs/mysqlstore v0.0.0-20200417050510-9cbb9420fc4c h1:HT6QRF79dL2Ed6HCrX9RufkxFGo7+NPkgYF1Uzvv/js= +github.com/srinathgs/mysqlstore v0.0.0-20200417050510-9cbb9420fc4c/go.mod h1:kt46Hd+lF0rtpeRgOvYSWYJItOAd73EKkIBZFbX7TXs= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= diff --git a/main.go b/main.go index e6c88f1..41cf12e 100644 --- a/main.go +++ b/main.go @@ -14,12 +14,14 @@ import ( "net/url" "os" "strings" + "time" _ "embed" + "git.milar.in/milarin/envvars/v2" "git.milar.in/milarin/slices" "github.com/gorilla/mux" - "github.com/gorilla/sessions" + "github.com/srinathgs/mysqlstore" ) var ( @@ -36,7 +38,20 @@ var ( Templates *template.Template - SessionStore = sessions.NewCookieStore(Must(hex.DecodeString(os.Getenv("SESSION_KEY")))) + SessionStore = Must(mysqlstore.NewMySQLStore( + fmt.Sprintf( + "%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=Local", + envvars.String("DB_USER", ""), + envvars.String("DB_PASS", ""), + envvars.String("DB_HOST", ""), + envvars.Uint16("DB_PORT", 3306), + envvars.String("DB_BASE", "startpage"), + ), + "sessions", + "/", + int((time.Hour * 24 * 365).Seconds()), + Must(hex.DecodeString(os.Getenv("SESSION_KEY"))), + )) ) func main() { @@ -58,9 +73,6 @@ func main() { r.HandleFunc("/search", search) r.PathPrefix("/static/").Handler(http.FileServer(http.FS(StaticFS))) - //http.HandleFunc("/style.css", ProvideFile("static/style.css", "text/css")) - //http.HandleFunc("/customize.js", ProvideFile("static/customize.js", "application/javascript")) - //http.HandleFunc("/icons.ttf", ProvideFile("static/icons.ttf", "font/ttf")) if err := http.ListenAndServe(fmt.Sprintf("%s:%d", *intf, *port), r); err != nil { panic(err) }