Compare commits
36 Commits
9837847d55
...
77401657f5
| Author | SHA1 | Date |
|---|---|---|
|
|
77401657f5 | |
|
|
ec0a266763 | |
|
|
5a87d49be9 | |
|
|
5bf7be12b5 | |
|
|
87af0963fc | |
|
|
6acfb4277e | |
|
|
a54d1f93f1 | |
|
|
44e9beca70 | |
|
|
36e0fdb797 | |
|
|
4178bf0622 | |
|
|
25581bcbf9 | |
|
|
1578be6b4b | |
|
|
a0e2415370 | |
|
|
07d8e6c489 | |
|
|
7d98cb19ae | |
|
|
dc72a8e3a6 | |
|
|
55eaea79be | |
|
|
d4877d283d | |
|
|
fd71cc85c4 | |
|
|
edf94cbca9 | |
|
|
baae696b56 | |
|
|
c3c4668c26 | |
|
|
d0e9e34a65 | |
|
|
daa799dfb5 | |
|
|
cf3985321d | |
|
|
e1e2dc5037 | |
|
|
69a25507e8 | |
|
|
77b76caf39 | |
|
|
f57d9ee392 | |
|
|
c4b96f9a9f | |
|
|
2501db94f5 | |
|
|
7c78231d91 | |
|
|
75a0cb9f3c | |
|
|
22442eb828 | |
|
|
8db7a7fa18 | |
|
|
5af16af2b2 |
|
|
@ -0,0 +1,7 @@
|
||||||
|
#!/bin/bash
|
||||||
|
echo "Building the project"
|
||||||
|
cd /home/videoplayer
|
||||||
|
git pull
|
||||||
|
pwd
|
||||||
|
/home/lijun/go/bin/go build
|
||||||
|
echo "Build complete"
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
package dao
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CID struct {
|
||||||
|
gorm.Model
|
||||||
|
Auth_id int `gorm:"column:auth_id"`
|
||||||
|
Name string `gorm:"column:name"`
|
||||||
|
Url string `gorm:"column:url"`
|
||||||
|
Script string `gorm:"column:script"`
|
||||||
|
Token string `gorm:"column:token"` // 用于外部回调
|
||||||
|
}
|
||||||
|
|
||||||
|
type CIDRunLog struct {
|
||||||
|
gorm.Model
|
||||||
|
CID_id int `gorm:"column:cid_id"`
|
||||||
|
Auth_id int `form:"column:auth_id"`
|
||||||
|
Script string `gorm:"column:script"`
|
||||||
|
Log string `gorm:"column:log"`
|
||||||
|
Error string `gorm:"column:error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateCID 创建持续集成、部署
|
||||||
|
func CreateCID(name, url, script, token string, auth_id int) uint {
|
||||||
|
cid := CID{Name: name, Url: url, Script: script, Token: token, Auth_id: auth_id}
|
||||||
|
result := DB.Debug().Create(&cid)
|
||||||
|
if result.Error != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return cid.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteCIDByID 删除持续集成、部署
|
||||||
|
func DeleteCIDByID(id, auth_id int) bool {
|
||||||
|
res := DB.Debug().Model(&CID{}).Where("id = ? and auth_id = ?", id, auth_id).Delete(&CID{})
|
||||||
|
if res.Error != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindCIDByID 查找持续集成、部署
|
||||||
|
func FindCIDByID(id, auth_id int) CID {
|
||||||
|
var cid CID
|
||||||
|
DB.Debug().Where("id = ? and auth_id = ?", id, auth_id).First(&cid)
|
||||||
|
return cid
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindCIDByAuthID 查找持续集成、部署
|
||||||
|
func FindCIDByAuthID(auth_id int) []CID {
|
||||||
|
var cids []CID
|
||||||
|
DB.Debug().Where("auth_id = ?", auth_id).Find(&cids)
|
||||||
|
return cids
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateCIDByID 更新持续集成、部署
|
||||||
|
func UpdateCIDByID(id, auth_id int, name, url, script, token string) bool {
|
||||||
|
pd := FindCIDByID(id, auth_id)
|
||||||
|
if pd.ID == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// 如果token为空,则不更新token
|
||||||
|
if token == "" {
|
||||||
|
token = pd.Token
|
||||||
|
}
|
||||||
|
result := DB.Debug().Model(&CID{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(CID{Name: name, Url: url, Script: script, Token: token})
|
||||||
|
if result.Error != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateRunLog,添加执行日志
|
||||||
|
func CreateRunLog(cid_id, auth_id int, script, log, err string) uint {
|
||||||
|
cidRunLog := CIDRunLog{CID_id: cid_id, Auth_id: auth_id, Log: log, Error: err, Script: script}
|
||||||
|
result := DB.Debug().Create(&cidRunLog)
|
||||||
|
if result != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return cidRunLog.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindRunLogByAuthID(auth_id int) []CIDRunLog {
|
||||||
|
var cidRunLogs []CIDRunLog
|
||||||
|
DB.Debug().Where(" auth_id = ?", auth_id).Order("created_at desc").Find(&cidRunLogs)
|
||||||
|
return cidRunLogs
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindRunLogByID(auth_id, cid_id int) []CIDRunLog {
|
||||||
|
var cidRunLog []CIDRunLog
|
||||||
|
DB.Debug().Where("cid_id = ? and auth_id = ?", cid_id, auth_id).Order("created_at desc").Find(&cidRunLog)
|
||||||
|
return cidRunLog
|
||||||
|
}
|
||||||
|
func FindRunLogByCIDLogID(id, auth_id int) []CIDRunLog {
|
||||||
|
var cidRunLogs []CIDRunLog
|
||||||
|
DB.Debug().Where("id = ? and auth_id = ?", id, auth_id).Order("created_at desc").Find(&cidRunLogs)
|
||||||
|
return cidRunLogs
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindCIDByIDAndToken(id int, token string) CID {
|
||||||
|
var cid CID
|
||||||
|
DB.Debug().Where("id = ? and token = ?", id, token).First(&cid)
|
||||||
|
return cid
|
||||||
|
}
|
||||||
|
|
@ -32,6 +32,14 @@ func Init() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("logger table:", err)
|
fmt.Println("logger table:", err)
|
||||||
} // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表
|
} // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表
|
||||||
|
err = db.AutoMigrate(&CID{})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("cid table:", err)
|
||||||
|
} // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表
|
||||||
|
err = db.AutoMigrate(&CIDRunLog{})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("cidrunlog table:", err)
|
||||||
|
} // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表
|
||||||
DB = db
|
DB = db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
11
dao/user.go
11
dao/user.go
|
|
@ -19,7 +19,10 @@ type User struct {
|
||||||
|
|
||||||
func CreateUser(name, password, email, gender string, age int) uint {
|
func CreateUser(name, password, email, gender string, age int) uint {
|
||||||
user := User{Name: name, Email: email, Password: password, Gender: gender, Age: age}
|
user := User{Name: name, Email: email, Password: password, Gender: gender, Age: age}
|
||||||
DB.Create(&user)
|
res := DB.Debug().Create(&user)
|
||||||
|
if res.Error != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return user.ID
|
return user.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,6 +37,12 @@ func FindUserByID(id int) []proto.User {
|
||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindUserByUserID(id int) User {
|
||||||
|
var user User
|
||||||
|
DB.Debug().Where("id = ?", id).First(&user)
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
func FindUserByName(name string) User {
|
func FindUserByName(name string) User {
|
||||||
var user User
|
var user User
|
||||||
fmt.Println("name:", name)
|
fmt.Println("name:", name)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,205 @@
|
||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"videoplayer/dao"
|
||||||
|
"videoplayer/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CIDCreateReq struct {
|
||||||
|
Name string `json:"name" form:"name"`
|
||||||
|
Url string `json:"url" form:"url"`
|
||||||
|
Script string `json:"script" form:"script"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CIDDeleteReq struct {
|
||||||
|
ID int `json:"id" form:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CIDRunReq struct {
|
||||||
|
ID int `json:"id" form:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CIDLogReq struct {
|
||||||
|
ID int `json:"id" form:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CIDUpdateReq struct {
|
||||||
|
ID int `json:"id" form:"id"`
|
||||||
|
Name string `json:"name" form:"name"`
|
||||||
|
Url string `json:"url" form:"url"`
|
||||||
|
Script string `json:"script" form:"script"`
|
||||||
|
Token string `json:"cidtoken" form:"cidtoken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetUpCIDGroup(router *gin.Engine) {
|
||||||
|
cidGroup := router.Group("/cid") //持续集成、部署
|
||||||
|
cidGroup.POST("/create", CreateCID)
|
||||||
|
cidGroup.POST("/delete", DeleteCID)
|
||||||
|
cidGroup.POST("/update", UpdateCID)
|
||||||
|
cidGroup.POST("/list", GetCIDList)
|
||||||
|
cidGroup.POST("/run", RunCID)
|
||||||
|
cidGroup.POST("/log", GetCIDLogList) //获取执行日志
|
||||||
|
cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情
|
||||||
|
cidGroup.POST("/callback", CIDCallback)
|
||||||
|
}
|
||||||
|
func RunCID(c *gin.Context) {
|
||||||
|
var req CIDRunReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
// 获取用户ID
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
authID := int(id.(float64))
|
||||||
|
username, _ := c.Get("username")
|
||||||
|
cid := dao.FindCIDByID(req.ID, authID)
|
||||||
|
if cid.ID == 0 {
|
||||||
|
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
go RunShell(username.(string), cid.Url, cid.Script, req.ID, authID)
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateCID(c *gin.Context) {
|
||||||
|
var req CIDCreateReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
// 获取用户ID
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
authID := int(id.(float64))
|
||||||
|
token, _ := generateRandomHexString(32)
|
||||||
|
|
||||||
|
res := dao.CreateCID(req.Name, req.Url, req.Script, token, authID)
|
||||||
|
if res != 0 {
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": res})
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": "CreateCID failed", "code": proto.OperationFailed, "message": "failed"})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteCID(c *gin.Context) {
|
||||||
|
var req CIDDeleteReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
// 获取用户ID
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
authID := int(id.(float64))
|
||||||
|
cid := dao.DeleteCIDByID(req.ID, authID)
|
||||||
|
if cid == false {
|
||||||
|
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateCID(c *gin.Context) {
|
||||||
|
var req CIDUpdateReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
// 获取用户ID
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
authID := int(id.(float64))
|
||||||
|
cid := dao.UpdateCIDByID(req.ID, authID, req.Name, req.Url, req.Script, req.Token)
|
||||||
|
if cid == false {
|
||||||
|
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCIDList(c *gin.Context) {
|
||||||
|
// 获取用户ID
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
authID := int(id.(float64))
|
||||||
|
cids := dao.FindCIDByAuthID(authID)
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cids})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCIDLog(c *gin.Context) {
|
||||||
|
var req CIDRunReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
// 获取用户ID
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
authID := int(id.(float64))
|
||||||
|
cidLogs := dao.FindRunLogByCIDLogID(req.ID, authID)
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCIDLogList(c *gin.Context) {
|
||||||
|
// 获取用户ID
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
authID := int(id.(float64))
|
||||||
|
var req CIDLogReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
cidLogs := dao.FindRunLogByID(authID, req.ID)
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CIDCallback(c *gin.Context) {
|
||||||
|
// 获取用户ID
|
||||||
|
token := c.Query("token")
|
||||||
|
cid_id := c.Query("id")
|
||||||
|
//将cid转换为int
|
||||||
|
cid, _ := strconv.Atoi(cid_id)
|
||||||
|
if token == "" || cid == 0 {
|
||||||
|
c.JSON(200, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res := dao.FindCIDByIDAndToken(cid, token)
|
||||||
|
if res.ID != 0 {
|
||||||
|
username, _ := c.Get("username")
|
||||||
|
go RunShell(username.(string), res.Url, res.Script, int(res.ID), res.Auth_id)
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
|
||||||
|
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) //添加执行日志
|
||||||
|
}
|
||||||
|
|
@ -9,39 +9,39 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeviceAddReq struct {
|
type DeviceAddReq struct {
|
||||||
DeviceName string `json:"device_name" form:"device_name" binding:"required"`
|
DeviceName string `json:"device_name" form:"device_name"`
|
||||||
DeviceIP string `json:"device_ip" form:"device_ip" binding:"required"`
|
DeviceIP string `json:"device_ip" form:"device_ip"`
|
||||||
DeviceStatus string `json:"device_status" form:"device_status" binding:"required"`
|
DeviceStatus string `json:"device_status" form:"device_status"`
|
||||||
AuthID int `json:"auth_id" form:"auth_id" binding:"required"`
|
AuthID int `json:"auth_id" form:"auth_id"`
|
||||||
DeviceInfo string `json:"device_info" form:"device_info" binding:"required"`
|
DeviceInfo string `json:"device_info" form:"device_info"`
|
||||||
DeviceType string `json:"device_type" form:"device_type" binding:"required"`
|
DeviceType string `json:"device_type" form:"device_type"`
|
||||||
DeviceLocation string `json:"device_location" form:"device_location" binding:"required"`
|
DeviceLocation string `json:"device_location" form:"device_location"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceUpdateReq struct {
|
type DeviceUpdateReq struct {
|
||||||
ID int `json:"id" form:"id"`
|
ID int `json:"id" form:"id"`
|
||||||
DeviceName string `json:"device_name" form:"device_name" binding:"required"`
|
DeviceName string `json:"device_name" form:"device_name" `
|
||||||
DeviceIP string `json:"device_ip" form:"device_ip" binding:"required"`
|
DeviceIP string `json:"device_ip" form:"device_ip" `
|
||||||
DeviceStatus string `json:"device_status" form:"device_status" binding:"required"`
|
DeviceStatus string `json:"device_status" form:"device_status" `
|
||||||
AuthID int `json:"auth_id" form:"auth_id" binding:"required"`
|
AuthID int `json:"auth_id" form:"auth_id" `
|
||||||
DeviceInfo string `json:"device_info" form:"device_info" binding:"required"`
|
DeviceInfo string `json:"device_info" form:"device_info" `
|
||||||
DeviceType string `json:"device_type" form:"device_type" binding:"required"`
|
DeviceType string `json:"device_type" form:"device_type" `
|
||||||
DeviceLocation string `json:"device_location" form:"device_location" binding:"required"`
|
DeviceLocation string `json:"device_location" form:"device_location" `
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceStatus struct {
|
type DeviceStatus struct {
|
||||||
IP string `json:"ip" form:"ip" binding:"required"`
|
IP string `json:"ip" form:"ip" `
|
||||||
Status string `json:"status" form:"status" binding:"required"`
|
Status string `json:"status" form:"status" `
|
||||||
ID int `json:"id" form:"id" binding:"required"`
|
ID int `json:"id" form:"id" `
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceDelReq struct {
|
type DeviceDelReq struct {
|
||||||
ID int `json:"id" form:"id" binding:"required"`
|
ID int `json:"id" form:"id" `
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceRestartReq struct {
|
type DeviceRestartReq struct {
|
||||||
ID int `json:"id" form:"id" binding:"required"`
|
ID int `json:"id" form:"id" `
|
||||||
Option string `json:"option" form:"option" binding:"required"`
|
Option string `json:"option" form:"option" `
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetUpDeviceGroup(router *gin.Engine) {
|
func SetUpDeviceGroup(router *gin.Engine) {
|
||||||
|
|
@ -59,7 +59,7 @@ func DeleteDevice(c *gin.Context) {
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
//获取post参数
|
//获取post参数
|
||||||
var req DeviceDelReq
|
var req DeviceDelReq
|
||||||
if err := c.ShouldBindJSON(&req); err == nil {
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
if service.DeleteDevice(req.ID, int(id.(float64))) {
|
if service.DeleteDevice(req.ID, int(id.(float64))) {
|
||||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -73,7 +73,7 @@ func DeleteDevice(c *gin.Context) {
|
||||||
func UpdateDevice(c *gin.Context) {
|
func UpdateDevice(c *gin.Context) {
|
||||||
var req DeviceUpdateReq
|
var req DeviceUpdateReq
|
||||||
user_id, _ := c.Get("id")
|
user_id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&req); err == nil {
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
res := service.UpdateDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, req.ID, int(user_id.(float64)))
|
res := service.UpdateDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, req.ID, int(user_id.(float64)))
|
||||||
if res {
|
if res {
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
|
|
@ -96,7 +96,7 @@ func UpdateDevice(c *gin.Context) {
|
||||||
func SetDeviceStatus(c *gin.Context) {
|
func SetDeviceStatus(c *gin.Context) {
|
||||||
var req DeviceStatus
|
var req DeviceStatus
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&req); err == nil {
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
if req.IP != "" {
|
if req.IP != "" {
|
||||||
if service.SetDeviceStatus(req.Status, req.ID, int(id.(float64))) {
|
if service.SetDeviceStatus(req.Status, req.ID, int(id.(float64))) {
|
||||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
||||||
|
|
@ -112,7 +112,7 @@ func SetDeviceStatus(c *gin.Context) {
|
||||||
func AddDevice(c *gin.Context) {
|
func AddDevice(c *gin.Context) {
|
||||||
var req DeviceAddReq
|
var req DeviceAddReq
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&req); err == nil {
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
user_id := int(id.(float64))
|
user_id := int(id.(float64))
|
||||||
fmt.Println(req)
|
fmt.Println(req)
|
||||||
device_id := service.AddDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, user_id)
|
device_id := service.AddDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, user_id)
|
||||||
|
|
|
||||||
|
|
@ -144,6 +144,7 @@ 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_"+id.(string)+"_status", "1", time.Second*5)
|
||||||
if res == "" {
|
if res == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -178,19 +179,44 @@ func SRMessage(c *gin.Context) {
|
||||||
for {
|
for {
|
||||||
if v := clients[ws]; v == true {
|
if v := clients[ws]; v == true {
|
||||||
res2 := worker.PopRedisListLeft(res + "_" + strconv.Itoa(id1))
|
res2 := worker.PopRedisListLeft(res + "_" + strconv.Itoa(id1))
|
||||||
if res2 != "" {
|
var res3 []byte
|
||||||
var msg proto.Message
|
var msg proto.Message
|
||||||
|
if res2 != "" {
|
||||||
|
//若有消息则发送消息
|
||||||
msg.Type = "msg"
|
msg.Type = "msg"
|
||||||
msg.Msg = res2
|
msg.Msg = res2
|
||||||
msg.From_user_id = id1
|
msg.From_user_id = id1
|
||||||
msg.Session = res
|
msg.Session = res
|
||||||
res3, _ := json.Marshal(msg)
|
res3, _ = json.Marshal(msg)
|
||||||
|
} else {
|
||||||
|
//若无消息则发送心跳包
|
||||||
|
msg.Type = "check"
|
||||||
|
msg.Msg = "check"
|
||||||
|
msg.From_user_id = -1
|
||||||
|
msg.Session = res
|
||||||
|
res3, _ = json.Marshal(msg)
|
||||||
|
}
|
||||||
|
//判断对方是否在线,若不在线则发送离线消息,否则正常发送消息
|
||||||
|
if worker.IsContainKey(to_user_id+"_status") == true {
|
||||||
|
if worker.GetRedis(to_user_id+"_status") == "0" {
|
||||||
|
msg.Type = "offline"
|
||||||
|
msg.Msg = "offline"
|
||||||
|
msg.From_user_id = -1
|
||||||
|
msg.Session = res
|
||||||
|
res3, _ = json.Marshal(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
|
worker.SetRedisWithExpire("user_"+id.(string)+"_status", "0", time.Second*120) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
||||||
|
clientsMux.Lock()
|
||||||
|
delete(clients, ws)
|
||||||
|
clientsMux.Unlock()
|
||||||
break
|
break
|
||||||
|
} else {
|
||||||
|
worker.SetRedisWithExpire("user_"+id.(string)+"_status", "1", time.Second*5) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
||||||
}
|
}
|
||||||
}
|
time.Sleep(time.Second * 1) // 每1秒查询一次
|
||||||
time.Sleep(time.Second * 1) // 每100毫秒查询一次
|
|
||||||
} else {
|
} else {
|
||||||
clientsMux.Lock()
|
clientsMux.Lock()
|
||||||
delete(clients, ws)
|
delete(clients, ws)
|
||||||
|
|
|
||||||
|
|
@ -253,7 +253,8 @@ func loginHandler(c *gin.Context) {
|
||||||
func registerHandler(c *gin.Context) {
|
func registerHandler(c *gin.Context) {
|
||||||
var req_data RLReq
|
var req_data RLReq
|
||||||
tokenString := ""
|
tokenString := ""
|
||||||
if err := c.ShouldBindJSON(&req_data); err == nil {
|
var id uint
|
||||||
|
if err := c.ShouldBind(&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 散列值
|
||||||
|
|
@ -263,7 +264,11 @@ func registerHandler(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"error": "user already exists", "code": proto.UsernameExists, "message": "error"})
|
c.JSON(200, gin.H{"error": "user already exists", "code": proto.UsernameExists, "message": "error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
id := service.CreateUser(req_data.User, req_data.Password, req_data.Email, req_data.Gender, req_data.Age)
|
id = service.CreateUser(req_data.User, req_data.Password, req_data.Email, req_data.Gender, req_data.Age)
|
||||||
|
if id == 0 {
|
||||||
|
c.JSON(200, gin.H{"error": "create user error", "code": proto.OperationFailed, "message": "error"})
|
||||||
|
return
|
||||||
|
}
|
||||||
// 生成 JWT 令牌
|
// 生成 JWT 令牌
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"username": req_data.User,
|
"username": req_data.User,
|
||||||
|
|
@ -273,9 +278,11 @@ func registerHandler(c *gin.Context) {
|
||||||
tokenString, err = token.SignedString(signingKey)
|
tokenString, err = token.SignedString(signingKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenGenerationError, "message": "error"})
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenGenerationError, "message": "error"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "error"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(req_data)
|
fmt.Println(req_data)
|
||||||
res := worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
res := worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||||
|
|
@ -284,5 +291,11 @@ func registerHandler(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 返回令牌
|
// 返回令牌
|
||||||
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": proto.SuccessCode, "message": "success"})
|
data := make(map[string]interface{})
|
||||||
|
data["id"] = id
|
||||||
|
data["username"] = req_data.User
|
||||||
|
data["email"] = req_data.Email
|
||||||
|
data["token"] = tokenString
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
main.go
11
main.go
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt"
|
"github.com/golang-jwt/jwt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
"videoplayer/handler"
|
"videoplayer/handler"
|
||||||
|
|
@ -16,6 +17,7 @@ var signingKey = []byte(proto.TOKEN_SECRET)
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
dao.Init()
|
dao.Init()
|
||||||
worker.InitRedis()
|
worker.InitRedis()
|
||||||
r.Use(handler.CrosHandler())
|
r.Use(handler.CrosHandler())
|
||||||
|
|
@ -24,10 +26,17 @@ func main() {
|
||||||
handler.SetUpUserGroup(r) // User
|
handler.SetUpUserGroup(r) // User
|
||||||
handler.SetUpDeviceGroup(r) // Device
|
handler.SetUpDeviceGroup(r) // Device
|
||||||
handler.SetUpIMGroup(r) // IM
|
handler.SetUpIMGroup(r) // IM
|
||||||
|
handler.SetUpCIDGroup(r) // CID,持续集成、部署
|
||||||
r.Run(":8083") // listen and serve on 0.0.0.0:8082
|
r.Run(":8083") // listen and serve on 0.0.0.0:8082
|
||||||
defer dao.Close()
|
defer dao.Close()
|
||||||
defer worker.CloseRedis()
|
defer worker.CloseRedis()
|
||||||
}
|
}
|
||||||
|
func init() {
|
||||||
|
// 创建cid的目录
|
||||||
|
os.MkdirAll(proto.CID_BASE_DIR, os.ModePerm)
|
||||||
|
os.MkdirAll(proto.CID_BASE_DIR+"script", os.ModePerm)
|
||||||
|
os.MkdirAll(proto.CID_BASE_DIR+"workspace", os.ModePerm)
|
||||||
|
}
|
||||||
|
|
||||||
func writeLogger(c *gin.Context) {
|
func writeLogger(c *gin.Context) {
|
||||||
ip := c.ClientIP()
|
ip := c.ClientIP()
|
||||||
|
|
@ -61,7 +70,7 @@ func JWTAuthMiddleware() gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
//如果请求为login或register,则不需要验证token
|
//如果请求为login或register,则不需要验证token
|
||||||
if strings.Contains(c.Request.URL.Path, "/login") || strings.Contains(c.Request.URL.Path, "/register") || strings.Contains(c.Request.URL.Path, "/uuid") || strings.Contains(c.Request.URL.Path, "/gqr") {
|
if strings.Contains(c.Request.URL.Path, "/login") || strings.Contains(c.Request.URL.Path, "/register") || strings.Contains(c.Request.URL.Path, "/uuid") || strings.Contains(c.Request.URL.Path, "/gqr") || strings.Contains(c.Request.URL.Path, "/cid/callback") {
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@ const (
|
||||||
REIDS_DB = 2
|
REIDS_DB = 2
|
||||||
|
|
||||||
TOKEN_SECRET = "mfjurnc_32ndj9dfhj"
|
TOKEN_SECRET = "mfjurnc_32ndj9dfhj"
|
||||||
|
|
||||||
|
// 以下是持续集成、部署的配置
|
||||||
|
CID_BASE_DIR = "/home/lijun/cid/"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package service
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
)
|
)
|
||||||
|
|
@ -47,7 +48,11 @@ func GetVideoList(auth_id int, start, end, hour string) []dao.Video {
|
||||||
es1 := strings.Split(es[1], ":")
|
es1 := strings.Split(es[1], ":")
|
||||||
end = es[0] + " " + hour + ":" + es1[1] + ":" + es1[2]
|
end = es[0] + " " + hour + ":" + es1[1] + ":" + es1[2]
|
||||||
}
|
}
|
||||||
|
_, err := time.Parse("2006-01-02 15:04:05", start)
|
||||||
|
_, err2 := time.Parse("2006-01-02 15:04:05", end)
|
||||||
|
if err != nil || err2 != nil {
|
||||||
|
return []dao.Video{}
|
||||||
|
}
|
||||||
return dao.FindVideoListByTime(auth_id, start, end)
|
return dao.FindVideoListByTime(auth_id, start, end)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue