Compare commits
No commits in common. "2b2035b4df6dd7745a300b9f99c4d3ca4cf0d0e9" and "36e0fdb7974568ca85fc751d96d90f545a315f79" have entirely different histories.
2b2035b4df
...
36e0fdb797
|
|
@ -13,7 +13,6 @@ type User struct {
|
||||||
Email string `gorm:"column:email"`
|
Email string `gorm:"column:email"`
|
||||||
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"`
|
|
||||||
CreateTime string `gorm:"column:create_time"`
|
CreateTime string `gorm:"column:create_time"`
|
||||||
UpdateTime string `gorm:"column:update_time"`
|
UpdateTime string `gorm:"column:update_time"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,10 +108,3 @@ func QuashAllDelay(user_id int, day int) int {
|
||||||
}
|
}
|
||||||
return int(res.RowsAffected)
|
return int(res.RowsAffected)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取视频列表分页
|
|
||||||
func GetVideoListByPage(auth_id, page, pageSize int) []Video {
|
|
||||||
var videos []Video
|
|
||||||
DB.Debug().Where("auth_id = ? and isdelete = ?", auth_id, 0).Order("create_time DESC").Offset((page - 1) * pageSize).Limit(pageSize).Find(&videos) //Offset((page - 1) * pageSize).Limit(pageSize),分页,page从1开始,pageSize每页多少条,Offset是偏移量,Limit是限制条数
|
|
||||||
return videos
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -60,8 +60,40 @@ func RunCID(c *gin.Context) {
|
||||||
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)
|
strs := strings.Split(cid.Url, "/")
|
||||||
|
name := strs[len(strs)-1]
|
||||||
|
names := strings.Split(name, ".")
|
||||||
|
name = names[0]
|
||||||
|
|
||||||
|
//脚本内容,不同用户的持续集成、部署目录不同
|
||||||
|
scriptContent := `
|
||||||
|
echo "start"
|
||||||
|
TARGET_DIR=` + proto.CID_BASE_DIR + username.(string) + `/workspace/` + name + `
|
||||||
|
if [ ! -d $TARGET_DIR ]; then
|
||||||
|
git clone ` + cid.Url + `
|
||||||
|
cd $TARGET_DIR
|
||||||
|
else
|
||||||
|
cd $TARGET_DIR
|
||||||
|
git pull
|
||||||
|
fi
|
||||||
|
` + cid.Script + `
|
||||||
|
pwd
|
||||||
|
ls -lh
|
||||||
|
echo "end"`
|
||||||
|
//执行脚本
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
fmt.Println("bash content:", scriptContent)
|
||||||
|
dao.CreateRunLog(req.ID, authID, scriptContent, out.String(), err3_info) //添加执行日志
|
||||||
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 {
|
||||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
|
@ -162,7 +194,6 @@ 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)
|
||||||
if token == "" || cid == 0 {
|
if token == "" || cid == 0 {
|
||||||
|
|
@ -171,36 +202,40 @@ func CIDCallback(c *gin.Context) {
|
||||||
}
|
}
|
||||||
res := dao.FindCIDByIDAndToken(cid, token)
|
res := dao.FindCIDByIDAndToken(cid, token)
|
||||||
if res.ID != 0 {
|
if res.ID != 0 {
|
||||||
user := dao.FindUserByID(res.Auth_id)
|
//从url获取仓库名称
|
||||||
go RunShell(user[0].Name, res.Url, res.Script, int(res.ID), res.Auth_id)
|
strs := strings.Split(res.Url, "/")
|
||||||
|
name := strs[len(strs)-1]
|
||||||
|
names := strings.Split(name, ".")
|
||||||
|
name = names[0]
|
||||||
|
user := dao.FindUserByUserID(res.Auth_id)
|
||||||
|
scriptContent := `
|
||||||
|
echo "start"
|
||||||
|
TARGET_DIR=` + proto.CID_BASE_DIR + user.Name + `workspace/` + name + `
|
||||||
|
if [ ! -d $TARGET_DIR ]; then
|
||||||
|
git clone ` + res.Url + `
|
||||||
|
cd $TARGET_DIR
|
||||||
|
else
|
||||||
|
cd $TARGET_DIR
|
||||||
|
git pull
|
||||||
|
fi
|
||||||
|
` + res.Script + `
|
||||||
|
pwd
|
||||||
|
ls -lh
|
||||||
|
echo "end"`
|
||||||
|
//执行脚本
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
dao.CreateRunLog(cid, res.Auth_id, scriptContent, out.String(), err3_info) //添加执行日志
|
||||||
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 {
|
||||||
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
|
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunShell(username, url, script string, id, authID int) {
|
|
||||||
strs := strings.Split(url, "/")
|
|
||||||
name := strs[len(strs)-1]
|
|
||||||
names := strings.Split(name, ".")
|
|
||||||
name = names[0]
|
|
||||||
|
|
||||||
//脚本内容,不同用户的持续集成、部署目录不同
|
|
||||||
scriptContent := `
|
|
||||||
echo "start"
|
|
||||||
` + script + `
|
|
||||||
echo "end"`
|
|
||||||
//执行脚本
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
fmt.Println("bash content:", scriptContent)
|
|
||||||
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info) //添加执行日志
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,11 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
"videoplayer/service"
|
"videoplayer/service"
|
||||||
"videoplayer/worker"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeviceAddReq struct {
|
type DeviceAddReq struct {
|
||||||
|
|
@ -204,64 +199,3 @@ func Restart(ip string) bool {
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 接收及发送消息
|
|
||||||
func GetRealTimeImage(c *gin.Context) {
|
|
||||||
id, _ := c.Get("id")
|
|
||||||
id1 := int(id.(float64))
|
|
||||||
device_id := c.Query("device_id")
|
|
||||||
//字符串转int
|
|
||||||
device_id_int, _ := strconv.Atoi(device_id)
|
|
||||||
device := service.GetDevice(device_id_int, id1)
|
|
||||||
if device.ID == 0 {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"code": proto.DataNotFound, "message": "device not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//建立连接
|
|
||||||
// 升级HTTP连接为WebSocket连接
|
|
||||||
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
||||||
clients[ws] = true
|
|
||||||
if err != nil {
|
|
||||||
// log.Println(err)
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer ws.Close()
|
|
||||||
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5) //设置播放状态
|
|
||||||
// 接收客户端消息并发送到指定用户
|
|
||||||
|
|
||||||
go func(ws *websocket.Conn, device_id int) {
|
|
||||||
|
|
||||||
}(ws, device_id_int)
|
|
||||||
|
|
||||||
for {
|
|
||||||
if v := clients[ws]; v == true {
|
|
||||||
res2 := worker.PopRedisListLeft(device_id + "_frames")
|
|
||||||
var res3 []byte
|
|
||||||
var msg proto.Message
|
|
||||||
if res2 != "" {
|
|
||||||
//若有消息则发送消息
|
|
||||||
msg.Type = "img"
|
|
||||||
msg.Msg = res2
|
|
||||||
msg.From_user_id = id1
|
|
||||||
res3, _ = json.Marshal(msg)
|
|
||||||
} else {
|
|
||||||
//若无消息则发送心跳包
|
|
||||||
msg.Type = "check"
|
|
||||||
msg.Msg = "check"
|
|
||||||
msg.From_user_id = -1
|
|
||||||
res3, _ = json.Marshal(msg)
|
|
||||||
}
|
|
||||||
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
|
||||||
if err2 != nil {
|
|
||||||
clientsMux.Lock()
|
|
||||||
clients[ws] = false
|
|
||||||
clientsMux.Unlock()
|
|
||||||
//设置ws关闭状态信息
|
|
||||||
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "0", time.Minute*5) //设置播放状态
|
|
||||||
break
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond * 100) //设置延时100ms
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,6 @@ func SRMessage(c *gin.Context) {
|
||||||
}
|
}
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
res := worker.GetRedis(redis_key + "_connection")
|
res := worker.GetRedis(redis_key + "_connection")
|
||||||
worker.SetRedisWithExpire("user_"+strconv.Itoa(id1)+"_status", "1", time.Second*5)
|
|
||||||
if res == "" {
|
if res == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -197,8 +196,8 @@ func SRMessage(c *gin.Context) {
|
||||||
res3, _ = json.Marshal(msg)
|
res3, _ = json.Marshal(msg)
|
||||||
}
|
}
|
||||||
//判断对方是否在线,若不在线则发送离线消息,否则正常发送消息
|
//判断对方是否在线,若不在线则发送离线消息,否则正常发送消息
|
||||||
if worker.IsContainKey("user_"+to_user_id+"_status") == true {
|
if worker.IsContainKey(to_user_id+"_status") == true {
|
||||||
if worker.GetRedis("user_"+to_user_id+"_status") == "0" {
|
if worker.GetRedis(to_user_id+"_status") == "0" {
|
||||||
msg.Type = "offline"
|
msg.Type = "offline"
|
||||||
msg.Msg = "offline"
|
msg.Msg = "offline"
|
||||||
msg.From_user_id = -1
|
msg.From_user_id = -1
|
||||||
|
|
@ -208,9 +207,13 @@ func SRMessage(c *gin.Context) {
|
||||||
}
|
}
|
||||||
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
worker.SetRedisWithExpire("user_"+strconv.Itoa(id1)+"_status", "0", time.Second*120) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
worker.SetRedisWithExpire("user_"+id.(string)+"_status", "0", time.Second*120) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
||||||
|
clientsMux.Lock()
|
||||||
|
delete(clients, ws)
|
||||||
|
clientsMux.Unlock()
|
||||||
|
break
|
||||||
} else {
|
} else {
|
||||||
worker.SetRedisWithExpire("user_"+strconv.Itoa(id1)+"_status", "1", time.Second*5) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
worker.SetRedisWithExpire("user_"+id.(string)+"_status", "1", time.Second*5) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second * 1) // 每1秒查询一次
|
time.Sleep(time.Second * 1) // 每1秒查询一次
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -254,7 +254,7 @@ func registerHandler(c *gin.Context) {
|
||||||
var req_data RLReq
|
var req_data RLReq
|
||||||
tokenString := ""
|
tokenString := ""
|
||||||
var id uint
|
var id uint
|
||||||
if err := c.ShouldBind(&req_data); err == nil {
|
if err := c.ShouldBindJSON(&req_data); err == nil {
|
||||||
if len(req_data.Password) != 32 {
|
if len(req_data.Password) != 32 {
|
||||||
hasher := md5.New()
|
hasher := md5.New()
|
||||||
hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值
|
hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值
|
||||||
|
|
@ -281,7 +281,7 @@ func registerHandler(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "error"})
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(req_data)
|
fmt.Println(req_data)
|
||||||
|
|
@ -296,6 +296,6 @@ func registerHandler(c *gin.Context) {
|
||||||
data["username"] = req_data.User
|
data["username"] = req_data.User
|
||||||
data["email"] = req_data.Email
|
data["email"] = req_data.Email
|
||||||
data["token"] = tokenString
|
data["token"] = tokenString
|
||||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data})
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "date": data})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,16 +68,6 @@ func CreateVideo(videoPath, videoName string, cameraID, authID, human, isDelete
|
||||||
return dao.CreateVideo(videoPath, videoName, cameraID, authID, human, isDelete, createTime, endTime, deleteTime, fileSize)
|
return dao.CreateVideo(videoPath, videoName, cameraID, authID, human, isDelete, createTime, endTime, deleteTime, fileSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVideoListByPage(auth_id, page, page_size int) []dao.Video {
|
|
||||||
if page < 0 {
|
|
||||||
page = 0 //默认第一页
|
|
||||||
}
|
|
||||||
if page_size < 0 || page_size > 100 {
|
|
||||||
page_size = 10 //默认每页10条
|
|
||||||
}
|
|
||||||
return dao.GetVideoListByPage(auth_id, page, page_size)
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteVideo(id, user int) int {
|
func DeleteVideo(id, user int) int {
|
||||||
return dao.DeleteVideoByID(id, user)
|
return dao.DeleteVideoByID(id, user)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue