diff --git a/app/cfg/cfg.go b/app/cfg/cfg.go index 3ed641a..776d891 100644 --- a/app/cfg/cfg.go +++ b/app/cfg/cfg.go @@ -10,13 +10,6 @@ import ( ) 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 { SecretKey string `yaml:"secretKey" json:"-"` LogLevel string `yaml:"logLevel"` @@ -27,6 +20,21 @@ type configStruct struct { IsProduction bool `yaml:"isProduction"` LoopDelay uint32 `yaml:"loopDelay"` } `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 { Host string `yaml:"host"` Port int `yaml:"port"` @@ -81,6 +89,7 @@ func Init() { maskedCfg := Config maskedCfg.Database.Password = "**password hidden**" + maskedCfg.Redis.Password = "**password hidden**" maskedCfg.SMTP.Password = "**password hidden**" maskedCfg.Application.SecretKey = "**secret key hidden**" diff --git a/app/main.go b/app/main.go index ff92f4c..70d29c8 100644 --- a/app/main.go +++ b/app/main.go @@ -1,16 +1,53 @@ package main import ( + "fmt" "iris-test/app/cfg" "iris-test/app/db" "iris-test/app/logging" "iris-test/app/views" + "time" "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 { + sessions_engine := createSessionEngine() app := iris.New() + app.Use(sessions_engine.Handler()) app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true)) views.CreateRouter(app) return app @@ -22,5 +59,6 @@ func main() { db.InitDB() app := createApp() + defer redisDB.Close() app.Listen(":8000") } diff --git a/config.template.yaml b/config.template.yaml index 497b25d..8a1e085 100644 --- a/config.template.yaml +++ b/config.template.yaml @@ -1,11 +1,3 @@ -# Database credentials -database: - host: "localhost" - port: 5432 - name: "iristest" - username: "iristest" - password: "iristest" - application: secretKey: "secret-key" logLevel: info @@ -14,6 +6,21 @@ application: isProduction: false 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: host: "smtp-host" port: 587 diff --git a/go.mod b/go.mod index 1bc134d..d423183 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,8 @@ require ( github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect github.com/andybalholm/brotli v1.0.6 // 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/flosch/pongo2/v4 v4.0.2 // 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/mailru/easyjson v0.7.7 // 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/russross/blackfriday/v2 v2.1.0 // indirect github.com/schollz/closestmatch v2.1.0+incompatible // indirect diff --git a/go.sum b/go.sum index 67f5d1e..0ce609f 100644 --- a/go.sum +++ b/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/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/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.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= 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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=