Create redis session engine
This commit is contained in:
@ -10,13 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type configStruct struct {
|
type configStruct struct {
|
||||||
Database struct {
|
|
||||||
Host string `yaml:"host"`
|
|
||||||
Port string `yaml:"port"`
|
|
||||||
Name string `yaml:"name"`
|
|
||||||
Username string `yaml:"username"`
|
|
||||||
Password string `yaml:"password" json:"-"`
|
|
||||||
} `yaml:"database"`
|
|
||||||
Application struct {
|
Application struct {
|
||||||
SecretKey string `yaml:"secretKey" json:"-"`
|
SecretKey string `yaml:"secretKey" json:"-"`
|
||||||
LogLevel string `yaml:"logLevel"`
|
LogLevel string `yaml:"logLevel"`
|
||||||
@ -27,6 +20,21 @@ type configStruct struct {
|
|||||||
IsProduction bool `yaml:"isProduction"`
|
IsProduction bool `yaml:"isProduction"`
|
||||||
LoopDelay uint32 `yaml:"loopDelay"`
|
LoopDelay uint32 `yaml:"loopDelay"`
|
||||||
} `yaml:"application"`
|
} `yaml:"application"`
|
||||||
|
Database struct {
|
||||||
|
Host string `yaml:"host"`
|
||||||
|
Port string `yaml:"port"`
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
Password string `yaml:"password" json:"-"`
|
||||||
|
} `yaml:"database"`
|
||||||
|
Redis struct {
|
||||||
|
Host string `yaml:"host"`
|
||||||
|
Port int `yaml:"port"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
Password string `yaml:"password" json:"-"`
|
||||||
|
Database string `yaml:"database"`
|
||||||
|
Prefix string `yaml:"prefix"`
|
||||||
|
} `yaml:"redis"`
|
||||||
SMTP struct {
|
SMTP struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
@ -81,6 +89,7 @@ func Init() {
|
|||||||
|
|
||||||
maskedCfg := Config
|
maskedCfg := Config
|
||||||
maskedCfg.Database.Password = "**password hidden**"
|
maskedCfg.Database.Password = "**password hidden**"
|
||||||
|
maskedCfg.Redis.Password = "**password hidden**"
|
||||||
maskedCfg.SMTP.Password = "**password hidden**"
|
maskedCfg.SMTP.Password = "**password hidden**"
|
||||||
maskedCfg.Application.SecretKey = "**secret key hidden**"
|
maskedCfg.Application.SecretKey = "**secret key hidden**"
|
||||||
|
|
||||||
|
|||||||
38
app/main.go
38
app/main.go
@ -1,16 +1,53 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"iris-test/app/cfg"
|
"iris-test/app/cfg"
|
||||||
"iris-test/app/db"
|
"iris-test/app/db"
|
||||||
"iris-test/app/logging"
|
"iris-test/app/logging"
|
||||||
"iris-test/app/views"
|
"iris-test/app/views"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
|
"github.com/kataras/iris/v12/sessions"
|
||||||
|
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var redisDB *redis.Database
|
||||||
|
|
||||||
|
func createSessionEngine() *sessions.Sessions {
|
||||||
|
redisAddr := fmt.Sprintf("%s:%d", cfg.Config.Redis.Host, cfg.Config.Redis.Port)
|
||||||
|
|
||||||
|
redisDB = redis.New(redis.Config{
|
||||||
|
Network: "tcp",
|
||||||
|
Addr: redisAddr,
|
||||||
|
Timeout: time.Duration(30) * time.Second,
|
||||||
|
MaxActive: 10,
|
||||||
|
Username: cfg.Config.Redis.Username,
|
||||||
|
Password: cfg.Config.Redis.Password,
|
||||||
|
Database: cfg.Config.Redis.Database,
|
||||||
|
Prefix: cfg.Config.Redis.Prefix,
|
||||||
|
Driver: redis.GoRedis(), // defaults to this driver.
|
||||||
|
// To set a custom, existing go-redis client, use the "SetClient" method:
|
||||||
|
// Driver: redis.GoRedis().SetClient(customGoRedisClient)
|
||||||
|
})
|
||||||
|
|
||||||
|
sessions_engine := sessions.New(sessions.Config{
|
||||||
|
Cookie: "_session_id",
|
||||||
|
Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute,
|
||||||
|
AllowReclaim: true,
|
||||||
|
CookieSecureTLS: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
sessions_engine.UseDatabase(redisDB)
|
||||||
|
|
||||||
|
return sessions_engine
|
||||||
|
}
|
||||||
|
|
||||||
func createApp() *iris.Application {
|
func createApp() *iris.Application {
|
||||||
|
sessions_engine := createSessionEngine()
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
app.Use(sessions_engine.Handler())
|
||||||
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
|
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
|
||||||
views.CreateRouter(app)
|
views.CreateRouter(app)
|
||||||
return app
|
return app
|
||||||
@ -22,5 +59,6 @@ func main() {
|
|||||||
db.InitDB()
|
db.InitDB()
|
||||||
|
|
||||||
app := createApp()
|
app := createApp()
|
||||||
|
defer redisDB.Close()
|
||||||
app.Listen(":8000")
|
app.Listen(":8000")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,3 @@
|
|||||||
# Database credentials
|
|
||||||
database:
|
|
||||||
host: "localhost"
|
|
||||||
port: 5432
|
|
||||||
name: "iristest"
|
|
||||||
username: "iristest"
|
|
||||||
password: "iristest"
|
|
||||||
|
|
||||||
application:
|
application:
|
||||||
secretKey: "secret-key"
|
secretKey: "secret-key"
|
||||||
logLevel: info
|
logLevel: info
|
||||||
@ -14,6 +6,21 @@ application:
|
|||||||
isProduction: false
|
isProduction: false
|
||||||
loopDelay: 3000
|
loopDelay: 3000
|
||||||
|
|
||||||
|
database:
|
||||||
|
host: "localhost"
|
||||||
|
port: 5432
|
||||||
|
name: "iristest"
|
||||||
|
username: "iristest"
|
||||||
|
password: "iristest"
|
||||||
|
|
||||||
|
redis:
|
||||||
|
host: "localhost"
|
||||||
|
port: 6379
|
||||||
|
username: "iristest"
|
||||||
|
password: "iristest"
|
||||||
|
database: ""
|
||||||
|
prefix: "myapp-"
|
||||||
|
|
||||||
smtp:
|
smtp:
|
||||||
host: "smtp-host"
|
host: "smtp-host"
|
||||||
port: 587
|
port: 587
|
||||||
|
|||||||
3
go.mod
3
go.mod
@ -19,6 +19,8 @@ require (
|
|||||||
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect
|
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect
|
||||||
github.com/andybalholm/brotli v1.0.6 // indirect
|
github.com/andybalholm/brotli v1.0.6 // indirect
|
||||||
github.com/aymerick/douceur v0.2.0 // indirect
|
github.com/aymerick/douceur v0.2.0 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/fatih/structs v1.1.0 // indirect
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
github.com/flosch/pongo2/v4 v4.0.2 // indirect
|
github.com/flosch/pongo2/v4 v4.0.2 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
@ -41,6 +43,7 @@ require (
|
|||||||
github.com/mailgun/raymond/v2 v2.0.48 // indirect
|
github.com/mailgun/raymond/v2 v2.0.48 // indirect
|
||||||
github.com/mailru/easyjson v0.7.7 // indirect
|
github.com/mailru/easyjson v0.7.7 // indirect
|
||||||
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
|
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
|
||||||
|
github.com/redis/go-redis/v9 v9.2.0 // indirect
|
||||||
github.com/rogpeppe/go-internal v1.11.0 // indirect
|
github.com/rogpeppe/go-internal v1.11.0 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
|
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
|
||||||
|
|||||||
7
go.sum
7
go.sum
@ -16,9 +16,14 @@ github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sx
|
|||||||
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||||
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
|
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
|
||||||
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
|
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
|
||||||
|
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||||
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||||
@ -98,6 +103,8 @@ github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTS
|
|||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/redis/go-redis/v9 v9.2.0 h1:zwMdX0A4eVzse46YN18QhuDiM4uf3JmkOB4VZrdt5uI=
|
||||||
|
github.com/redis/go-redis/v9 v9.2.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||||
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
|
|||||||
Reference in New Issue
Block a user