Compare commits
46 Commits
feat-db-ma
...
release
| Author | SHA1 | Date |
|---|---|---|
|
|
80352e8601 | |
|
|
99dda61a84 | |
|
|
08c0752803 | |
|
|
ed2b936bf2 | |
|
|
6d7e6c26fc | |
|
|
3d0253523b | |
|
|
76b6ec7d74 | |
|
|
4bc256aba4 | |
|
|
6481b18fcd | |
|
|
63ea13bbde | |
|
|
dda5035c7c | |
|
|
7be4aeb90e | |
|
|
00aa3b5b7b | |
|
|
dae8a96478 | |
|
|
fc0218e754 | |
|
|
770cde41bd | |
|
|
da70405ab3 | |
|
|
c75dd8b95e | |
|
|
1a2661bf41 | |
|
|
106e27f457 | |
|
|
345d120460 | |
|
|
0edba050c3 | |
|
|
d02cceb775 | |
|
|
f334668e4b | |
|
|
b4f721060f | |
|
|
c0b8c7ba63 | |
|
|
2f8d80184b | |
|
|
ff3e032d6d | |
|
|
defbafd9d6 | |
|
|
27c5b3cd3c | |
|
|
5e184badca | |
|
|
b3492e6826 | |
|
|
44b74e468b | |
|
|
4e2f319229 | |
|
|
94e5c784c0 | |
|
|
b1bb1cb734 | |
|
|
e2ebdc0621 | |
|
|
f0e025bec5 | |
|
|
6e5ee4f0e2 | |
|
|
4592681978 | |
|
|
80ab607432 | |
|
|
1ff1347187 | |
|
|
1244feb7de | |
|
|
6c835e371a | |
|
|
1cfb517fc9 | |
|
|
29c05b0af7 |
|
|
@ -0,0 +1,35 @@
|
||||||
|
version: '1.0'
|
||||||
|
name: pipeline-20250906-release
|
||||||
|
displayName: pipeline-20250906-release
|
||||||
|
triggers:
|
||||||
|
trigger: auto
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
precise:
|
||||||
|
- release
|
||||||
|
stages:
|
||||||
|
- name: stage-4863fcf5
|
||||||
|
displayName: 未命名
|
||||||
|
strategy: naturally
|
||||||
|
trigger: auto
|
||||||
|
executor: []
|
||||||
|
steps:
|
||||||
|
- step: build@golang
|
||||||
|
name: build_golang
|
||||||
|
displayName: Golang 构建
|
||||||
|
golangVersion: '1.24'
|
||||||
|
commands:
|
||||||
|
- '# 默认使用goproxy.cn'
|
||||||
|
- export GOPROXY=https://goproxy.cn
|
||||||
|
- '# 输入你的构建命令'
|
||||||
|
- go get
|
||||||
|
- go build
|
||||||
|
artifacts:
|
||||||
|
- name: BUILD_ARTIFACT
|
||||||
|
path:
|
||||||
|
- ./
|
||||||
|
caches:
|
||||||
|
- /go/pkg/mod
|
||||||
|
notify: []
|
||||||
|
strategy:
|
||||||
|
retry: '0'
|
||||||
15
dao/cid.go
15
dao/cid.go
|
|
@ -3,6 +3,7 @@ package dao
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CID struct {
|
type CID struct {
|
||||||
|
|
@ -11,6 +12,8 @@ type CID struct {
|
||||||
Name string `gorm:"column:name"`
|
Name string `gorm:"column:name"`
|
||||||
Url string `gorm:"column:url"`
|
Url string `gorm:"column:url"`
|
||||||
Time int `gorm:"column:time"` // 定时任务,单位秒,大于0表示定时任务
|
Time int `gorm:"column:time"` // 定时任务,单位秒,大于0表示定时任务
|
||||||
|
LastSuccess time.Time `gorm:"column:last_success" json:"last_success"`
|
||||||
|
LastFail time.Time `gorm:"column:last_fail" json:"last_fail"`
|
||||||
Script string `gorm:"column:script"`
|
Script string `gorm:"column:script"`
|
||||||
Token string `gorm:"column:token"` // 用于外部回调
|
Token string `gorm:"column:token"` // 用于外部回调
|
||||||
}
|
}
|
||||||
|
|
@ -54,7 +57,7 @@ func FindCIDByID(id, auth_id int) CID {
|
||||||
// FindCIDByAuthID 查找持续集成、部署
|
// FindCIDByAuthID 查找持续集成、部署
|
||||||
func FindCIDByAuthID(auth_id int) []CID {
|
func FindCIDByAuthID(auth_id int) []CID {
|
||||||
var cids []CID
|
var cids []CID
|
||||||
DB.Where("auth_id = ?", auth_id).Find(&cids)
|
DB.Where("auth_id = ?", auth_id).Order("created_at").Find(&cids)
|
||||||
return cids
|
return cids
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,3 +124,13 @@ func FindCIDByCID(id uint) CID {
|
||||||
DB.Where("id = ? ", id).First(&cid)
|
DB.Where("id = ? ", id).First(&cid)
|
||||||
return cid
|
return cid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateLastSuccessByID(id int, time time.Time) error {
|
||||||
|
res := DB.Where("id = ?", id).Update("last_success", time)
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateLastFailByID(id int, time time.Time) error {
|
||||||
|
res := DB.Where("id = ?", id).Update("last_fail", time)
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
|
||||||
18
dao/db.go
18
dao/db.go
|
|
@ -4,13 +4,18 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DB *gorm.DB
|
var DB *gorm.DB
|
||||||
|
|
||||||
|
var DBMMap = map[uint]*proto.DBValue{}
|
||||||
|
var DBMMapRWMutex = &sync.RWMutex{} //dbm日志
|
||||||
|
|
||||||
func Init() error {
|
func Init() error {
|
||||||
var db *gorm.DB
|
var db *gorm.DB
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -21,17 +26,20 @@ func Init() error {
|
||||||
} else if proto.Config.DB == 1 {
|
} else if proto.Config.DB == 1 {
|
||||||
dsn = proto.Config.PG_DSN
|
dsn = proto.Config.PG_DSN
|
||||||
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||||
|
} else if proto.Config.DB == 2 { //sqlite
|
||||||
|
dsn = proto.Config.SQLITE_FILE
|
||||||
|
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to connect database")
|
panic("failed to connect database")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = db.AutoMigrate(&User{})
|
//err = db.AutoMigrate(&User{})
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
fmt.Println("user table:", err)
|
// fmt.Println("user table:", err)
|
||||||
return err
|
// return err
|
||||||
} // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表
|
//}
|
||||||
err = db.AutoMigrate(&Video{})
|
err = db.AutoMigrate(&Video{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("video table:", err)
|
fmt.Println("video table:", err)
|
||||||
|
|
|
||||||
106
dao/dbm.go
106
dao/dbm.go
|
|
@ -111,6 +111,17 @@ func DeleteDBManageByID(id uint) error {
|
||||||
return res.Error
|
return res.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteDBManageByUserID(id uint) error {
|
||||||
|
var db2 *gorm.DB
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
db2 = DB.Debug()
|
||||||
|
} else {
|
||||||
|
db2 = DB
|
||||||
|
}
|
||||||
|
res := db2.Where("user_id = ?", id).Delete(&proto.DBManage{})
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
func FindDBRunHistoryByID(id uint) (proto.SQLRunHistory, error) {
|
func FindDBRunHistoryByID(id uint) (proto.SQLRunHistory, error) {
|
||||||
var history proto.SQLRunHistory
|
var history proto.SQLRunHistory
|
||||||
var db2 *gorm.DB
|
var db2 *gorm.DB
|
||||||
|
|
@ -125,7 +136,6 @@ func FindDBRunHistoryByID(id uint) (proto.SQLRunHistory, error) {
|
||||||
}
|
}
|
||||||
return history, nil
|
return history, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
|
func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
|
||||||
var histories []proto.SQLRunHistory
|
var histories []proto.SQLRunHistory
|
||||||
var db2 *gorm.DB
|
var db2 *gorm.DB
|
||||||
|
|
@ -141,6 +151,43 @@ func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
|
||||||
return histories, nil
|
return histories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindDBRunHistoryByAuthIDAndDbId(auth_id int, db_id uint) ([]proto.SQLRunHistory, error) {
|
||||||
|
var histories []proto.SQLRunHistory
|
||||||
|
var db2 *gorm.DB
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
db2 = DB.Debug()
|
||||||
|
} else {
|
||||||
|
db2 = DB
|
||||||
|
}
|
||||||
|
res := db2.Where("user_id = ? and db_id = ?", auth_id, db_id).Find(&histories)
|
||||||
|
if res.Error != nil {
|
||||||
|
return nil, res.Error
|
||||||
|
}
|
||||||
|
return histories, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DelSQLRunHistoryByID(id uint) error {
|
||||||
|
var db2 *gorm.DB
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
db2 = DB.Debug()
|
||||||
|
} else {
|
||||||
|
db2 = DB
|
||||||
|
}
|
||||||
|
res := db2.Where("id = ?", id).Delete(&proto.SQLRunHistory{})
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func DelSQLRunHistoryByAuthID(auth_id int) error {
|
||||||
|
var db2 *gorm.DB
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
db2 = DB.Debug()
|
||||||
|
} else {
|
||||||
|
db2 = DB
|
||||||
|
}
|
||||||
|
res := db2.Where("user_id = ?", auth_id).Delete(&proto.SQLRunHistory{})
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
func FindAllSQLRunHistory() ([]proto.SQLRunHistory, error) {
|
func FindAllSQLRunHistory() ([]proto.SQLRunHistory, error) {
|
||||||
var histories []proto.SQLRunHistory
|
var histories []proto.SQLRunHistory
|
||||||
var db2 *gorm.DB
|
var db2 *gorm.DB
|
||||||
|
|
@ -155,3 +202,60 @@ func FindAllSQLRunHistory() ([]proto.SQLRunHistory, error) {
|
||||||
}
|
}
|
||||||
return histories, nil
|
return histories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RunSQLWithOrder(sql string, db_ *gorm.DB) (result proto.SQLResult, err error) {
|
||||||
|
var db2 *gorm.DB
|
||||||
|
// 保留 Debug 模式
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
db2 = db_.Debug()
|
||||||
|
} else {
|
||||||
|
db2 = db_
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行 SQL 并获取底层 Rows 对象
|
||||||
|
rows, err := db2.Raw(sql).Rows()
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
defer rows.Close() // 确保关闭 Rows
|
||||||
|
|
||||||
|
// 获取列名顺序(关键:这里的顺序与 SQL 查询的列顺序一致)
|
||||||
|
columns, err := rows.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
for _, col := range columns {
|
||||||
|
result.Columns = append(result.Columns, proto.SQLResultColumnsValue{Prop: col, Label: col})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历每行数据
|
||||||
|
for rows.Next() {
|
||||||
|
// 准备接收每行数据的容器(按列顺序)
|
||||||
|
values := make([]interface{}, len(columns))
|
||||||
|
valuePtrs := make([]interface{}, len(columns)) // 用于 Scan 的指针切片
|
||||||
|
|
||||||
|
// 为每个列绑定指针(Scan 要求传入指针)
|
||||||
|
for i := range values {
|
||||||
|
valuePtrs[i] = &values[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 扫描当前行数据到指针切片
|
||||||
|
if err2 := rows.Scan(valuePtrs...); err2 != nil {
|
||||||
|
return result, err2
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将当前行数据存入 map(便于按列名访问)
|
||||||
|
rowMap := make(map[string]interface{})
|
||||||
|
for i, col := range columns {
|
||||||
|
rowMap[col] = values[i]
|
||||||
|
}
|
||||||
|
result.Rows = append(result.Rows, rowMap)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查遍历过程中是否有错误
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
20
dao/user.go
20
dao/user.go
|
|
@ -14,15 +14,19 @@ type User struct {
|
||||||
Password string `gorm:"column:password"`
|
Password string `gorm:"column:password"`
|
||||||
Gender string `gorm:"column:gender"`
|
Gender string `gorm:"column:gender"`
|
||||||
Role string `gorm:"column:role"`
|
Role string `gorm:"column:role"`
|
||||||
Redis bool `gorm:"column:redis"`
|
Redis int `gorm:"column:redis"`
|
||||||
Run bool `gorm:"column:run"`
|
Run int `gorm:"column:run"`
|
||||||
Upload bool `gorm:"column:upload"`
|
Upload int `gorm:"column:upload"`
|
||||||
VideoFunc bool `gorm:"column:video_func"` //视频功能
|
VideoFunc int `gorm:"column:video_func"` //视频功能
|
||||||
DeviceFunc bool `gorm:"column:device_func"` //设备功能
|
DeviceFunc int `gorm:"column:device_func"` //设备功能
|
||||||
CIDFunc bool `gorm:"column:cid_func"` //持续集成功能
|
CIDFunc int `gorm:"column:cid_func"` //持续集成功能, 0为无效, -1为false, 1为true
|
||||||
Avatar string `gorm:"column:avatar"`
|
Avatar string `gorm:"column:avatar"`
|
||||||
CreateTime string `gorm:"column:create_time"`
|
PasswordNeedSecondAuth int `gorm:"column:password_need_second_auth" json:"password_need_second_auth"`
|
||||||
UpdateTime string `gorm:"column:update_time"`
|
ThirdPartyNeedSecondAuth int `gorm:"column:third_party_need_second_auth" json:"third_party_need_second_auth"`
|
||||||
|
CodeNeedSecondAuth int `gorm:"column:code_need_second_auth" json:"code_need_second_auth"`
|
||||||
|
AISecondAuth int `json:"ai_second_auth" column:"ai_second_auth"`
|
||||||
|
LoginAddressInfo string `gorm:"column:login_address_info" json:"login_address_info,omitempty"`
|
||||||
|
LoginDeviceInfo string `gorm:"column:login_device_info" json:"login_device_info,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateUser(name, password, email, gender string, age int) uint {
|
func CreateUser(name, password, email, gender string, age int) uint {
|
||||||
|
|
|
||||||
8
go.mod
8
go.mod
|
|
@ -11,7 +11,7 @@ require (
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
gorm.io/driver/mysql v1.5.6
|
gorm.io/driver/mysql v1.5.6
|
||||||
gorm.io/driver/postgres v1.5.9
|
gorm.io/driver/postgres v1.5.9
|
||||||
gorm.io/gorm v1.25.10
|
gorm.io/gorm v1.30.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
@ -39,6 +39,7 @@ require (
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/kr/text v0.2.0 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||||
|
|
@ -48,9 +49,10 @@ require (
|
||||||
golang.org/x/arch v0.8.0 // indirect
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
golang.org/x/crypto v0.23.0 // indirect
|
golang.org/x/crypto v0.23.0 // indirect
|
||||||
golang.org/x/net v0.25.0 // indirect
|
golang.org/x/net v0.25.0 // indirect
|
||||||
golang.org/x/sync v0.1.0 // indirect
|
golang.org/x/sync v0.9.0 // indirect
|
||||||
golang.org/x/sys v0.20.0 // indirect
|
golang.org/x/sys v0.20.0 // indirect
|
||||||
golang.org/x/text v0.15.0 // indirect
|
golang.org/x/text v0.20.0 // indirect
|
||||||
google.golang.org/protobuf v1.34.1 // indirect
|
google.golang.org/protobuf v1.34.1 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
gorm.io/driver/sqlite v1.6.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
10
go.sum
10
go.sum
|
|
@ -71,6 +71,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
|
@ -115,12 +117,16 @@ golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
|
||||||
|
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
|
||||||
|
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||||
|
|
@ -139,8 +145,12 @@ gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
|
||||||
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
|
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
|
||||||
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
|
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
|
||||||
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
|
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
|
||||||
|
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
|
||||||
|
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
|
||||||
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||||
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
|
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
|
||||||
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||||
|
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
|
||||||
|
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
|
||||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||||
|
|
|
||||||
115
handler/cid.go
115
handler/cid.go
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -57,6 +58,27 @@ func SetUpCIDGroup(router *gin.Engine) {
|
||||||
cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情
|
cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情
|
||||||
cidGroup.GET("/callback", CIDCallback)
|
cidGroup.GET("/callback", CIDCallback)
|
||||||
cidGroup.POST("/callback", CIDCallback)
|
cidGroup.POST("/callback", CIDCallback)
|
||||||
|
cidGroup.GET("/running", GetRunningCIDs)
|
||||||
|
}
|
||||||
|
func GetRunningCIDs(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
user_id := int(id.(float64))
|
||||||
|
req_type := c.Query("type") //请求方式 0默认自己, 1为所有(管理员可选)
|
||||||
|
req_type_ := 0
|
||||||
|
if req_type != "" {
|
||||||
|
req_type_, _ = strconv.Atoi(req_type)
|
||||||
|
}
|
||||||
|
resp_data, err := service.GetCIDRunningList(user_id, req_type_)
|
||||||
|
|
||||||
|
var resp proto.GeneralResp
|
||||||
|
if err != nil {
|
||||||
|
resp.Code = proto.InternalServerError
|
||||||
|
resp.Message = err.Error()
|
||||||
|
} else {
|
||||||
|
resp.Code, resp.Message = 0, ""
|
||||||
|
resp.Data = resp_data
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
}
|
}
|
||||||
func RunCID(c *gin.Context) {
|
func RunCID(c *gin.Context) {
|
||||||
var req CIDRunReq
|
var req CIDRunReq
|
||||||
|
|
@ -65,20 +87,19 @@ func RunCID(c *gin.Context) {
|
||||||
//获取权限
|
//获取权限
|
||||||
//user := dao.FindUserByUserID(authID)
|
//user := dao.FindUserByUserID(authID)
|
||||||
user := service.GetUserByIDFromUserCenter(authID)
|
user := service.GetUserByIDFromUserCenter(authID)
|
||||||
if user.Run == false {
|
if user.Run <= 0 {
|
||||||
c.JSON(200, gin.H{"error": "no run Permissions", "code": proto.NoRunPermissions, "message": "no run Permissions"})
|
c.JSON(200, gin.H{"error": "no run Permissions", "code": proto.NoRunPermissions, "message": "no run Permissions"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.ShouldBind(&req); err == nil {
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
// 获取用户ID
|
// 获取用户ID
|
||||||
username, _ := c.Get("username")
|
|
||||||
cid := dao.FindCIDByID(req.ID, authID)
|
cid := dao.FindCIDByID(req.ID, authID)
|
||||||
if cid.ID == 0 {
|
if cid.ID == 0 {
|
||||||
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
go RunShell(username.(string), cid.Url, cid.Script, req.ID, authID)
|
go RunShellCID(cid.Name, cid.Url, cid.Script, req.ID, authID)
|
||||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -185,32 +206,43 @@ func CIDCallback(c *gin.Context) {
|
||||||
// 获取用户ID
|
// 获取用户ID
|
||||||
token := c.Query("token")
|
token := c.Query("token")
|
||||||
cid_id := c.Query("id")
|
cid_id := c.Query("id")
|
||||||
//fmt.Println("token:", token, "cid_id:", cid_id)
|
|
||||||
//将cid转换为int
|
//将cid转换为int
|
||||||
cid, _ := strconv.Atoi(cid_id)
|
cid, _ := strconv.Atoi(cid_id)
|
||||||
|
var req proto.CIDCallBackReq
|
||||||
if token == "" || cid == 0 {
|
var resp proto.GeneralResp
|
||||||
c.JSON(200, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
if err := c.ShouldBindQuery(&req); err != nil {
|
||||||
|
resp.Code, resp.Message = proto.ParameterError, err.Error()
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if req.ID == 0 || req.Token == "" {
|
||||||
|
resp.Code, resp.Message = proto.ParameterError, "token or id is empty"
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
res := dao.FindCIDByIDAndToken(cid, token)
|
res := dao.FindCIDByIDAndToken(cid, token)
|
||||||
if res.ID == 0 {
|
if res.ID == 0 {
|
||||||
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
|
resp.Code, resp.Message = proto.ParameterError, "CID not found by id and token:"+req.Token+", id:"+strconv.Itoa(int(res.ID))
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//user := dao.FindUserByUserID(res.Auth_id)
|
//user := dao.FindUserByUserID(res.Auth_id)
|
||||||
user := service.GetUserByIDFromUserCenter(res.Auth_id)
|
user := service.GetUserByIDFromUserCenter(res.Auth_id)
|
||||||
if user.Run == false {
|
if user.Run <= 0 {
|
||||||
c.JSON(200, gin.H{"error": "no run Permissions", "code": proto.NoRunPermissions, "message": "the user has no run Permissions"})
|
resp.Code, resp.Message = proto.NoRunPermissions, "the user has no run Permissions"
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if res.ID != 0 {
|
if res.ID != 0 {
|
||||||
user := dao.FindUserByID(res.Auth_id)
|
go RunShellCID(res.Name, res.Url, res.Script, int(res.ID), res.Auth_id)
|
||||||
go RunShell(user[0].Name, res.Url, res.Script, int(res.ID), res.Auth_id)
|
resp.Code, resp.Message, resp.Data = proto.SuccessCode, "success", res.Name
|
||||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
c.JSON(http.StatusOK, resp)
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
|
resp.Code, resp.Message = proto.OperationFailed, "CID not found by id and token"
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -220,6 +252,44 @@ func RunShell(username, url, script string, id, authID int) {
|
||||||
name := strs[len(strs)-1]
|
name := strs[len(strs)-1]
|
||||||
names := strings.Split(name, ".")
|
names := strings.Split(name, ".")
|
||||||
name = names[0]
|
name = names[0]
|
||||||
|
//脚本内容,不同用户的持续集成、部署目录不同
|
||||||
|
scriptContent := `
|
||||||
|
echo "start"
|
||||||
|
` + script + `
|
||||||
|
echo "end"`
|
||||||
|
start := time.Now()
|
||||||
|
//执行脚本
|
||||||
|
cmd := exec.Command("/bin/bash", "-c", scriptContent)
|
||||||
|
// 使用bytes.Buffer捕获输出
|
||||||
|
var out bytes.Buffer
|
||||||
|
cmd.Stdout = &out
|
||||||
|
err3 := cmd.Run()
|
||||||
|
err3_info := ""
|
||||||
|
if err3 != nil {
|
||||||
|
err3_info = err3.Error()
|
||||||
|
}
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
//fmt.Println("bash content:", scriptContent)
|
||||||
|
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info, elapsed.Seconds()) //添加执行日志
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunShellCID(cid_name, url, script string, id, authID int) {
|
||||||
|
strs := strings.Split(url, "/")
|
||||||
|
name := strs[len(strs)-1]
|
||||||
|
names := strings.Split(name, ".")
|
||||||
|
name = names[0]
|
||||||
|
now := time.Now()
|
||||||
|
var cid_running proto.CIDRunning
|
||||||
|
cid_running.ID = id
|
||||||
|
cid_running.AuthID = authID
|
||||||
|
cid_running.StartTime = now
|
||||||
|
cid_running.CID = cid_name
|
||||||
|
//加入正在运行
|
||||||
|
proto.CID_RunningMutex.Lock()
|
||||||
|
user_running_list := proto.CID_Running_Map[authID]
|
||||||
|
user_running_list = append(user_running_list, cid_running)
|
||||||
|
proto.CID_Running_Map[authID] = user_running_list
|
||||||
|
proto.CID_RunningMutex.Unlock()
|
||||||
|
|
||||||
//脚本内容,不同用户的持续集成、部署目录不同
|
//脚本内容,不同用户的持续集成、部署目录不同
|
||||||
scriptContent := `
|
scriptContent := `
|
||||||
|
|
@ -240,6 +310,23 @@ echo "end"`
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
//fmt.Println("bash content:", scriptContent)
|
//fmt.Println("bash content:", scriptContent)
|
||||||
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info, elapsed.Seconds()) //添加执行日志
|
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info, elapsed.Seconds()) //添加执行日志
|
||||||
|
if err3 != nil {
|
||||||
|
dao.UpdateLastFailByID(id, time.Now())
|
||||||
|
} else {
|
||||||
|
dao.UpdateLastSuccessByID(id, time.Now())
|
||||||
|
}
|
||||||
|
//移除正在运行
|
||||||
|
proto.CID_RunningMutex.Lock()
|
||||||
|
user_running_list = proto.CID_Running_Map[authID]
|
||||||
|
for i, v := range user_running_list {
|
||||||
|
if v.StartTime.Equal(now) == true {
|
||||||
|
//删除
|
||||||
|
user_running_list = append(user_running_list[:i], user_running_list[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
proto.CID_Running_Map[authID] = user_running_list
|
||||||
|
proto.CID_RunningMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定时任务处理逻辑
|
// 定时任务处理逻辑
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
"videoplayer/service"
|
"videoplayer/service"
|
||||||
|
|
@ -15,7 +16,56 @@ func SetDBManageGroup(router *gin.Engine) {
|
||||||
dbm.POST("/get_db_manage", GetDBManageHandler) // 获取数据库管理信息
|
dbm.POST("/get_db_manage", GetDBManageHandler) // 获取数据库管理信息
|
||||||
dbm.POST("/get_sql_history", GetSQLRunHistoryHandler) // 获取SQL运行历史
|
dbm.POST("/get_sql_history", GetSQLRunHistoryHandler) // 获取SQL运行历史
|
||||||
dbm.POST("/update_db_manage", UpdateDBManageHandler) // 更新数据库管理信息
|
dbm.POST("/update_db_manage", UpdateDBManageHandler) // 更新数据库管理信息
|
||||||
|
dbm.POST("/del_db_manage", DeleteDBManageHandler) // 删除数据库管理信息
|
||||||
|
dbm.POST("/del_dbm_sql_history", DeleteSQLRunHistoryHandler) // 删除SQL运行历史
|
||||||
|
dbm.POST("/get_db_table_desc", GetDBTableDescHandler) // 获取数据库表描述
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteSQLRunHistoryHandler(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
userID := int(id.(float64))
|
||||||
|
|
||||||
|
var req proto.DeleteDBManageSQLHistoryReq
|
||||||
|
var resp proto.GeneralResp
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
resp.Code = proto.ParameterError
|
||||||
|
resp.Message = "请求参数解析错误"
|
||||||
|
} else {
|
||||||
|
err2 := service.DeleteSQLRunHistory(&req, userID)
|
||||||
|
if err2 != nil {
|
||||||
|
resp.Code = proto.DBMRunSQLHistoryDeleteFailed
|
||||||
|
resp.Message = "删除SQL运行历史失败: " + err2.Error()
|
||||||
|
} else {
|
||||||
|
resp.Code = proto.SuccessCode
|
||||||
|
resp.Message = "删除SQL运行历史成功"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteDBManageHandler(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
userID := int(id.(float64))
|
||||||
|
|
||||||
|
var req proto.DeleteDBManageReq
|
||||||
|
var resp proto.GeneralResp
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
resp.Code = proto.ParameterError
|
||||||
|
resp.Message = "请求参数解析错误"
|
||||||
|
} else {
|
||||||
|
err2 := service.DeleteDBManage(&req, userID)
|
||||||
|
if err2 != nil {
|
||||||
|
resp.Code = proto.DBMDeleteFailed
|
||||||
|
resp.Message = "删除数据库管理失败: " + err2.Error()
|
||||||
|
} else {
|
||||||
|
resp.Code = proto.SuccessCode
|
||||||
|
resp.Message = "删除数据库管理成功"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
|
}
|
||||||
|
|
||||||
func UpdateDBManageHandler(c *gin.Context) {
|
func UpdateDBManageHandler(c *gin.Context) {
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
userID := int(id.(float64))
|
userID := int(id.(float64))
|
||||||
|
|
@ -118,6 +168,7 @@ func RunSQLHandler(c *gin.Context) {
|
||||||
resp.Code = proto.ParameterError
|
resp.Code = proto.ParameterError
|
||||||
resp.Message = "请求参数解析错误"
|
resp.Message = "请求参数解析错误"
|
||||||
} else {
|
} else {
|
||||||
|
log.Println("run sql request, sql is:", req.SQL)
|
||||||
req.UserID = userID
|
req.UserID = userID
|
||||||
res, err2 := service.RunSQL(&req)
|
res, err2 := service.RunSQL(&req)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
|
|
@ -130,4 +181,28 @@ func RunSQLHandler(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, resp)
|
c.JSON(http.StatusOK, resp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDBTableDescHandler(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
userID := int(id.(float64))
|
||||||
|
|
||||||
|
var req proto.GetDBTableDescReq
|
||||||
|
var resp proto.GeneralResp
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
resp.Code = proto.ParameterError
|
||||||
|
resp.Message = "请求参数解析错误"
|
||||||
|
} else {
|
||||||
|
tableDesc, err2 := service.GetDBTableDesc(&req, userID)
|
||||||
|
if err2 != nil {
|
||||||
|
resp.Code = proto.DBMGetTableDescFailed
|
||||||
|
resp.Message = "获取数据库表描述失败: " + err2.Error()
|
||||||
|
} else {
|
||||||
|
resp.Code = proto.SuccessCode
|
||||||
|
resp.Message = "获取数据库表描述成功"
|
||||||
|
resp.Data = tableDesc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ func UploadFileV2(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
user := dao.FindUserByUserID(id1)
|
user := dao.FindUserByUserID(id1)
|
||||||
if user.Upload == false {
|
if user.Upload <= 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
|
|
@ -57,6 +59,8 @@ func SetUpToolGroup(router *gin.Engine) {
|
||||||
toolGroup.POST("/del_monitor", DelMonitor) //删除设备监控
|
toolGroup.POST("/del_monitor", DelMonitor) //删除设备监控
|
||||||
//发送邮件
|
//发送邮件
|
||||||
toolGroup.POST("/send_mail", SendMailTool)
|
toolGroup.POST("/send_mail", SendMailTool)
|
||||||
|
//下载代理
|
||||||
|
toolGroup.GET("/dlp", DownloadProxyHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMonitorList(c *gin.Context) {
|
func GetMonitorList(c *gin.Context) {
|
||||||
|
|
@ -254,7 +258,7 @@ func UploadFile(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
user := dao.FindUserByUserID(id1)
|
user := dao.FindUserByUserID(id1)
|
||||||
if user.Upload == false {
|
if user.Upload <= 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -347,7 +351,7 @@ func SetRedis(c *gin.Context) {
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
id1 := int(id.(float64))
|
id1 := int(id.(float64))
|
||||||
user := dao.FindUserByUserID(id1)
|
user := dao.FindUserByUserID(id1)
|
||||||
if user.Redis == false {
|
if user.Redis <= 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -375,7 +379,7 @@ func GetRedis(c *gin.Context) {
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
id1 := int(id.(float64))
|
id1 := int(id.(float64))
|
||||||
user := dao.FindUserByUserID(id1)
|
user := dao.FindUserByUserID(id1)
|
||||||
if user.Redis == false {
|
if user.Redis <= 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -440,7 +444,14 @@ func SendMail(title, content string) {
|
||||||
em.SmtpUserName = "354425203@qq.com"
|
em.SmtpUserName = "354425203@qq.com"
|
||||||
em.SmtpPort = 587
|
em.SmtpPort = 587
|
||||||
em.ImapPort = 993
|
em.ImapPort = 993
|
||||||
err := em.Send(title, content, []string{"3236990479@qq.com", "lijun@ljsea.top"})
|
var targetMails []string
|
||||||
|
for _, v := range proto.Config.MONITOR_MAIL {
|
||||||
|
targetMails = append(targetMails, v.Value)
|
||||||
|
}
|
||||||
|
if targetMails == nil || len(targetMails) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err := em.Send(title, content, targetMails)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
@ -478,3 +489,71 @@ func SendMailTool(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DownloadProxyHandle 处理下载代理请求
|
||||||
|
func DownloadProxyHandle(c *gin.Context) {
|
||||||
|
key := c.Query("key")
|
||||||
|
if key == "" || key != proto.Config.DOWNLOAD_PROXY_KEY {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "failed, key is null or error"})
|
||||||
|
}
|
||||||
|
// 获取URL参数
|
||||||
|
encodedURL := c.Query("url")
|
||||||
|
if encodedURL == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "URL参数不能为空", "code": proto.ParameterError, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// URL解码
|
||||||
|
decodedURL, err := url.QueryUnescape(encodedURL)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "URL解码失败: " + err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证URL格式
|
||||||
|
parsedURL, err := url.Parse(decodedURL)
|
||||||
|
if err != nil || parsedURL.Scheme == "" || parsedURL.Host == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的URL格式", "code": proto.ParameterError, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发起请求获取目标资源
|
||||||
|
resp, err := http.Get(decodedURL)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取资源失败: " + err.Error(), "code": proto.InternalServerError})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// 检查响应状态
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
c.JSON(http.StatusBadGateway, gin.H{"error": "目标资源请求失败,状态码: " + resp.Status, "code": proto.InternalServerError})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件名
|
||||||
|
filename := getFilenameFromURL(parsedURL)
|
||||||
|
|
||||||
|
// 设置响应头,告诉浏览器这是一个文件下载
|
||||||
|
c.Header("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
|
||||||
|
c.Header("Content-Type", resp.Header.Get("Content-Type"))
|
||||||
|
c.Header("Content-Length", resp.Header.Get("Content-Length"))
|
||||||
|
|
||||||
|
// 将响应体流式传输到客户端
|
||||||
|
_, err = io.Copy(c.Writer, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
// 已经开始传输数据后发生错误,无法返回JSON,只能记录日志
|
||||||
|
log.Println("tool download proxy handle error:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从URL中提取文件名
|
||||||
|
func getFilenameFromURL(u *url.URL) string {
|
||||||
|
// 从路径中提取文件名
|
||||||
|
filename := filepath.Base(u.Path)
|
||||||
|
// 如果路径中没有文件名,尝试从查询参数中获取
|
||||||
|
if filename == "" || filename == "/" {
|
||||||
|
filename = "download_file"
|
||||||
|
}
|
||||||
|
return filename
|
||||||
|
}
|
||||||
|
|
|
||||||
15
main.go
15
main.go
|
|
@ -32,10 +32,12 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to connect database:" + err.Error())
|
panic("failed to connect database:" + err.Error())
|
||||||
}
|
}
|
||||||
|
if proto.Config.MEMORY_CACHE == false { //不开启内存缓存,才使用redis
|
||||||
err = worker.InitRedis()
|
err = worker.InitRedis()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to connect redis:" + err.Error())
|
panic("failed to connect redis:" + err.Error())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
r.Use(handler.CrosHandler())
|
r.Use(handler.CrosHandler())
|
||||||
r.Use(JWTAuthMiddleware()) // 使用 JWT 认证中间件
|
r.Use(JWTAuthMiddleware()) // 使用 JWT 认证中间件
|
||||||
handler.SetUpVideoGroup(r) // Video
|
handler.SetUpVideoGroup(r) // Video
|
||||||
|
|
@ -232,7 +234,12 @@ func myTask() {
|
||||||
RunGeneralCron()
|
RunGeneralCron()
|
||||||
service.ShellWillRunFromServer()
|
service.ShellWillRunFromServer()
|
||||||
service.SyncTokenSecretFromUserCenter()
|
service.SyncTokenSecretFromUserCenter()
|
||||||
|
service.DelDBMMap() //定时处理DBMMap中的数据
|
||||||
|
if proto.Config.MEMORY_CACHE {
|
||||||
|
worker.DeleteMemoryCacheCron()
|
||||||
|
// 清理后持久化
|
||||||
|
worker.WriteMemoryCacheToFile()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadConfigToSetSystem() {
|
func ReadConfigToSetSystem() {
|
||||||
|
|
@ -385,13 +392,13 @@ func UserFuncIntercept(id int, url string) bool {
|
||||||
//如果用户有权限,则不拦截
|
//如果用户有权限,则不拦截
|
||||||
for k, v := range proto.Per_menu_map {
|
for k, v := range proto.Per_menu_map {
|
||||||
if strings.Contains(url, k) {
|
if strings.Contains(url, k) {
|
||||||
if v == 1 && user.VideoFunc == false {
|
if v == 1 && user.VideoFunc <= 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if v == 2 && user.DeviceFunc == false {
|
if v == 2 && user.DeviceFunc <= 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if v == 3 && user.CIDFunc == false {
|
if v == 3 && user.CIDFunc <= 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,13 @@ import (
|
||||||
|
|
||||||
var Config ConfigStruct
|
var Config ConfigStruct
|
||||||
var SigningKey = []byte{}
|
var SigningKey = []byte{}
|
||||||
var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true, "/user/sync": true, "/tool/file/": true, "/user/reset": true} // 不需要token验证的url
|
var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true, "/user/sync": true, "/tool/file/": true, "/user/reset": true, "/tool/dlp": true} // 不需要token验证的url
|
||||||
var Per_menu_map = map[string]int{"/video/": 1, "/device/": 2, "/cid/": 3}
|
var Per_menu_map = map[string]int{"/video/": 1, "/device/": 2, "/cid/": 3}
|
||||||
var File_Type = map[string]int{"im": 1, "avatar": 2, "file": 3, "config": 4} // 文件类型
|
var File_Type = map[string]int{"im": 1, "avatar": 2, "file": 3, "config": 4} // 文件类型
|
||||||
|
var CID_Running_Map = map[int][]CIDRunning{} //正在运行的cid
|
||||||
|
|
||||||
// 配置读写锁
|
// 配置读写锁
|
||||||
|
var CID_RunningMutex = sync.RWMutex{}
|
||||||
var ConfigRWLock = &sync.RWMutex{}
|
var ConfigRWLock = &sync.RWMutex{}
|
||||||
var SigningKeyRWLock = &sync.RWMutex{}
|
var SigningKeyRWLock = &sync.RWMutex{}
|
||||||
|
|
||||||
|
|
@ -27,13 +29,13 @@ var SigningKeyIsValid = true // 是否有效的签名密钥
|
||||||
const (
|
const (
|
||||||
MYSQL_USER = "video_t2"
|
MYSQL_USER = "video_t2"
|
||||||
MYSQL_DB = "video_t2"
|
MYSQL_DB = "video_t2"
|
||||||
MYSQL_PASSWORD = "2t2SKHmWEYj2xFKF"
|
MYSQL_PASSWORD = "2fdreYj2xFKF"
|
||||||
MYSQL_PORT = "3306"
|
MYSQL_PORT = "3306"
|
||||||
MYSQL_HOST = "127.0.0.1"
|
MYSQL_HOST = "127.0.0.1"
|
||||||
MYSQL_DSN = MYSQL_USER + ":" + MYSQL_PASSWORD + "@tcp(" + MYSQL_HOST + ":" + MYSQL_PORT + ")/" + MYSQL_DB + "?charset=utf8mb4&parseTime=True&loc=Local"
|
MYSQL_DSN = MYSQL_USER + ":" + MYSQL_PASSWORD + "@tcp(" + MYSQL_HOST + ":" + MYSQL_PORT + ")/" + MYSQL_DB + "?charset=utf8mb4&parseTime=True&loc=Local"
|
||||||
|
|
||||||
REDIS_ADDR = "127.0.0.1:6379"
|
REDIS_ADDR = "127.0.0.1:6379"
|
||||||
REDIS_PASSWORD = "lj502138"
|
REDIS_PASSWORD = "lgybvueiogvter"
|
||||||
REIDS_DB = 2
|
REIDS_DB = 2
|
||||||
|
|
||||||
TOKEN_SECRET = "mfjurnc_32ndj9dfhj"
|
TOKEN_SECRET = "mfjurnc_32ndj9dfhj"
|
||||||
|
|
@ -43,6 +45,8 @@ const (
|
||||||
|
|
||||||
// 以下是文件上传的配置
|
// 以下是文件上传的配置
|
||||||
FILE_BASE_DIR = "/home/lijun/file/"
|
FILE_BASE_DIR = "/home/lijun/file/"
|
||||||
|
|
||||||
|
DBMMap_Max_Keep_Time = 10 * 60 //DBMap中的数据最大保持时间,10min
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -73,15 +77,20 @@ type User struct {
|
||||||
Email string `gorm:"column:email"`
|
Email string `gorm:"column:email"`
|
||||||
Gender string `gorm:"column:gender"`
|
Gender string `gorm:"column:gender"`
|
||||||
}
|
}
|
||||||
|
type StructValue struct {
|
||||||
|
Value string `json:"value" form:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
type ConfigStruct struct {
|
type ConfigStruct struct {
|
||||||
DB int `json:"db"` // 0: mysql, 1: pg
|
DB int `json:"db"` // 0: mysql, 1: pg, 2:sqlite
|
||||||
MYSQL_DSN string `json:"mysql_dsn"`
|
MYSQL_DSN string `json:"mysql_dsn"`
|
||||||
PG_DSN string `json:"pg_dsn"`
|
PG_DSN string `json:"pg_dsn"`
|
||||||
|
SQLITE_FILE string `json:"sqlite_file"`
|
||||||
REDIS_ADDR string `json:"redis_addr"`
|
REDIS_ADDR string `json:"redis_addr"`
|
||||||
TOKEN_USE_REDIS bool `json:"token_use_redis"`
|
TOKEN_USE_REDIS bool `json:"token_use_redis"`
|
||||||
REDIS_User_PW bool `json:"redis_user_pw"` // 是否使用密码
|
REDIS_User_PW bool `json:"redis_user_pw"` // 是否使用密码
|
||||||
REDIS_PASSWORD string `json:"redis_password"`
|
REDIS_PASSWORD string `json:"redis_password"`
|
||||||
|
MEMORY_CACHE bool `json:"memory_cache"` //使用程序内缓存,开启这个redis将不生效
|
||||||
REDIS_DB int `json:"redis_db"`
|
REDIS_DB int `json:"redis_db"`
|
||||||
TOKEN_SECRET string `json:"token_secret"`
|
TOKEN_SECRET string `json:"token_secret"`
|
||||||
CID_BASE_DIR string `json:"cid_base_dir"`
|
CID_BASE_DIR string `json:"cid_base_dir"`
|
||||||
|
|
@ -96,6 +105,8 @@ type ConfigStruct struct {
|
||||||
SERVER_NAME string `json:"server_name"` // 服务器名称,用于区分不同服务器
|
SERVER_NAME string `json:"server_name"` // 服务器名称,用于区分不同服务器
|
||||||
MONITOR_SERVER_TOKEN string `json:"monitor_server_token"` // 监控服务器token,用于状态监控及邮件通知
|
MONITOR_SERVER_TOKEN string `json:"monitor_server_token"` // 监控服务器token,用于状态监控及邮件通知
|
||||||
APP_ID string `json:"app_id"` // 应用ID,用于标识不同应用
|
APP_ID string `json:"app_id"` // 应用ID,用于标识不同应用
|
||||||
|
MONITOR_MAIL []StructValue `json:"monitor_mail"` // 设备监控邮件通知配置
|
||||||
|
DOWNLOAD_PROXY_KEY string `json:"download_proxy_key"` // 下载代理key
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteConfigToFile() {
|
func WriteConfigToFile() {
|
||||||
|
|
@ -121,25 +132,7 @@ func ReadConfig(path string) error {
|
||||||
fmt.Println("Config file not found!")
|
fmt.Println("Config file not found!")
|
||||||
//创建默认配置
|
//创建默认配置
|
||||||
DefaultConfig()
|
DefaultConfig()
|
||||||
//写入json文件
|
WriteConfigToFile()
|
||||||
file, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating config file")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
encoder := json.NewEncoder(file)
|
|
||||||
|
|
||||||
configData, err2 := json.MarshalIndent(Config, "", " ")
|
|
||||||
if err2 != nil {
|
|
||||||
log.Println("WriteConfigToFile json marshal error:", err)
|
|
||||||
return err2
|
|
||||||
}
|
|
||||||
err = encoder.Encode(string(configData))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error encoding config")
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//读json文件
|
//读json文件
|
||||||
|
|
@ -182,6 +175,7 @@ func DefaultConfig() {
|
||||||
Config.MYSQL_DSN = MYSQL_DSN
|
Config.MYSQL_DSN = MYSQL_DSN
|
||||||
Config.PG_DSN = ""
|
Config.PG_DSN = ""
|
||||||
Config.REDIS_ADDR = REDIS_ADDR
|
Config.REDIS_ADDR = REDIS_ADDR
|
||||||
|
Config.SQLITE_FILE = ""
|
||||||
Config.TOKEN_USE_REDIS = false
|
Config.TOKEN_USE_REDIS = false
|
||||||
Config.REDIS_User_PW = false
|
Config.REDIS_User_PW = false
|
||||||
Config.REDIS_PASSWORD = REDIS_PASSWORD
|
Config.REDIS_PASSWORD = REDIS_PASSWORD
|
||||||
|
|
|
||||||
53
proto/dbm.go
53
proto/dbm.go
|
|
@ -1,6 +1,8 @@
|
||||||
package proto
|
package proto
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DB_TYPE_MYSQL = 0 // DBTypeMySQL MySQL数据库
|
DB_TYPE_MYSQL = 0 // DBTypeMySQL MySQL数据库
|
||||||
|
|
@ -21,7 +23,7 @@ type RunSQLRequest struct {
|
||||||
type DBManage struct {
|
type DBManage struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
UserID uint `gorm:"column:user_id"` // 用户ID
|
UserID uint `gorm:"column:user_id"` // 用户ID
|
||||||
DB_IP string `gorm:"column:db_ip;type:varchar(255);uniqueIndex:idx_db_ip"` // 数据库IP
|
DB_IP string `gorm:"column:db_ip;type:varchar(255)"` // 数据库IP
|
||||||
DB_Port uint `gorm:"column:db_port"` // 数据库端口
|
DB_Port uint `gorm:"column:db_port"` // 数据库端口
|
||||||
DB_NAME string `gorm:"column:db_name;type:varchar(255);uniqueIndex:idx_db_name"` // 数据库名称
|
DB_NAME string `gorm:"column:db_name;type:varchar(255);uniqueIndex:idx_db_name"` // 数据库名称
|
||||||
DB_User string `gorm:"column:db_user;type:varchar(255);uniqueIndex:idx_db_user"` // 数据库用户名
|
DB_User string `gorm:"column:db_user;type:varchar(255);uniqueIndex:idx_db_user"` // 数据库用户名
|
||||||
|
|
@ -46,6 +48,7 @@ type CreateDBManageReq struct {
|
||||||
DB_User string `json:"db_user" form:"db_user"` // 数据库用户名
|
DB_User string `json:"db_user" form:"db_user"` // 数据库用户名
|
||||||
DB_Password string `json:"db_password" form:"db_password"` // 数据库密码
|
DB_Password string `json:"db_password" form:"db_password"` // 数据库密码
|
||||||
DB_Type uint `json:"db_type" form:"db_type"` // 数据库类型: 0为mysql,1为postgres,2为sqlite,3为sqlserver,4为oracle,5为mongodb,6为redis
|
DB_Type uint `json:"db_type" form:"db_type"` // 数据库类型: 0为mysql,1为postgres,2为sqlite,3为sqlserver,4为oracle,5为mongodb,6为redis
|
||||||
|
DB_Desc string `json:"db_desc" form:"db_desc"` // 数据库描述备注
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateDBManageReq struct {
|
type UpdateDBManageReq struct {
|
||||||
|
|
@ -56,6 +59,7 @@ type UpdateDBManageReq struct {
|
||||||
DB_User string `json:"db_user" form:"db_user"` // 数据库用户名
|
DB_User string `json:"db_user" form:"db_user"` // 数据库用户名
|
||||||
DB_Password string `json:"db_password" form:"db_password"` // 数据库密码
|
DB_Password string `json:"db_password" form:"db_password"` // 数据库密码
|
||||||
DB_Type uint `json:"db_type" form:"db_type"` // 数据库类型: 0为mysql,1为postgres,2为sqlite,3为sqlserver,4为oracle,5为mongodb,6为redis
|
DB_Type uint `json:"db_type" form:"db_type"` // 数据库类型: 0为mysql,1为postgres,2为sqlite,3为sqlserver,4为oracle,5为mongodb,6为redis
|
||||||
|
DB_Desc string `json:"db_desc" form:"db_desc"` // 数据库描述备注
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetDBManageReq struct {
|
type GetDBManageReq struct {
|
||||||
|
|
@ -67,3 +71,48 @@ type GetSQLRunHistoryReq struct {
|
||||||
DB_ID uint `json:"db_id" form:"db_id"` // 数据库ID
|
DB_ID uint `json:"db_id" form:"db_id"` // 数据库ID
|
||||||
GET_TYPE int `json:"get_type" form:"get_type"` // 获取类型: 0获取自己,1为获取全部(管理员权限)
|
GET_TYPE int `json:"get_type" form:"get_type"` // 获取类型: 0获取自己,1为获取全部(管理员权限)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SQLResult 包含查询结果的列名顺序和对应数据
|
||||||
|
type SQLResult struct {
|
||||||
|
Columns []SQLResultColumnsValue // 列名顺序(与 SQL 查询的列顺序一致)
|
||||||
|
Rows []map[string]interface{} // 每行数据(map 便于按列名访问)
|
||||||
|
}
|
||||||
|
|
||||||
|
type SQLResultColumnsValue struct {
|
||||||
|
Prop string `json:"prop"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Attr string `json:"attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteDBManageReq struct {
|
||||||
|
DB_ID uint `json:"db_id" form:"db_id"` // 数据库ID
|
||||||
|
Del_Type uint `json:"del_type" form:"del_type"` // 删除类型: 0为删除单条,1为所有
|
||||||
|
UserID uint `json:"user_id" form:"user_id"` // 用户ID
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteDBManageSQLHistoryReq struct {
|
||||||
|
DB_ID uint `json:"db_id" form:"db_id"` // 数据库ID
|
||||||
|
Del_Type uint `json:"del_type" form:"del_type"` // 删除类型: 0为删除单条,1为所有
|
||||||
|
History_ID uint `json:"history_id" form:"history_id"` // SQL执行历史ID,如果为0则删除所有
|
||||||
|
UserID uint `json:"user_id" form:"user_id"` // 用户ID
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetDBTableDescReq struct {
|
||||||
|
DB_ID uint `json:"db_id" form:"db_id"` // 数据库ID
|
||||||
|
Table string `json:"table" form:"table"` // 数据库表名
|
||||||
|
GetType int `json:"get_type" form:"get_type"` // 获取类型: 0获取全部,1获取1个
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetDBTableDescResp struct {
|
||||||
|
TableName string `json:"table_name"` // 表名
|
||||||
|
}
|
||||||
|
|
||||||
|
type DBTableAttribute struct {
|
||||||
|
ColumnName string `json:"column_name"` // 列名
|
||||||
|
ColumnType string `json:"column_type"` // 列类型
|
||||||
|
}
|
||||||
|
|
||||||
|
type DBValue struct {
|
||||||
|
LastUserTime int64 `json:"last_user_time"`
|
||||||
|
Value *gorm.DB `json:"value"`
|
||||||
|
}
|
||||||
|
|
|
||||||
10
proto/im.go
10
proto/im.go
|
|
@ -1,5 +1,7 @@
|
||||||
package proto
|
package proto
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type ImKeyReq struct {
|
type ImKeyReq struct {
|
||||||
To_user_id int `json:"to_user_id" form:"to_user_id" binding:"required"`
|
To_user_id int `json:"to_user_id" form:"to_user_id" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
@ -12,3 +14,11 @@ type Message struct {
|
||||||
From_user_id int `json:"from_user_id"`
|
From_user_id int `json:"from_user_id"`
|
||||||
Session string `json:"session"`
|
Session string `json:"session"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cid正在运行结构
|
||||||
|
type CIDRunning struct {
|
||||||
|
ID int `json:"id" form:"id"` //cid的id
|
||||||
|
CID string `json:"cid" form:"cid"` //cid名称
|
||||||
|
AuthID int `json:"auth_id" form:"auth_id"` //所属用户
|
||||||
|
StartTime time.Time `json:"start_time" form:"start_time"` //开始时间
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,8 @@ type GeneralResp struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Data any `json:"data"`
|
Data any `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CIDCallBackReq struct {
|
||||||
|
Token string `json:"token" form:"token"`
|
||||||
|
ID int `json:"id" form:"id"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,4 +85,7 @@ const (
|
||||||
DBMCreateFailed = 1002 // 创建数据库管理失败
|
DBMCreateFailed = 1002 // 创建数据库管理失败
|
||||||
DBMGetFailed = 1003 // 获取数据库管理信息失败`
|
DBMGetFailed = 1003 // 获取数据库管理信息失败`
|
||||||
DBMUpdateFailed = 1004 // 更新数据库管理信息失败
|
DBMUpdateFailed = 1004 // 更新数据库管理信息失败
|
||||||
|
DBMDeleteFailed = 1005 // 删除数据库管理信息失败
|
||||||
|
DBMRunSQLHistoryDeleteFailed = 1006 // 删除SQL运行历史失败
|
||||||
|
DBMGetTableDescFailed = 1007 // 获取表描述信息失败
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,10 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
|
"videoplayer/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunSQL(req *proto.RunSQLRequest) ([]map[string]interface{}, error) {
|
func RunSQL(req *proto.RunSQLRequest) (*proto.SQLResult, error) {
|
||||||
|
|
||||||
dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
|
dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -23,7 +24,7 @@ func RunSQL(req *proto.RunSQLRequest) ([]map[string]interface{}, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
res, err := dao.RunSQL(req.SQL, db_)
|
res, err := dao.RunSQLWithOrder(req.SQL, db_)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -33,10 +34,20 @@ func RunSQL(req *proto.RunSQLRequest) ([]map[string]interface{}, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return &res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
|
func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
|
||||||
|
//dao.DBMMapRWMutex.RLock()
|
||||||
|
if dao.DBMMap != nil {
|
||||||
|
dbValue := dao.DBMMap[dbmInfo.ID]
|
||||||
|
if dbValue != nil {
|
||||||
|
dbValue.LastUserTime = worker.GetCurrentTimestamp()
|
||||||
|
return dbValue.Value, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//dao.DBMMapRWMutex.RUnlock()
|
||||||
|
|
||||||
switch dbmInfo.DB_Type {
|
switch dbmInfo.DB_Type {
|
||||||
case proto.DB_TYPE_MYSQL: // MySQL
|
case proto.DB_TYPE_MYSQL: // MySQL
|
||||||
dsn := dbmInfo.DB_User + ":" + dbmInfo.DB_Password + "@tcp(" + dbmInfo.DB_IP + ":" + strconv.Itoa(int(dbmInfo.DB_Port)) + ")/" + dbmInfo.DB_NAME + "?charset=utf8mb4&parseTime=True&loc=Local"
|
dsn := dbmInfo.DB_User + ":" + dbmInfo.DB_Password + "@tcp(" + dbmInfo.DB_IP + ":" + strconv.Itoa(int(dbmInfo.DB_Port)) + ")/" + dbmInfo.DB_NAME + "?charset=utf8mb4&parseTime=True&loc=Local"
|
||||||
|
|
@ -46,6 +57,7 @@ func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
|
||||||
}
|
}
|
||||||
case proto.DB_TYPE_POSTGRES: // PostgreSQL
|
case proto.DB_TYPE_POSTGRES: // PostgreSQL
|
||||||
dsn := "host=" + dbmInfo.DB_IP + " user=" + dbmInfo.DB_User + " password=" + dbmInfo.DB_Password + " dbname=" + dbmInfo.DB_NAME + " port=" + strconv.Itoa(int(dbmInfo.DB_Port)) + " sslmode=disable TimeZone=Asia/Shanghai"
|
dsn := "host=" + dbmInfo.DB_IP + " user=" + dbmInfo.DB_User + " password=" + dbmInfo.DB_Password + " dbname=" + dbmInfo.DB_NAME + " port=" + strconv.Itoa(int(dbmInfo.DB_Port)) + " sslmode=disable TimeZone=Asia/Shanghai"
|
||||||
|
|
||||||
db_, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
db_, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -53,6 +65,13 @@ func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
|
||||||
default:
|
default:
|
||||||
err = errors.New("unsupported database type")
|
err = errors.New("unsupported database type")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dao.DBMMapRWMutex.Lock()
|
||||||
|
var dbValue proto.DBValue
|
||||||
|
dbValue.Value = db_
|
||||||
|
dbValue.LastUserTime = worker.GetCurrentTimestamp()
|
||||||
|
dao.DBMMap[dbmInfo.ID] = &dbValue
|
||||||
|
dao.DBMMapRWMutex.Unlock()
|
||||||
return db_, err
|
return db_, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,7 +84,7 @@ func CreateDBManage(req *proto.CreateDBManageReq, userID uint) (proto.DBManage,
|
||||||
DB_User: req.DB_User,
|
DB_User: req.DB_User,
|
||||||
DB_Password: req.DB_Password,
|
DB_Password: req.DB_Password,
|
||||||
DB_Type: req.DB_Type,
|
DB_Type: req.DB_Type,
|
||||||
DB_Desc: "",
|
DB_Desc: req.DB_Desc,
|
||||||
DB_STATUS: 0, // 初始状态为未连接
|
DB_STATUS: 0, // 初始状态为未连接
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,7 +122,11 @@ func GetSQLRunHistory(req *proto.GetSQLRunHistoryReq, userID int) ([]proto.SQLRu
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if req.GET_TYPE == 0 { // 获取自己的SQL执行历史
|
if req.GET_TYPE == 0 { // 获取自己的SQL执行历史
|
||||||
|
if req.DB_ID > 0 {
|
||||||
|
historyList, err = dao.FindDBRunHistoryByAuthIDAndDbId(userID, req.DB_ID)
|
||||||
|
} else {
|
||||||
historyList, err = dao.FindDBRunHistoryByAuthID(userID)
|
historyList, err = dao.FindDBRunHistoryByAuthID(userID)
|
||||||
|
}
|
||||||
} else if req.GET_TYPE == 1 { // 管理员获取所有SQL执行历史
|
} else if req.GET_TYPE == 1 { // 管理员获取所有SQL执行历史
|
||||||
user := GetUserByIDFromUserCenter(userID)
|
user := GetUserByIDFromUserCenter(userID)
|
||||||
if user.Role != "admin" {
|
if user.Role != "admin" {
|
||||||
|
|
@ -133,6 +156,7 @@ func UpdateDBManage(req *proto.UpdateDBManageReq, userID int) (proto.DBManage, e
|
||||||
dbmInfo.DB_User = req.DB_User
|
dbmInfo.DB_User = req.DB_User
|
||||||
dbmInfo.DB_Password = req.DB_Password
|
dbmInfo.DB_Password = req.DB_Password
|
||||||
dbmInfo.DB_Type = req.DB_Type
|
dbmInfo.DB_Type = req.DB_Type
|
||||||
|
dbmInfo.DB_Desc = req.DB_Desc
|
||||||
|
|
||||||
err = dao.UpdateDBManage(dbmInfo.ID, &dbmInfo)
|
err = dao.UpdateDBManage(dbmInfo.ID, &dbmInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -140,3 +164,87 @@ func UpdateDBManage(req *proto.UpdateDBManageReq, userID int) (proto.DBManage, e
|
||||||
}
|
}
|
||||||
return dbmInfo, nil
|
return dbmInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteDBManage(req *proto.DeleteDBManageReq, userId int) error {
|
||||||
|
user := GetUserByIDFromUserCenter(userId)
|
||||||
|
if req.Del_Type == 0 && req.DB_ID > 0 {
|
||||||
|
dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dbmInfo.UserID != uint(req.UserID) && user.Role != "admin" {
|
||||||
|
return errors.New("unauthorized access to the database management system")
|
||||||
|
}
|
||||||
|
err = dao.DeleteDBManageByID(req.DB_ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if req.Del_Type == 1 && req.UserID > 0 {
|
||||||
|
if req.UserID != uint(userId) && user.Role != "admin" {
|
||||||
|
return errors.New("unauthorized access to delete all database management systems")
|
||||||
|
}
|
||||||
|
err := dao.DeleteDBManageByUserID(req.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.New("invalid delete type or parameters")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteSQLRunHistory(req *proto.DeleteDBManageSQLHistoryReq, userId int) error {
|
||||||
|
user := GetUserByIDFromUserCenter(userId)
|
||||||
|
if req.Del_Type == 0 && req.History_ID > 0 {
|
||||||
|
history, err := dao.FindDBRunHistoryByID(req.History_ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if history.UserID != uint(req.UserID) && user.Role != "admin" {
|
||||||
|
return errors.New("unauthorized access to the SQL run history")
|
||||||
|
}
|
||||||
|
err = dao.DelSQLRunHistoryByID(req.History_ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if req.Del_Type == 1 && req.UserID > 0 {
|
||||||
|
if req.UserID != uint(userId) && user.Role != "admin" {
|
||||||
|
return errors.New("unauthorized access to delete all SQL run history")
|
||||||
|
}
|
||||||
|
err := dao.DelSQLRunHistoryByAuthID(int(req.UserID))
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
return errors.New("invalid delete type or parameters")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDBTableDesc(req *proto.GetDBTableDescReq, userId int) (*proto.SQLResult, error) {
|
||||||
|
//dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
|
||||||
|
//if err != nil {`
|
||||||
|
// return nil, err
|
||||||
|
//}
|
||||||
|
//if dbmInfo.UserID != uint(userId) && GetUserByIDFromUserCenter(userId).Role != "admin" {
|
||||||
|
// return nil, errors.New("unauthorized access to the database management system")
|
||||||
|
//}
|
||||||
|
//db_, err := GetGORMDBObject(&dbmInfo)
|
||||||
|
//if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
//}
|
||||||
|
//res, err := dao.GetDBTableDesc(db_, req.Table, req.GetType)
|
||||||
|
//if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
//}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DelDBMMap() {
|
||||||
|
dao.DBMMapRWMutex.Lock()
|
||||||
|
cur := worker.GetCurrentTimestamp()
|
||||||
|
for k, v := range dao.DBMMap {
|
||||||
|
if (cur - v.LastUserTime) > proto.DBMMap_Max_Keep_Time {
|
||||||
|
delete(dao.DBMMap, k) //删除
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dao.DBMMapRWMutex.Unlock()
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -325,3 +325,28 @@ func GetTokenSecretFromUserCenter() (*proto.SecretSyncSettings, error) {
|
||||||
}
|
}
|
||||||
return &secretResp, nil
|
return &secretResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取cid正在运行
|
||||||
|
func GetCIDRunningList(user_id int, req_type int) ([]proto.CIDRunning, error) {
|
||||||
|
var err error
|
||||||
|
var resp []proto.CIDRunning
|
||||||
|
if req_type == 0 {
|
||||||
|
proto.CID_RunningMutex.RLock()
|
||||||
|
resp = proto.CID_Running_Map[user_id]
|
||||||
|
proto.CID_RunningMutex.RUnlock()
|
||||||
|
} else if req_type == 1 {
|
||||||
|
user := GetUserByIDFromUserCenter(user_id)
|
||||||
|
if user.Role != "admin" {
|
||||||
|
err = errors.New("no permission")
|
||||||
|
} else {
|
||||||
|
proto.CID_RunningMutex.RLock()
|
||||||
|
for _, v := range proto.CID_Running_Map {
|
||||||
|
resp = append(resp, v...)
|
||||||
|
}
|
||||||
|
proto.CID_RunningMutex.RUnlock()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = errors.New("request type is error")
|
||||||
|
}
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,186 @@
|
||||||
|
package worker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MemoryCacheValue 缓存值结构,包含值和过期时间
|
||||||
|
type MemoryCacheValue struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
ExpireAt int64 `json:"expireAt"` // 过期时间戳,秒级
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
memoryCacheData = NewMemoryCache()
|
||||||
|
)
|
||||||
|
|
||||||
|
// MemoryCache 线程安全的内存缓存
|
||||||
|
type MemoryCache struct {
|
||||||
|
data map[string]MemoryCacheValue
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMemoryCache 创建新的内存缓存实例
|
||||||
|
func NewMemoryCache() *MemoryCache {
|
||||||
|
return &MemoryCache{
|
||||||
|
data: make(map[string]MemoryCacheValue),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitStaticMemoryCache 初始化全局缓存实例
|
||||||
|
func InitStaticMemoryCache() {
|
||||||
|
// 先尝试从文件加载
|
||||||
|
ReadMemoryCacheFromJsonFile()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWithExp 设置带过期时间的键值对
|
||||||
|
func (mc *MemoryCache) SetWithExp(key string, value string, expireAt int64) {
|
||||||
|
mc.mu.Lock()
|
||||||
|
defer mc.mu.Unlock()
|
||||||
|
mc.data[key] = MemoryCacheValue{value, expireAt}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get 获取键值,如果已过期则返回空并删除
|
||||||
|
func (mc *MemoryCache) Get(key string) string {
|
||||||
|
// 先加读锁检查
|
||||||
|
mc.mu.RLock()
|
||||||
|
value, ok := mc.data[key]
|
||||||
|
mc.mu.RUnlock()
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否过期
|
||||||
|
now := time.Now().Unix()
|
||||||
|
if value.ExpireAt > now {
|
||||||
|
return value.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// 已过期,删除该键
|
||||||
|
mc.mu.Lock()
|
||||||
|
// 二次检查,防止并发情况下已被删除
|
||||||
|
if v, exists := mc.data[key]; exists && v.ExpireAt <= now {
|
||||||
|
delete(mc.data, key)
|
||||||
|
}
|
||||||
|
mc.mu.Unlock()
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set 设置永不过期的键值对
|
||||||
|
func (mc *MemoryCache) Set(key string, value string) {
|
||||||
|
mc.SetWithExp(key, value, math.MaxInt64)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Del 删除指定键
|
||||||
|
func (mc *MemoryCache) Del(key string) {
|
||||||
|
mc.mu.Lock()
|
||||||
|
defer mc.mu.Unlock()
|
||||||
|
delete(mc.data, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear 清空所有缓存
|
||||||
|
func (mc *MemoryCache) Clear() {
|
||||||
|
mc.mu.Lock()
|
||||||
|
defer mc.mu.Unlock()
|
||||||
|
mc.data = make(map[string]MemoryCacheValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
const CRON_MAX_DEL_NUMBER = 100 // 每次清理的最大数量
|
||||||
|
|
||||||
|
// DeleteMemoryCacheCron 定时清理过期键
|
||||||
|
func DeleteMemoryCacheCron() {
|
||||||
|
memoryCacheData.mu.Lock()
|
||||||
|
defer memoryCacheData.mu.Unlock()
|
||||||
|
|
||||||
|
now := time.Now().Unix()
|
||||||
|
i := 0
|
||||||
|
for key, value := range memoryCacheData.data {
|
||||||
|
if i >= CRON_MAX_DEL_NUMBER {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if value.ExpireAt < now {
|
||||||
|
delete(memoryCacheData.data, key)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMemoryCacheFilePath 获取缓存持久化文件路径
|
||||||
|
func GetMemoryCacheFilePath() string {
|
||||||
|
if os.Getenv("OS") == "Windows_NT" {
|
||||||
|
return "C:/Users/Administrator/vp_mc.json"
|
||||||
|
}
|
||||||
|
return "/etc/vp_mc.json"
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteMemoryCacheToFile 将缓存写入文件持久化
|
||||||
|
func WriteMemoryCacheToFile() {
|
||||||
|
memoryCacheData.mu.RLock()
|
||||||
|
defer memoryCacheData.mu.RUnlock()
|
||||||
|
|
||||||
|
path := GetMemoryCacheFilePath()
|
||||||
|
data, err := json.MarshalIndent(memoryCacheData.data, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("mc write file json err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先写入临时文件,再原子替换,防止文件损坏
|
||||||
|
tempPath := path + ".tmp"
|
||||||
|
if err := os.WriteFile(tempPath, data, 0644); err != nil {
|
||||||
|
log.Println("mc write temp file err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Rename(tempPath, path); err != nil {
|
||||||
|
log.Println("mc rename file err:", err)
|
||||||
|
os.Remove(tempPath) // 清理临时文件
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadMemoryCacheFromJsonFile 从文件加载缓存
|
||||||
|
func ReadMemoryCacheFromJsonFile() {
|
||||||
|
path := GetMemoryCacheFilePath()
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("mc file not exists:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("mc open file err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var data map[string]MemoryCacheValue
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
if err := decoder.Decode(&data); err != nil {
|
||||||
|
log.Println("mc decode file err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 过滤已过期的数据
|
||||||
|
now := time.Now().Unix()
|
||||||
|
memoryCacheData.mu.Lock()
|
||||||
|
for k, v := range data {
|
||||||
|
if v.ExpireAt > now || v.ExpireAt == math.MaxInt64 {
|
||||||
|
memoryCacheData.data[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memoryCacheData.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提供全局缓存的访问方法
|
||||||
|
func GetGlobalCache() *MemoryCache {
|
||||||
|
return memoryCacheData
|
||||||
|
}
|
||||||
|
|
@ -1,16 +1,14 @@
|
||||||
package worker
|
package worker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
)
|
)
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/go-redis/redis/v8"
|
|
||||||
)
|
|
||||||
|
|
||||||
var RedisClient *redis.Client // Redis 客户端, 用于连接 Redis 服务器
|
var RedisClient *redis.Client // Redis 客户端, 用于连接 Redis 服务器
|
||||||
func InitRedis() error {
|
func InitRedis() error {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue