Feat: captcha config

This commit is contained in:
HFO4
2019-11-13 17:22:45 +08:00
parent 7d4e212d4e
commit 9660d2f9c1
5 changed files with 69 additions and 20 deletions

View File

@@ -3,6 +3,8 @@ package conf
import (
"cloudreve/pkg/util"
"github.com/go-ini/ini"
"github.com/mojocn/base64Captcha"
"gopkg.in/go-playground/validator.v8"
)
// database 数据库
@@ -15,17 +17,51 @@ type database struct {
TablePrefix string
}
var DatabaseConfig = &database{
Type: "UNSET",
}
// system 系统通用配置
type system struct {
Debug bool
SessionSecret string
}
var SystemConfig = &system{}
// captcha 验证码配置
type captcha struct {
Height int `validate:"gte=0"`
Width int `validate:"gte=0"`
Mode int `validate:"gte=0,lte=3"`
ComplexOfNoiseText int `validate:"gte=0,lte=2"`
ComplexOfNoiseDot int `validate:"gte=0,lte=2"`
IsShowHollowLine bool
IsShowNoiseDot bool
IsShowNoiseText bool
IsShowSlimeLine bool
IsShowSineLine bool
CaptchaLen int `validate:"gte=0"`
}
// DatabaseConfig 数据库配置
var DatabaseConfig = &database{
Type: "UNSET",
}
// SystemConfig 系统公用配置
var SystemConfig = &system{
Debug: false,
}
// CaptchaConfig 验证码配置
var CaptchaConfig = &captcha{
Height: 60,
Width: 240,
Mode: 3,
ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
IsShowHollowLine: false,
IsShowNoiseDot: false,
IsShowNoiseText: false,
IsShowSlimeLine: false,
IsShowSineLine: false,
CaptchaLen: 6,
}
var cfg *ini.File
@@ -42,6 +78,7 @@ func Init(path string) {
sections := map[string]interface{}{
"Database": DatabaseConfig,
"System": SystemConfig,
"Captcha": CaptchaConfig,
}
for sectionName, sectionStruct := range sections {
err = mapSection(sectionName, sectionStruct)
@@ -58,5 +95,13 @@ func mapSection(section string, confStruct interface{}) error {
if err != nil {
return err
}
// 验证合法性
validate := validator.New(&validator.Config{TagName: "validate"})
err = validate.Struct(confStruct)
if err != nil {
return err
}
return nil
}

View File

@@ -25,7 +25,7 @@ type Logger struct {
// Println 打印
func (ll *Logger) Println(msg string) {
fmt.Printf("%s %s", time.Now().Format("2006-01-02 15:04:05 -0700"), msg)
fmt.Printf("%s %s\n", time.Now().Format("2006-01-02 15:04:05 -0700"), msg)
}
// Panic 极端错误