commit 218e00c17f9fbafe56fdfbdbb00858737bf8f078 Author: lijun <2021141461138@stu.scu.edu.cn> Date: Sat May 18 17:12:24 2024 +0800 go语言实现视频播放及查询,实现登陆及视频列表获取返回视频功能 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7b4a30d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/videoplayer.iml b/.idea/videoplayer.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/videoplayer.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/dao/db.go b/dao/db.go new file mode 100644 index 0000000..23831b9 --- /dev/null +++ b/dao/db.go @@ -0,0 +1,18 @@ +package dao + +import ( + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func Init() { + dsn := "video:dLPmjweadG625DtH@tcp(114.115.206.93:3306)/video?charset=utf8mb4&parseTime=True&loc=Local" + + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } + DB = db +} diff --git a/dao/user.go b/dao/user.go new file mode 100644 index 0000000..5f8cda9 --- /dev/null +++ b/dao/user.go @@ -0,0 +1,45 @@ +package dao + +type User struct { + ID int `gorm:"primaryKey column:id"` + Name string `gorm:"column:name"` + Age int `gorm:"column:age"` + Email string `gorm:"column:email"` + Password string `gorm:"column:password"` + Gender string `gorm:"column:gender"` + CreateTime string `gorm:"column:create_time"` + UpdateTime string `gorm:"column:update_time"` +} + +func CreateUser(name string, password, email string) int { + user := User{Name: name, Email: email, Password: password} + DB.Create(&user) + return user.ID +} + +func DeleteUserByID(id int) int { + DB.Delete(&User{}, id) + return id +} + +func FindUserByID(id int) User { + var user User + DB.Debug().First(&user, id) + return user +} + +func FindUserByName(name string) User { + var user User + DB.Debug().Where("name = ?", name).First(&user) + return user +} + +func FindUserByEmail(email string) User { + var user User + DB.Debug().Where("email = ?", email).First(&user) + return user +} + +func UpdateUserByID(id int, name, password, email string) { + DB.Model(&User{}).Where("id = ?", id).Updates(User{Name: name, Password: password, Email: email}) +} diff --git a/dao/video.go b/dao/video.go new file mode 100644 index 0000000..c7e937c --- /dev/null +++ b/dao/video.go @@ -0,0 +1,47 @@ +package dao + +import "time" + +type Video struct { + ID int `gorm:"primaryKey column:id"` + CameraID int `gorm:"column:camera_id"` + VideoPath string `gorm:"column:video_path"` + VideoName string `gorm:"column:video_name"` + AuthId int `gorm:"column:auth_id"` + Human int `gorm:"column:human"` + IsDelete int `gorm:"column:is_delete"` + CreateTime string `gorm:"column:create_time"` + EndTime string `gorm:"column:end_time"` + DeleteTime string `gorm:"column:delete_time"` + FileSize int `gorm:"column:file_size"` +} + +func CreateVideo(videoPath, videoName string, authID, human, isDelete int, createTime, endTime string, fileSize int) int { + video := Video{VideoPath: videoPath, VideoName: videoName, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, FileSize: fileSize} + DB.Create(&video) + return video.ID +} + +func DeleteVideoByID(id int) int { + delete_time := time.Now().Format("2006-01-02 15:04:05") + DB.Updates(&Video{IsDelete: 1, DeleteTime: delete_time}).Where("id = ?", id) + return id +} + +func FindVideoByID(id, auth_id int) Video { + var video Video + DB.Debug().Where("id = ? and auth_id = ?", id, auth_id).First(&video) + return video +} + +func FindVideoListsByAuthID(auth_id int) []Video { + var videos []Video + DB.Debug().Where("auth_id = ?", auth_id).Where("is_delete=0").Find(&videos).Limit(30) + return videos +} + +func FindVideoListByTime(auth_id int, startTime, endTime string) []Video { + var videos []Video + DB.Debug().Where("auth_id = ?", auth_id).Where("is_delete=0").Where("create_time > ? and create_time < ?", startTime, endTime).Find(&videos) + return videos +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2533223 --- /dev/null +++ b/go.mod @@ -0,0 +1,45 @@ +module videoplayer + +go 1.21 + +require ( + github.com/gin-gonic/gin v1.10.0 + github.com/go-redis/redis/v8 v8.11.5 + github.com/golang-jwt/jwt v3.2.2+incompatible + gorm.io/driver/mysql v1.5.6 + gorm.io/gorm v1.25.7 +) + +require ( + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..733ac37 --- /dev/null +++ b/go.sum @@ -0,0 +1,119 @@ +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= +github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +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/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/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/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= +github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +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/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.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +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/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +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= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +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/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/handler/device.go b/handler/device.go new file mode 100644 index 0000000..abeebd1 --- /dev/null +++ b/handler/device.go @@ -0,0 +1 @@ +package handler diff --git a/handler/user.go b/handler/user.go new file mode 100644 index 0000000..a56b7a9 --- /dev/null +++ b/handler/user.go @@ -0,0 +1,77 @@ +package handler + +import ( + "fmt" + "github.com/gin-gonic/gin" + "github.com/golang-jwt/jwt" + "time" + "videoplayer/service" + "videoplayer/worker" +) + +var signingKey = []byte("my_secret_key") + +func SetUpUserGroup(router *gin.Engine) { + userGroup := router.Group("/user") + userGroup.POST("/register", registerHandler) + userGroup.POST("/login", loginHandler) +} + +type RLReq struct { + User string `json:"user"` + Email string `json:"email"` + Password string `json:"password"` +} + +func loginHandler(c *gin.Context) { + var req_data RLReq + tokenString := "" + if err := c.ShouldBindJSON(&req_data); err == nil { + user := service.GetUser(req_data.User, req_data.Password) + if user.ID != 0 { + // 生成 JWT 令牌 + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "username": user.Name, + "id": user.ID, + "exp": time.Now().Add(time.Minute * 10).Unix(), // 令牌过期时间, 1分钟后过期 + }) + tokenString, err = token.SignedString(signingKey) + if err != nil { + c.JSON(400, gin.H{"error": err.Error(), "code": 1, "message": "error"}) + return + } + worker.SetRedisWithExpire(req_data.User, tokenString, 600) // 设置过期时间为10分钟 + // 返回令牌 + c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": 0, "message": "success"}) + } else { + c.JSON(400, gin.H{"error": "user not found", "code": 1, "message": "error"}) + } + } else { + c.JSON(400, gin.H{"error": err.Error(), "code": 1, "message": "error"}) + } + +} + +func registerHandler(c *gin.Context) { + var req_data RLReq + tokenString := "" + if err := c.ShouldBindJSON(&req_data); err == nil { + id := service.CreateUser(req_data.User, req_data.Password, req_data.Email) + // 生成 JWT 令牌 + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "username": req_data.User, + "id": id, + "exp": time.Now().Add(time.Minute * 10).Unix(), // 令牌过期时间, 1分钟后过期 + }) + tokenString, err = token.SignedString(signingKey) + if err != nil { + c.JSON(400, gin.H{"error": err.Error()}) + } + } else { + c.JSON(400, gin.H{"error": err.Error()}) + } + fmt.Println(req_data) + worker.SetRedisWithExpire(req_data.User, tokenString, 600) // 设置过期时间为10分钟 + // 返回令牌 + c.JSON(200, gin.H{"token": tokenString, "username": req_data.User}) +} diff --git a/handler/video.go b/handler/video.go new file mode 100644 index 0000000..7568b3e --- /dev/null +++ b/handler/video.go @@ -0,0 +1,90 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "net/http" + "os" + "time" + "videoplayer/service" +) + +type gvlReq struct { + StartTime string `json:"begin"` + EndTime string `json:"end"` + IP string `json:"ip"` + Token string `json:"token"` +} +type videoReq struct { + CameraID int `json:"camera_id"` + VideoPath string `json:"video_path"` + VideoName string `json:"video_name"` + AuthId int `json:"auth_id"` + Human int `json:"human"` + IsDelete int `json:"is_delete"` + CreateTime string `json:"create_time"` + EndTime string `json:"end_time"` + DeleteTime string `json:"delete_time"` + FileSize int `json:"file_size"` +} + +type videoPReq struct { + ID int `json:"id"` + Token string `json:"token"` + AuthId string `json:"userId"` + IP string `json:"ip"` +} + +func SetUpVideoGroup(router *gin.Engine) { + videoGroup := router.Group("/video") + videoGroup.POST("/get_video_list", GetVideoList) + videoGroup.POST("/create", CreateVideo) + videoGroup.GET("/mp4", GetVideo) +} + +func GetVideo(c *gin.Context) { + var vp_req videoPReq + if err := c.ShouldBindQuery(&vp_req); err == nil { + video := service.GetVideo(vp_req.ID, c.GetInt("id")) + name := video.VideoName + path := video.VideoPath + if video.IsDelete == 0 { + // 打开文件 + file, err := os.Open(path + name) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + defer file.Close() + // 设置响应头 + c.Header("Content-Type", "video/mp4") + + // 开始传输文件 + http.ServeContent(c.Writer, c.Request, name, time.Now(), file) // 传输文件, 传输文件的内容类型, 文件名, 文件最后修改时间, 文件 + + // 返回文件 + c.JSON(http.StatusOK, gin.H{"message": "success", "code": 200}) + } else { + c.JSON(http.StatusBadRequest, gin.H{"error": "video is deleted"}) + } + } else { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } +} +func CreateVideo(c *gin.Context) { + var video_req videoReq + if err := c.ShouldBindJSON(&video_req); err == nil { + + } else { + + } +} + +func GetVideoList(c *gin.Context) { + var gvl_req gvlReq + if err := c.ShouldBindJSON(&gvl_req); err == nil { + videos := service.GetVideoList(c.GetInt("id")) + c.JSON(http.StatusOK, gin.H{"videos": videos, "code": 0, "message": "success"}) + } else { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"}) + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..76d4c69 --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "github.com/gin-gonic/gin" + "github.com/golang-jwt/jwt" + "videoplayer/dao" + "videoplayer/handler" + "videoplayer/worker" +) + +var signingKey = []byte("my_secret_key") + +func main() { + + r := gin.Default() + r.Use(JWTAuthMiddleware()) // 使用 JWT 认证中间件 + handler.SetUpVideoGroup(r) + handler.SetUpUserGroup(r) + dao.Init() + worker.InitRedis() + r.Run("8082") // listen and serve on 0.0.0.0:8082 +} + +func JWTAuthMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + // 从请求头中获取 JWT 令牌 + tokenString := c.Request.Header.Get("Authorization") + + //请求方式为get时,从url中获取token + if tokenString == "" { + tokenString = c.Query("token") + } + + //如果请求为login或register,则不需要验证token + if c.Request.URL.Path == "/login" || c.Request.URL.Path == "/register" { + c.Next() + return + } + if tokenString == "" { + c.AbortWithStatus(401) + c.JSON(401, gin.H{ + "message": "Unauthorized", + "error": "token is empty", + "code": "3", + }) + return + } + + // 解析 JWT 令牌 + token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + return signingKey, nil + }) + + // 验证令牌 + if err != nil || !token.Valid { + c.AbortWithStatus(401) + c.JSON(401, gin.H{ + "message": "Unauthorized", + "error": "Invalid token", + "code": "4", + }) + return + } + + // 将用户信息添加到上下文中 + c.Set("id", token.Claims.(jwt.MapClaims)["id"]) + c.Set("username", token.Claims.(jwt.MapClaims)["username"]) + + // 继续处理请求 + c.Next() + } +} diff --git a/service/userService.go b/service/userService.go new file mode 100644 index 0000000..d5e5d1e --- /dev/null +++ b/service/userService.go @@ -0,0 +1,25 @@ +package service + +import ( + "regexp" + "videoplayer/dao" +) + +func CreateUser(name string, password, email string) int { + return dao.CreateUser(name, password, email) +} + +func GetUser(name, password string) dao.User { + emailRegex := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` + re := regexp.MustCompile(emailRegex) + var user dao.User + if re.MatchString(name) { + user = dao.FindUserByEmail(name) + } else { + user = dao.FindUserByName(name) + } + if user.ID != 0 && user.Password == password { + return user + } + return dao.User{} +} diff --git a/service/videoService.go b/service/videoService.go new file mode 100644 index 0000000..52a3c0e --- /dev/null +++ b/service/videoService.go @@ -0,0 +1,11 @@ +package service + +import "videoplayer/dao" + +func GetVideo(id, auth_id int) dao.Video { + return dao.FindVideoByID(id, auth_id) +} + +func GetVideoList(auth_id int) []dao.Video { + return dao.FindVideoListsByAuthID(auth_id) +} diff --git a/worker/redis.go b/worker/redis.go new file mode 100644 index 0000000..ca61115 --- /dev/null +++ b/worker/redis.go @@ -0,0 +1,141 @@ +package worker + +import ( + "log" + "time" +) +import ( + "context" + "encoding/json" + "github.com/go-redis/redis/v8" +) + +var redisClient *redis.Client // Redis 客户端, 用于连接 Redis 服务器 +func InitRedis() { + ctx := context.Background() + // 连接redis + redisClient = redis.NewClient(&redis.Options{ + Addr: "localhost:6379", // Redis 服务器地址 + Password: "", // 如果 Redis 设置了密码 + DB: 0, // 使用的数据库编号 + }) + + // 验证 Redis 客户端是否可以正常工作 + _, err := redisClient.Ping(ctx).Result() + if err != nil { + log.Fatalf("Error connecting to Redis: %v", err) + } +} + +func CloseRedis() { + // 关闭 Redis 客户端 + if err := redisClient.Close(); err != nil { + log.Fatalf("Error closing Redis client: %v", err) + } +} + +func isContainKey(key string) bool { + ctx := context.Background() + val, err := redisClient.Exists(ctx, key).Result() // 检查键是否存在, 如果存在则返回 1, 否则返回 0 + if err != nil { + log.Fatalf("Error getting key: %v", err) + } + if val == 0 { + return false + } + return true +} + +// 设置redis +func SetRedis(key string, value string) { + ctx := context.Background() + // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等 + err := redisClient.Set(ctx, key, value, time.Second*100).Err() + if err != nil { + log.Fatalf("Error setting key: %v", err) + } +} + +// 设置redis +func SetRedisWithExpire(key string, value string, expire time.Duration) { + ctx := context.Background() + // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等 + err := redisClient.Set(ctx, key, value, expire).Err() + if err != nil { + log.Fatalf("Error setting key: %v", err) + } +} + +// 获取redis +func GetRedis(key string) string { + ctx := context.Background() + val, err := redisClient.Get(ctx, key).Result() // 从 Redis 读取键值, 如果键不存在则返回空字符串, 如果出现错误则返回错误 + if err != nil { + log.Fatalf("Error getting key: %v", err) + } + return val +} + +// pop redis list from left +func popRedisList(key string) string { + ctx := context.Background() + val, err := redisClient.LPop(ctx, key).Result() // 从 Redis 读取键值, 如果键不存在则返回空字符串, 如果出现错误则返回错误 + if err != nil { + log.Fatalf("Error reading from Redis: %v", err) + } + return val +} + +// push redis list from right +func pushRedisList(key string, value string) { + ctx := context.Background() + err := redisClient.RPush(ctx, key, value).Err() + if err != nil { + log.Fatalf("Error setting key: %v", err) + } +} + +// delete redis key +func delRedis(key string) { + ctx := context.Background() + err := redisClient.Del(ctx, key).Err() + if err != nil { + log.Fatalf("Error setting key: %v", err) + } +} + +// User 用户,用于存入 Redis hash +type RUser struct { + ID int `json:"id"` + Name string `json:"name"` + Age int `json:"age"` + Email string `json:"email"` +} + +func (u *RUser) toJSONString() string { + // 将User对象编码为JSON字符串 + userJSON, err := json.Marshal(u) + if err != nil { + log.Fatalf("Failed to marshal user: %v", err) + } + return string(userJSON) +} + +// put hash to redis +func hSetRedis(key string, field string, value string) { + ctx := context.Background() + err := redisClient.HSet(ctx, key, field, value).Err() + if err != nil { + log.Fatalf("Error setting key: %v", err) + } +} + +// get hash from redis +func hGetRedis(key string, field string) string { + ctx := context.Background() + val, err := redisClient.HGet(ctx, key, field).Result() + if err != nil { + log.Fatalf("Error getting key: %v", err) + } + return val +}