Skip to content

Commit

Permalink
Captcha redis store (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
barats authored Feb 22, 2023
1 parent fe69f86 commit d5057b8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ admin_port = 9092
# 例如:https://t.cn/ 是前缀(不要忘记最后一个/符号)
url_prefix = http://localhost:9091/

# captcha 验证码默认会写入内存,也可以指定存储到 redis
[captcha]
store = memory
# store = redis

# Redis 配置信息
[redis]
host = redis:6379
database = 0
username =
password =
pool_size = 50

# Postgresql 数据库配置信息
[postgres]
host = localhost
port = 5432
user = postgres
password = xxx
database = oh_url_shortener
max_open_conn = 20
max_idle_conn = 5
```

## Admin 后台默认帐号
Expand Down
4 changes: 4 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ port = 9091
admin_port = 9092
url_prefix = http://localhost:9091/

[captcha]
store = memory
# store = redis

[redis]
host = 127.0.0.1:56379
database= 0
Expand Down
4 changes: 4 additions & 0 deletions docker/docker_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ port = 9091
admin_port = 9092
url_prefix = http://localhost:9091/

[captcha]
store = memory
# store = redis

[redis]
host = redis:6379
database = 0
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"ohurlshortener/utils"

"github.com/Masterminds/sprig"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -110,9 +111,14 @@ func initSettings() {
_, err := utils.InitConfig(cmdConfig)
utils.ExitOnError("Config initialization failed.", err)

_, err = storage.InitRedisService()
rs, err := storage.InitRedisService()
utils.ExitOnError("Redis initialization failed.", err)

if strings.EqualFold("redis", strings.ToLower(utils.CaptchaConfig.Store)) {
crs := storage.CaptchaRedisStore{KeyPrefix: "oh_captcha", Expiration: 1 * time.Minute, RedisService: rs}
captcha.SetCustomStore(&crs)
}

_, err = storage.InitDatabaseService()
storage.CallProcedureStatsTop25() // recalculate when ohUrlShortener starts
storage.CallProcedureStatsSum() // recalculate when ohUrlShortener starts
Expand Down
8 changes: 8 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ var (
DatabaseConfig DatabaseConfigInfo
AppConfig AppConfigInfo
RedisConfig RedisConfigInfo
CaptchaConfig CaptchaConfigInfo
)

type CaptchaConfigInfo struct {
Store string
}

// AppConfigInfo 应用配置
type AppConfigInfo struct {
Port int
Expand Down Expand Up @@ -77,5 +82,8 @@ func InitConfig(file string) (*ini.File, error) {
RedisConfig.Database = redisSection.Key("database").MustInt()
RedisConfig.PoolSize = redisSection.Key("pool_size").MustInt()

captchaSection := cfg.Section("captcha")
CaptchaConfig.Store = captchaSection.Key("store").String()

return cfg, err
}

0 comments on commit d5057b8

Please sign in to comment.