50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"rag-local/common"
|
|
"rag-local/kb/consts"
|
|
"rag-local/kb/dao"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
var SystemConfigService = &systemConfigService{}
|
|
|
|
type systemConfigService struct{}
|
|
|
|
// EnsureAccessToken 每次启动生成新的访问令牌(内存持有,不落库),供登录使用
|
|
func (s *systemConfigService) EnsureAccessToken(ctx context.Context) (string, error) {
|
|
token := common.RandomToken(16)
|
|
common.SetAccessToken(token)
|
|
return token, nil
|
|
}
|
|
|
|
func (s *systemConfigService) Login(ctx context.Context, token string) (string, error) {
|
|
if !common.CheckAccessToken(token) {
|
|
return "", gerror.New("访问令牌错误")
|
|
}
|
|
return common.SignToken("owner", common.AccessTokenFingerprint(), common.TokenExpireSeconds)
|
|
}
|
|
|
|
// GetSettings 读取全局分块默认值(未设置时用内置默认值)
|
|
func (s *systemConfigService) GetSettings(ctx context.Context) (chunkSize, chunkOverlap int, err error) {
|
|
return dao.AppConfig.GetInt(ctx, consts.SettingsKeyChunkSize, consts.DefaultChunkSize),
|
|
dao.AppConfig.GetInt(ctx, consts.SettingsKeyChunkOverlap, consts.DefaultChunkOverlap), nil
|
|
}
|
|
|
|
// SaveSettings 保存全局分块默认值
|
|
func (s *systemConfigService) SaveSettings(ctx context.Context, chunkSize, chunkOverlap int) error {
|
|
if chunkSize < 50 || chunkSize > 5000 {
|
|
return gerror.New("分块大小需在 50~5000 之间")
|
|
}
|
|
if chunkOverlap < 0 || chunkOverlap > 500 {
|
|
return gerror.New("重叠字数需在 0~500 之间")
|
|
}
|
|
if err := dao.AppConfig.SetInt(ctx, consts.SettingsKeyChunkSize, chunkSize); err != nil {
|
|
return err
|
|
}
|
|
return dao.AppConfig.SetInt(ctx, consts.SettingsKeyChunkOverlap, chunkOverlap)
|
|
}
|