refactor: 调整项目目录结构,移除其他语言 API 目录

This commit is contained in:
RockYang
2023-07-10 09:42:11 +08:00
parent 22efe81080
commit 9d51a478b9
58 changed files with 0 additions and 15 deletions

97
api/store/leveldb.go Normal file
View File

@@ -0,0 +1,97 @@
package store
import (
"chatplus/store/vo"
"encoding/json"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
)
type LevelDB struct {
driver *leveldb.DB
}
func NewLevelDB() (*LevelDB, error) {
db, err := leveldb.OpenFile("data/leveldb", nil)
if err != nil {
return nil, err
}
return &LevelDB{
driver: db,
}, nil
}
func (db *LevelDB) Put(key string, value interface{}) error {
bytes, err := json.Marshal(value)
if err != nil {
return err
}
return db.driver.Put([]byte(key), bytes, nil)
}
func (db *LevelDB) Get(key string) ([]byte, error) {
bytes, err := db.driver.Get([]byte(key), nil)
if err != nil {
return nil, err
}
return bytes, nil
}
func (db *LevelDB) Search(prefix string) []string {
var items = make([]string, 0)
iter := db.driver.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
defer iter.Release()
for iter.Next() {
items = append(items, string(iter.Value()))
}
return items
}
func (db *LevelDB) SearchPage(prefix string, page int, pageSize int) *vo.Page {
var items = make([]string, 0)
iter := db.driver.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
defer iter.Release()
res := &vo.Page{Page: page, PageSize: pageSize}
// 计算数据总数和总页数
total := 0
for iter.Next() {
total++
}
res.TotalPage = (total + pageSize - 1) / pageSize
res.Total = int64(total)
// 计算目标页码的起始和结束位置
start := (page - 1) * pageSize
if start > total {
return nil
}
end := start + pageSize
if end > total {
end = total
}
// 跳转到目标页码的起始位置
count := 0
for iter.Next() {
if count >= start {
items = append(items, string(iter.Value()))
}
count++
}
iter.Release()
res.Items = items
return res
}
func (db *LevelDB) Delete(key string) error {
return db.driver.Delete([]byte(key), nil)
}
// Close release resources
func (db *LevelDB) Close() error {
return db.driver.Close()
}

View File

@@ -0,0 +1,9 @@
package model
// ApiKey OpenAI API 模型
type ApiKey struct {
BaseModel
UserId uint //用户ID系统添加的用户 ID 为 0
Value string // API Key 的值
LastUsedAt int64 // 最后使用时间
}

9
api/store/model/base.go Normal file
View File

@@ -0,0 +1,9 @@
package model
import "time"
type BaseModel struct {
Id uint `gorm:"primarykey;column:id"`
CreatedAt time.Time
UpdatedAt time.Time
}

View File

@@ -0,0 +1,16 @@
package model
type HistoryMessage struct {
BaseModel
ChatId string // 会话 ID
UserId uint // 用户 ID
RoleId uint // 角色 ID
Type string
Icon string
Tokens int
Content string
}
func (HistoryMessage) TableName() string {
return "chatgpt_chat_history"
}

View File

@@ -0,0 +1,10 @@
package model
type ChatItem struct {
BaseModel
ChatId string `gorm:"column:chat_id;unique"` // 会话 ID
UserId uint // 用户 ID
RoleId uint // 角色 ID
Model string // 会话模型
Title string // 会话标题
}

View File

@@ -0,0 +1,12 @@
package model
type ChatRole struct {
BaseModel
Key string `gorm:"column:marker;unique"` // 角色唯一标识
Name string // 角色名称
Context string `gorm:"column:context_json"` // 角色语料信息 json
HelloMsg string // 打招呼的消息
Icon string // 角色聊天图标
Enable bool // 是否启用被启用
Sort int //排序数字
}

View File

@@ -0,0 +1,7 @@
package model
type Config struct {
Id uint `gorm:"primarykey;column:id"`
Key string `gorm:"column:marker;unique"`
Config string `gorm:"column:config_json"`
}

18
api/store/model/user.go Normal file
View File

@@ -0,0 +1,18 @@
package model
type User struct {
BaseModel
Username string `gorm:"index:username,unique"`
Password string
Nickname string
Avatar string
Salt string // 密码盐
Tokens int64 // 剩余tokens
Calls int // 剩余对话次数
ChatConfig string `gorm:"column:chat_config_json"` // 聊天配置 json
ChatRoles string `gorm:"column:chat_roles_json"` // 聊天角色
ExpiredTime int64 // 账户到期时间
Status bool `gorm:"default:true"` // 当前状态
LastLoginAt int64 // 最后登录时间
LastLoginIp string // 最后登录 IP
}

View File

@@ -0,0 +1,9 @@
package model
type UserLoginLog struct {
BaseModel
UserId uint
Username string
LoginIp string
LoginAddress string
}

36
api/store/mysql.go Normal file
View File

@@ -0,0 +1,36 @@
package store
import (
"chatplus/core/types"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"time"
)
func NewGormConfig() *gorm.Config {
return &gorm.Config{
Logger: logger.Default.LogMode(logger.Warn),
NamingStrategy: schema.NamingStrategy{
TablePrefix: "chatgpt_", // 设置表前缀
SingularTable: false, // 使用单数表名形式
},
}
}
func NewMysql(config *gorm.Config, appConfig *types.AppConfig) (*gorm.DB, error) {
db, err := gorm.Open(mysql.Open(appConfig.MysqlDns), config)
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
sqlDB.SetMaxIdleConns(32)
sqlDB.SetMaxOpenConns(512)
sqlDB.SetConnMaxLifetime(time.Hour)
if err != nil {
return nil, err
}
return db, nil
}

9
api/store/vo/api_key.go Normal file
View File

@@ -0,0 +1,9 @@
package vo
// ApiKey OpenAI API 模型
type ApiKey struct {
BaseVo
UserId uint `json:"user_id"` //用户ID系统添加的用户 ID 为 0
Value string `json:"value"` // API Key 的值
LastUsedAt int64 `json:"last_used_at"` // 最后使用时间
}

7
api/store/vo/base.go Normal file
View File

@@ -0,0 +1,7 @@
package vo
type BaseVo struct {
Id uint `json:"id"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

View File

@@ -0,0 +1,16 @@
package vo
type HistoryMessage struct {
BaseVo
ChatId string `json:"chat_id"`
UserId uint `json:"user_id"`
RoleId uint `json:"role_id"`
Type string `json:"type"`
Icon string `json:"icon"`
Tokens int `json:"tokens"`
Content string `json:"content"`
}
func (HistoryMessage) TableName() string {
return "chatgpt_chat_history"
}

11
api/store/vo/chat_item.go Normal file
View File

@@ -0,0 +1,11 @@
package vo
type ChatItem struct {
BaseVo
UserId uint `json:"user_id"`
Icon string `json:"icon"`
RoleId uint `json:"role_id"`
ChatId string `json:"chat_id"`
Model string `json:"model"`
Title string `json:"title"`
}

14
api/store/vo/chat_role.go Normal file
View File

@@ -0,0 +1,14 @@
package vo
import "chatplus/core/types"
type ChatRole struct {
BaseVo
Key string `json:"key"` // 角色唯一标识
Name string `json:"name"` // 角色名称
Context []types.Message `json:"context"` // 角色语料信息
HelloMsg string `json:"hello_msg"` // 打招呼的消息
Icon string `json:"icon"` // 角色聊天图标
Enable bool `json:"enable"` // 是否启用被启用
Sort int `json:"sort"` // 排序
}

10
api/store/vo/config.go Normal file
View File

@@ -0,0 +1,10 @@
package vo
import "chatplus/core/types"
type Config struct {
Id uint `json:"id"`
Key string `json:"key"`
ChatConfig types.ChatConfig `json:"chat_config"`
SystemConfig types.SystemConfig `json:"system_config"`
}

22
api/store/vo/page.go Normal file
View File

@@ -0,0 +1,22 @@
package vo
import "math"
type Page struct {
Items interface{} `json:"items"`
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int64 `json:"total"`
TotalPage int `json:"total_page"`
}
func NewPage(total int64, page int, pageSize int, items interface{}) Page {
totalPage := math.Ceil(float64(total) / float64(pageSize))
return Page{
Items: items,
Page: page,
PageSize: pageSize,
Total: total,
TotalPage: int(totalPage),
}
}

19
api/store/vo/user.go Normal file
View File

@@ -0,0 +1,19 @@
package vo
import "chatplus/core/types"
type User struct {
BaseVo
Username string `json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Salt string `json:"salt"` // 密码盐
Tokens int64 `json:"tokens"` // 剩余tokens
Calls int `json:"calls"` // 剩余对话次数
ChatConfig types.ChatConfig `json:"chat_config"` // 聊天配置
ChatRoles []string `json:"chat_roles"` // 聊天角色集合
ExpiredTime int64 `json:"expired_time"` // 账户到期时间
Status bool `json:"status"` // 当前状态
LastLoginAt int64 `json:"last_login_at"` // 最后登录时间
LastLoginIp string `json:"last_login_ip"` // 最后登录 IP
}

View File

@@ -0,0 +1,9 @@
package vo
type UserLoginLog struct {
BaseVo
UserId uint `json:"user_id"`
Username string `json:"username"`
LoginIp string `json:"login_ip"`
LoginAddress string `json:"login_address"`
}