Skip to content

Commit

Permalink
Add redis configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkuroki committed Aug 15, 2020
1 parent b032651 commit 20e6167
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
37 changes: 37 additions & 0 deletions application.k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
database:
dialect: postgres
host: dbserver-k8s-service
port: 5432
dbname: testdb
username: testusr
password: testusr
migration: false

redis:
enabled: true
connection_pool_size: 10
host: redis-k8s-service
port: 6379

extension:
master_generator: false
cors_enabled: false
security_enabled: true

log:
format: ${time_rfc3339} [${level}] ${remote_ip} ${method} ${uri} ${status}
level: 2
file_path: ./application.log

security:
exclude_path:
- /api/account/login$
- /api/account/logout$
- /api/health$
user_path:
- /api/account/.*
- /api/master/.*
- /api/book/list
- /api/book/search.*
admin_path:
- /api/.*
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type Config struct {
Password string
Migration bool `default:"false"`
}
Redis struct {
Enabled bool `default:"false"`
ConnectionPoolSize int `yaml:"connection_pool_size" default:"10"`
Host string
Port string
}
Extension struct {
MasterGenerator bool `yaml:"master_generator" default:"false"`
CorsEnabled bool `yaml:"cors_enabled" default:"false"`
Expand Down
2 changes: 1 addition & 1 deletion controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func PostLogin() echo.HandlerFunc {
func PostLogout() echo.HandlerFunc {
return func(c echo.Context) error {
_ = session.SetAccount(c, nil)
_ = session.Save(c)
_ = session.Delete(c)
return c.NoContent(http.StatusOK)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ybkuroki/go-webapp-sample
go 1.14

require (
github.com/garyburd/redigo v1.6.2 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/gorilla/sessions v1.2.0
github.com/jinzhu/configor v1.2.0
Expand All @@ -20,6 +21,7 @@ require (
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
golang.org/x/text v0.3.3 // indirect
gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0
gopkg.in/yaml.v2 v2.2.8 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/garyburd/redigo v1.6.2 h1:yE/pwKCrbLpLpQICzYTeZ7JsTA/C53wFTJHaEtRqniM=
github.com/garyburd/redigo v1.6.2/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
Expand Down Expand Up @@ -174,6 +176,8 @@ golang.org/x/tools v0.0.0-20190608022120-eacb66d2a7c3/go.mod h1:/rFqwRUd4F7ZHNgw
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b h1:U/Uqd1232+wrnHOvWNaxrNqn/kFnr4yu4blgPtQt0N8=
gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b/go.mod h1:fgfIZMlsafAHpspcks2Bul+MWUNw/2dyQmjC2faKjtg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
Expand Down
7 changes: 5 additions & 2 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ func getConnection(config *config.Config) string {

// InitDB initialize a database connection.
func InitDB() {
db, err := gorm.Open(config.GetConfig().Database.Dialect, getConnection(config.GetConfig()))
logger.GetEchoLogger().Info("Try database connection")
conf := config.GetConfig()
db, err := gorm.Open(conf.Database.Dialect, getConnection(conf))
if err != nil {
panic(fmt.Sprintf("[Error]: %s", err))
logger.GetEchoLogger().Error("Failure database connection")
}
logger.GetEchoLogger().Info(fmt.Sprintf("Success database connection, %s:%s", conf.Database.Host, conf.Database.Port))
db.LogMode(true)
db.SetLogger(logger.GetLogger())
rep = &Repository{}
Expand Down
30 changes: 29 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package session

import (
"encoding/json"
"fmt"
"net/http"
"regexp"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/ybkuroki/go-webapp-sample/config"
"github.com/ybkuroki/go-webapp-sample/model"
"gopkg.in/boj/redistore.v1"
)

const (
Expand All @@ -22,7 +24,18 @@ const (
// Init initalize session authentication.
func Init(e *echo.Echo, conf *config.Config) {
if conf.Extension.SecurityEnabled {
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
if conf.Redis.Enabled {
e.Logger.Info("Try redis connection")
address := fmt.Sprintf("%s:%s", conf.Redis.Host, conf.Redis.Port)
store, err := redistore.NewRediStore(conf.Redis.ConnectionPoolSize, "tcp", address, "", []byte("secret"))
if err != nil {
e.Logger.Error("Failure redis connection")
}
e.Use(session.Middleware(store))
e.Logger.Info(fmt.Sprintf("Success redis connection, %s", address))
} else {
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
}
e.Use(AuthenticationMiddleware(conf))
}
}
Expand Down Expand Up @@ -86,6 +99,21 @@ func Save(c echo.Context) error {
Path: "/",
HttpOnly: true,
}
return saveSession(c, sess)
}

// Delete the current session.
func Delete(c echo.Context) error {
sess := Get(c)
sess.Options = &sessions.Options{
Path: "/",
HttpOnly: true,
MaxAge: -1,
}
return saveSession(c, sess)
}

func saveSession(c echo.Context, sess *sessions.Session) error {
if err := sess.Save(c.Request(), c.Response()); err != nil {
return c.NoContent(http.StatusInternalServerError)
}
Expand Down

0 comments on commit 20e6167

Please sign in to comment.