Captcha redis store (#19)

This commit is contained in:
巴拉迪维 2023-02-22 16:41:22 +08:00 committed by GitHub
parent fe69f8668e
commit d5057b8f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 1 deletions

View File

@ -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 后台默认帐号

View File

@ -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

View File

@ -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

View File

@ -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"
)
@ -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

View File

@ -18,8 +18,13 @@ var (
DatabaseConfig DatabaseConfigInfo
AppConfig AppConfigInfo
RedisConfig RedisConfigInfo
CaptchaConfig CaptchaConfigInfo
)
type CaptchaConfigInfo struct {
Store string
}
// AppConfigInfo 应用配置
type AppConfigInfo struct {
Port int
@ -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
}