Skip to content

Commit

Permalink
Merge pull request #62 from alimy/pr-feature
Browse files Browse the repository at this point in the history
support composite feature that server supported just by configure file
  • Loading branch information
rocboss committed Jun 6, 2022
2 parents 6efbf08 + 89dd735 commit 06c11e2
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 124 deletions.
57 changes: 33 additions & 24 deletions config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@ App: # APP基础设置项
DefaultContextTimeout: 60
DefaultPageSize: 10
MaxPageSize: 100
SmsJuheKey:
SmsJuheTplID:
SmsJuheTplVal: "#code#=%d&#m#=%d"
AlipayAppID:
AlipayPrivateKey:
Runtime: # App运行时功能调节
DisablePhoneVerify: False # 禁止绑定手机号码时验证短信验证码,为true时任意验证码都可以通过验证
Server: # 服务设置
RunMode: debug
HttpIp: 0.0.0.0
HttpPort: 8008
ReadTimeout: 60
WriteTimeout: 60
Log: # 日志
LogType: zinc # 可选file或zinc
LogFileSavePath: storage/logs
LogFileName: app
LogFileExt: .log
LogZincHost: http://127.0.0.1:4080/es/_bulk
LogZincIndex: paopao-log
LogZincUser: admin
LogZincPassword: admin
Features:
Default: ["Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LoggerZinc"]
Develop: ["Zinc", "MySQL", "AliOSS", "LoggerFile"]
Slim: ["Zinc", "MySQL", "Redis", "AliOSS", "LoggerFile"]
Sms: "SmsJuhe"
SmsJuhe:
Key:
TplID:
TplVal: "#code#=%d&#m#=%d"
Alipay:
AppID:
PrivateKey:
LoggerFile: # 使用File写日志
SavePath: storage/logs
FileName: app
FileExt: .log
LoggerZinc: # 使用Zinc写日志
Host: http://127.0.0.1:4080/es/_bulk
Index: paopao-log
User: admin
Password: admin
JWT: # 鉴权加密
Secret: 18a6413dc4fe394c66345ebe501b2f26
Issuer: paopao-api
Expand All @@ -36,14 +41,18 @@ Search: # 搜索配置
ZincIndex: paopao-data
ZincUser: admin
ZincPassword: admin
Storage: # 阿里云OSS存储配置
AliossAccessKeyID:
AliossAccessKeySecret:
AliossEndpoint:
AliossBucket:
AliossDomain:
Database: # 数据库
DBType: mysql
Zinc: # Zinc搜索配置
Host: http://127.0.0.1:4080
Index: paopao-data
User: admin
Password: admin
AliOSS: # 阿里云OSS存储配置
AccessKeyID:
AccessKeySecret:
Endpoint:
Bucket:
Domain:
MySQL: # MySQL数据库
Username: root # 填写你的数据库账号
Password: root # 填写你的数据库密码
Host: 127.0.0.1:3306
Expand Down
33 changes: 22 additions & 11 deletions global/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@ import (
)

var (
ServerSetting *setting.ServerSettingS
AppSetting *setting.AppSettingS
RuntimeSetting *setting.RuntimeSettingS
DatabaseSetting *setting.DatabaseSettingS
RedisSetting *setting.RedisSettingS
SearchSetting *setting.SearchSettingS
AliossSetting *setting.AliossSettingS
JWTSetting *setting.JWTSettingS
LoggerSetting *setting.LoggerSettingS
Logger *logrus.Logger
Mutex *sync.Mutex
Features *setting.FeaturesSettingS
ServerSetting *setting.ServerSettingS
AppSetting *setting.AppSettingS
MySQLSetting *setting.MySQLSettingS
RedisSetting *setting.RedisSettingS
SmsJuheSetting *setting.SmsJuheSettings
AlipaySetting *setting.AlipaySettingS
ZincSetting *setting.ZincSettingS
AliOSSSetting *setting.AliOSSSettingS
JWTSetting *setting.JWTSettingS
LoggerFileSetting *setting.LoggerFileSettingS
LoggerZincSetting *setting.LoggerZincSettingS
Logger *logrus.Logger
Mutex *sync.Mutex
)

func Cfg(key string) (string, bool) {
return Features.Cfg(key)
}

func CfgIf(expression string) bool {
return Features.CfgIf(expression)
}
55 changes: 19 additions & 36 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
if err != nil {
log.Fatalf("init.setupDBEngine err: %v", err)
}
client := zinc.NewClient(global.SearchSetting)
client := zinc.NewClient(global.ZincSetting)
service.Initialize(global.DBEngine, client)
}

Expand All @@ -37,40 +37,22 @@ func setupSetting() error {
return err
}

err = setting.ReadSection("Server", &global.ServerSetting)
if err != nil {
return err
}
err = setting.ReadSection("App", &global.AppSetting)
if err != nil {
return err
}
err = setting.ReadSection("Runtime", &global.RuntimeSetting)
if err != nil {
return err
}
err = setting.ReadSection("Log", &global.LoggerSetting)
if err != nil {
return err
}
err = setting.ReadSection("Database", &global.DatabaseSetting)
if err != nil {
return err
}
err = setting.ReadSection("Search", &global.SearchSetting)
if err != nil {
return err
}
err = setting.ReadSection("Redis", &global.RedisSetting)
if err != nil {
return err
}
err = setting.ReadSection("JWT", &global.JWTSetting)
if err != nil {
return err
global.Features = setting.FeaturesFrom("Features")

objects := map[string]interface{}{
"App": &global.AppSetting,
"Server": &global.ServerSetting,
"Alipay": &global.AlipaySetting,
"SmsJuhe": &global.SmsJuheSetting,
"LoggerFile": &global.LoggerFileSetting,
"LoggerZinc": &global.LoggerZincSetting,
"MySQL": &global.MySQLSetting,
"Zinc": &global.ZincSetting,
"Redis": &global.RedisSetting,
"JWT": &global.JWTSetting,
"AliOSS": &global.AliOSSSetting,
}
err = setting.ReadSection("Storage", &global.AliossSetting)
if err != nil {
if err = setting.Unmarshal(objects); err != nil {
return err
}

Expand All @@ -82,7 +64,7 @@ func setupSetting() error {
}

func setupLogger() error {
logger, err := logger.New(global.LoggerSetting)
logger, err := logger.New()
if err != nil {
return err
}
Expand All @@ -91,9 +73,10 @@ func setupLogger() error {
return nil
}

// setupDBEngine 暂时只支持MySQL
func setupDBEngine() error {
var err error
global.DBEngine, err = model.NewDBEngine(global.DatabaseSetting)
global.DBEngine, err = model.NewDBEngine(global.MySQLSetting)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func (d *dataServant) SendPhoneCaptcha(phone string) error {
resp, err := client.R().
SetFormData(map[string]string{
"mobile": phone,
"tpl_id": global.AppSetting.SmsJuheTplID,
"tpl_value": fmt.Sprintf(global.AppSetting.SmsJuheTplVal, captcha, m),
"key": global.AppSetting.SmsJuheKey,
"tpl_id": global.SmsJuheSetting.TplID,
"tpl_value": fmt.Sprintf(global.SmsJuheSetting.TplVal, captcha, m),
"key": global.SmsJuheSetting.Key,
}).Post(gateway)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Model struct {

type ConditionsT map[string]interface{}

func NewDBEngine(databaseSetting *setting.DatabaseSettingS) (*gorm.DB, error) {
func NewDBEngine(databaseSetting *setting.MySQLSettingS) (*gorm.DB, error) {
newLogger := logger.New(
global.Logger, // io writer(日志输出的目标,前缀和日志包含的内容)
logger.Config{
Expand Down
12 changes: 6 additions & 6 deletions internal/routers/api/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ func UploadAttachment(c *gin.Context) {
randomPath := uuid.Must(uuid.NewV4()).String()
ossSavePath := uploadType + "/" + GeneratePath(randomPath[:8]) + "/" + randomPath[9:] + fileExt

client, err := oss.New(global.AliossSetting.AliossEndpoint, global.AliossSetting.AliossAccessKeyID, global.AliossSetting.AliossAccessKeySecret)
client, err := oss.New(global.AliOSSSetting.Endpoint, global.AliOSSSetting.AccessKeyID, global.AliOSSSetting.AccessKeySecret)
if err != nil {
global.Logger.Errorf("oss.New err: %v", err)
response.ToErrorResponse(errcode.FileUploadFailed)
return
}

bucket, err := client.Bucket(global.AliossSetting.AliossBucket)
bucket, err := client.Bucket(global.AliOSSSetting.Bucket)
if err != nil {
global.Logger.Errorf("client.Bucket err: %v", err)
response.ToErrorResponse(errcode.FileUploadFailed)
Expand All @@ -121,7 +121,7 @@ func UploadAttachment(c *gin.Context) {
// 构造附件Model
attachment := &model.Attachment{
FileSize: fileHeader.Size,
Content: "https://" + global.AliossSetting.AliossDomain + "/" + ossSavePath,
Content: "https://" + global.AliOSSSetting.Domain + "/" + ossSavePath,
}

if userID, exists := c.Get("UID"); exists {
Expand Down Expand Up @@ -256,22 +256,22 @@ func DownloadAttachment(c *gin.Context) {
}

// 开始构造下载地址
client, err := oss.New(global.AliossSetting.AliossEndpoint, global.AliossSetting.AliossAccessKeyID, global.AliossSetting.AliossAccessKeySecret)
client, err := oss.New(global.AliOSSSetting.Endpoint, global.AliOSSSetting.AccessKeyID, global.AliOSSSetting.AccessKeySecret)
if err != nil {
global.Logger.Errorf("oss.New err: %v", err)
response.ToErrorResponse(errcode.DownloadReqError)
return
}

bucket, err := client.Bucket(global.AliossSetting.AliossBucket)
bucket, err := client.Bucket(global.AliOSSSetting.Bucket)
if err != nil {
global.Logger.Errorf("client.Bucket err: %v", err)
response.ToErrorResponse(errcode.DownloadReqError)
return
}

// 签名
objectKey := strings.Replace(content.Content, "https://"+global.AliossSetting.AliossDomain+"/", "", -1)
objectKey := strings.Replace(content.Content, "https://"+global.AliOSSSetting.Domain+"/", "", -1)
signedURL, err := bucket.SignURL(objectKey, oss.HTTPGet, 60)
if err != nil {
global.Logger.Errorf("client.SignURL err: %v", err)
Expand Down
17 changes: 7 additions & 10 deletions internal/routers/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func ChangeAvatar(c *gin.Context) {
user = u.(*model.User)
}

if strings.Index(param.Avatar, "https://"+global.AliossSetting.AliossDomain) != 0 {
if strings.Index(param.Avatar, "https://"+global.AliOSSSetting.Domain) != 0 {
response.ToErrorResponse(errcode.InvalidParams)
return
}
Expand Down Expand Up @@ -238,13 +238,10 @@ func BindUserPhone(c *gin.Context) {
return
}

// 验证短信验证码
if !global.RuntimeSetting.DisablePhoneVerify {
if err := service.CheckPhoneCaptcha(param.Phone, param.Captcha); err != nil {
global.Logger.Errorf("service.CheckPhoneCaptcha err: %v\n", err)
response.ToErrorResponse(err)
return
}
if err := service.CheckPhoneCaptcha(param.Phone, param.Captcha); err != nil {
global.Logger.Errorf("service.CheckPhoneCaptcha err: %v\n", err)
response.ToErrorResponse(err)
return
}

// 执行绑定
Expand Down Expand Up @@ -383,7 +380,7 @@ func GetUserRechargeLink(c *gin.Context) {

}

client, err := alipay.New(global.AppSetting.AlipayAppID, global.AppSetting.AlipayPrivateKey, true)
client, err := alipay.New(global.AlipaySetting.AppID, global.AlipaySetting.PrivateKey, true)
// 将 key 的验证调整到初始化阶段
if err != nil {
global.Logger.Errorf("alipay.New err: %v\n", err)
Expand Down Expand Up @@ -462,7 +459,7 @@ func AlipayNotify(c *gin.Context) {
response := app.NewResponse(c)
c.Request.ParseForm()

aliClient, err := alipay.New(global.AppSetting.AlipayAppID, global.AppSetting.AlipayPrivateKey, true)
aliClient, err := alipay.New(global.AlipaySetting.AppID, global.AlipaySetting.PrivateKey, true)
// 将 key 的验证调整到初始化阶段
if err != nil {
global.Logger.Errorf("alipay.New err: %v\n", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq)
for _, item := range param.Contents {
// 检查附件是否是本站资源
if item.Type == model.CONTENT_TYPE_IMAGE || item.Type == model.CONTENT_TYPE_VIDEO || item.Type == model.CONTENT_TYPE_ATTACHMENT {
if strings.Index(item.Content, "https://"+global.AliossSetting.AliossDomain) != 0 {
if strings.Index(item.Content, "https://"+global.AliOSSSetting.Domain) != 0 {
continue
}
}
Expand Down
12 changes: 6 additions & 6 deletions internal/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (p *PostContentItem) Check() error {
// 检查附件是否是本站资源
if p.Type == model.CONTENT_TYPE_IMAGE || p.Type == model.CONTENT_TYPE_VIDEO || p.Type == model.
CONTENT_TYPE_ATTACHMENT {
if strings.Index(p.Content, "https://"+global.AliossSetting.AliossDomain) != 0 {
if strings.Index(p.Content, "https://"+global.AliOSSSetting.Domain) != 0 {
return fmt.Errorf("附件非本站资源")
}
}
Expand Down Expand Up @@ -377,7 +377,7 @@ func GetPostCount(conditions *model.ConditionsT) (int64, error) {
}

func GetPostListFromSearch(q *core.QueryT, offset, limit int) ([]*model.PostFormated, int64, error) {
queryResult, err := ds.QueryAll(q, global.SearchSetting.ZincIndex, offset, limit)
queryResult, err := ds.QueryAll(q, global.ZincSetting.Index, offset, limit)
if err != nil {
return nil, 0, err
}
Expand All @@ -391,7 +391,7 @@ func GetPostListFromSearch(q *core.QueryT, offset, limit int) ([]*model.PostForm
}

func GetPostListFromSearchByQuery(query string, offset, limit int) ([]*model.PostFormated, int64, error) {
queryResult, err := ds.QuerySearch(global.SearchSetting.ZincIndex, query, offset, limit)
queryResult, err := ds.QuerySearch(global.ZincSetting.Index, query, offset, limit)
if err != nil {
return nil, 0, err
}
Expand All @@ -405,7 +405,7 @@ func GetPostListFromSearchByQuery(query string, offset, limit int) ([]*model.Pos
}

func PushPostToSearch(post *model.Post) {
indexName := global.SearchSetting.ZincIndex
indexName := global.ZincSetting.Index

postFormated := post.Format()
postFormated.User = &model.UserFormated{
Expand Down Expand Up @@ -456,7 +456,7 @@ func PushPostToSearch(post *model.Post) {
}

func DeleteSearchPost(post *model.Post) error {
indexName := global.SearchSetting.ZincIndex
indexName := global.ZincSetting.Index

return ds.DelDoc(indexName, fmt.Sprintf("%d", post.ID))
}
Expand All @@ -469,7 +469,7 @@ func PushPostsToSearch(c *gin.Context) {
pages := math.Ceil(float64(totalRows) / float64(splitNum))
nums := int(pages)

indexName := global.SearchSetting.ZincIndex
indexName := global.ZincSetting.Index
// 创建索引
ds.CreateSearchIndex(indexName)

Expand Down
Loading

0 comments on commit 06c11e2

Please sign in to comment.