From 35db85978854b30e0a9fcdcef6dc2821b024a8a4 Mon Sep 17 00:00:00 2001 From: nite07 Date: Thu, 24 Oct 2024 02:35:37 +0800 Subject: [PATCH] u --- config/config.go | 2 +- go.mod | 22 ++-- go.sum | 107 +++--------------- logger/log.go | 7 -- main.go | 46 ++------ server/handler.go | 39 ------- server/server.go | 117 +++++++++++++------- server/static/index.html | 234 ++++++++++++++++++++++++++++++--------- streamer/streamer.go | 21 ++-- websocket/outputer.go | 6 + websocket/websocket.go | 47 ++++++++ websocket_handler.go | 60 ++++++++++ 12 files changed, 416 insertions(+), 292 deletions(-) delete mode 100644 logger/log.go delete mode 100644 server/handler.go create mode 100644 websocket/outputer.go create mode 100644 websocket/websocket.go create mode 100644 websocket_handler.go diff --git a/config/config.go b/config/config.go index 6573586..defc0be 100644 --- a/config/config.go +++ b/config/config.go @@ -44,7 +44,7 @@ type LogConfig struct { } type AuthConfig struct { - Key string `json:"key"` + Token string `json:"token"` } type Config struct { diff --git a/go.mod b/go.mod index 5621761..56b6819 100644 --- a/go.mod +++ b/go.mod @@ -2,46 +2,40 @@ module live-streamer go 1.23.2 -require github.com/fsnotify/fsnotify v1.7.0 +require ( + github.com/fsnotify/fsnotify v1.7.0 + github.com/gin-gonic/gin v1.10.0 + github.com/gofrs/uuid/v5 v5.3.0 + github.com/gorilla/websocket v1.5.3 +) require ( - atomicgo.dev/cursor v0.2.0 // indirect - atomicgo.dev/keyboard v0.2.9 // indirect - atomicgo.dev/schedule v0.1.0 // indirect github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect - github.com/containerd/console v1.0.3 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect - github.com/gin-gonic/gin v1.10.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/goccy/go-json v0.10.2 // indirect - github.com/gookit/color v1.5.4 // indirect - github.com/gorilla/websocket v1.5.3 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/kr/pretty v0.1.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect - github.com/lithammer/fuzzysearch v1.1.8 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.15 // 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/pterm/pterm v0.12.79 // indirect - github.com/rivo/uniseg v0.4.4 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect - github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // 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/term v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index d3a4e91..e8b1f09 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,3 @@ -atomicgo.dev/cursor v0.2.0 h1:H6XN5alUJ52FZZUkI7AlJbUc1aW38GWZalpYRPpoPOw= -atomicgo.dev/cursor v0.2.0/go.mod h1:Lr4ZJB3U7DfPPOkbH7/6TOtJ4vFGHlgj1nc+n900IpU= -atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8= -atomicgo.dev/keyboard v0.2.9/go.mod h1:BC4w9g00XkxH/f1HXhW2sXmJFOCWbKn9xrOunSFtExQ= -atomicgo.dev/schedule v0.1.0 h1:nTthAbhZS5YZmgYbb2+DH8uQIZcTlIrd4eYr3UQxEjs= -atomicgo.dev/schedule v0.1.0/go.mod h1:xeUa3oAkiuHYh8bKiQBRojqAMq3PXXbJujjb0hw8pEU= -github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs= -github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8= -github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII= -github.com/MarvinJWendt/testza v0.2.10/go.mod h1:pd+VWsoGUiFtq+hRKSU1Bktnn+DMCSrDrXDpX2bG66k= -github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI= -github.com/MarvinJWendt/testza v0.3.0/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c= -github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYewEjXsvsVUPbE= -github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk= 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= @@ -20,9 +6,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/ 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/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= -github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= 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/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= @@ -32,6 +17,8 @@ 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= @@ -40,33 +27,28 @@ github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBEx github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= 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/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk= +github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= +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/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= -github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= -github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= -github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 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.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= -github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= 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/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 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/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4= -github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4= 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-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 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= @@ -74,106 +56,45 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 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/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI= -github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg= -github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE= -github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEejaWgXU= -github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE= -github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8= -github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s= -github.com/pterm/pterm v0.12.79 h1:lH3yrYMhdpeqX9y5Ep1u7DejyHy7NSQg9qrBjF9dFT4= -github.com/pterm/pterm v0.12.79/go.mod h1:1v/gzOF1N0FsjbgTHZ1wVycRkKiatFvJSJC4IGaQAAo= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= -github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= 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.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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= -github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= -github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= -github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 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.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 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/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= -golang.org/x/sys v0.4.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.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= -golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= -golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= -golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/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= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/logger/log.go b/logger/log.go deleted file mode 100644 index f62deb6..0000000 --- a/logger/log.go +++ /dev/null @@ -1,7 +0,0 @@ -package logger - -type Logger interface { - Print(v ...interface{}) - Println(v ...interface{}) - Printf(format string, v ...interface{}) -} diff --git a/main.go b/main.go index 5003d60..8a9bd89 100644 --- a/main.go +++ b/main.go @@ -2,70 +2,44 @@ package main import ( "live-streamer/config" - "live-streamer/logger" "live-streamer/server" "live-streamer/streamer" "live-streamer/utils" + "live-streamer/websocket" "log" - "os" - "strings" "github.com/fsnotify/fsnotify" ) var GlobalStreamer *streamer.Streamer -var Logger logger.Logger +var outputer websocket.Outputer func main() { - server.NewServer(":8080", input) + server.NewServer(":8080", websocketRequestHandler) server.GlobalServer.Run() - Logger = server.GlobalServer + outputer = server.GlobalServer if !utils.HasFFMPEG() { log.Fatal("ffmpeg not found") } - GlobalStreamer = streamer.NewStreamer(config.GlobalConfig.VideoList, Logger) + GlobalStreamer = streamer.NewStreamer(config.GlobalConfig.VideoList, outputer) go startWatcher() GlobalStreamer.Stream() GlobalStreamer.Close() } -func input(msg string) { - switch msg { - case "prev": - GlobalStreamer.Prev() - case "next": - GlobalStreamer.Next() - case "quit": - GlobalStreamer.Close() - os.Exit(0) - case "list": - list := GlobalStreamer.GetVideoListPath() - Logger.Println("\nvideo list:\n", strings.Join(list, "\n")) - case "current": - videoPath, err := GlobalStreamer.GetCurrentVideoPath() - if err != nil { - Logger.Println("current video: none") - } - Logger.Println("current video: ", videoPath) - default: - Logger.Println("unknown command") - } -} - func startWatcher() { watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatalf("failed to create watcher: %v", err) } defer watcher.Close() - for _, item := range config.GlobalConfig.InputItems { if item.ItemType == "dir" { err = watcher.Add(item.Path) if err != nil { log.Fatalf("failed to add dir to watcher: %v", err) } - Logger.Println("watching dir:", item.Path) + log.Println("watching dir:", item.Path) } } if err != nil { @@ -80,19 +54,21 @@ func startWatcher() { } if event.Op&fsnotify.Create == fsnotify.Create { if utils.IsSupportedVideo(event.Name) { - Logger.Println("new video added:", event.Name) + log.Println("new video added:", event.Name) GlobalStreamer.Add(event.Name) + server.GlobalServer.Broadcast(websocket.MakeResponse(websocket.TypeAddVideo, true, event.Name, "")) } } if event.Op&fsnotify.Remove == fsnotify.Remove { - Logger.Println("video removed:", event.Name) + server.GlobalServer.Broadcast(websocket.MakeResponse(websocket.TypeRemoveVideo, true, event.Name, "")) + log.Println("video removed:", event.Name) GlobalStreamer.Remove(event.Name) } case err, ok := <-watcher.Errors: if !ok { return } - Logger.Println("watcher error:", err) + log.Println("watcher error:", err) } } } diff --git a/server/handler.go b/server/handler.go deleted file mode 100644 index 31976b7..0000000 --- a/server/handler.go +++ /dev/null @@ -1,39 +0,0 @@ -package server - -import ( - "live-streamer/streamer" - "net/http" - - "github.com/gin-gonic/gin" -) - -func GetCurrentVideo(c *gin.Context) { - type response struct { - Success bool `json:"success"` - Data string `json:"data"` - Message string `json:"message"` - } - videoPath, err := streamer.GlobalStreamer.GetCurrentVideoPath() - if err != nil { - c.JSON(http.StatusOK, response{ - Success: false, - Message: err.Error(), - }) - } - c.JSON(http.StatusOK, response{ - Success: true, - Data: videoPath, - }) -} - -func GetVideoList(c *gin.Context) { - type response struct { - Success bool `json:"success"` - Data []string `json:"data"` - } - list := streamer.GlobalStreamer.GetVideoListPath() - c.JSON(http.StatusOK, response{ - Success: true, - Data: list, - }) -} diff --git a/server/server.go b/server/server.go index aec7888..1edbc80 100644 --- a/server/server.go +++ b/server/server.go @@ -2,12 +2,16 @@ package server import ( "embed" - "fmt" "html/template" + "live-streamer/config" + mywebsocket "live-streamer/websocket" "log" "net/http" + "sync" + "time" "github.com/gin-gonic/gin" + uuid "github.com/gofrs/uuid/v5" "github.com/gorilla/websocket" ) @@ -20,18 +24,20 @@ var upgrader = websocket.Upgrader{ }, } -type InputFunc func(string) +type InputFunc func(mywebsocket.Response) type Server struct { addr string - outputChan chan string dealInputFunc InputFunc - clients []*Client + clients map[string]*Client historyOutput string + mu sync.Mutex } type Client struct { + id string conn *websocket.Conn + mu sync.Mutex } var GlobalServer *Server @@ -39,22 +45,21 @@ var GlobalServer *Server func NewServer(addr string, dealInputFunc InputFunc) { GlobalServer = &Server{ addr: addr, - outputChan: make(chan string), dealInputFunc: dealInputFunc, + clients: make(map[string]*Client), + historyOutput: "", } } func (s *Server) Run() { - router := gin.Default() + router := gin.New() tpl, err := template.ParseFS(staticFiles, "static/*") if err != nil { log.Fatalf("Error parsing templates: %v", err) } router.SetHTMLTemplate(tpl) - router.GET("/ws", s.handleWebSocket) - router.GET("/video/current", GetCurrentVideo) - router.GET("/video/list", GetVideoList) + router.GET("/ws", AuthMiddleware(), s.handleWebSocket) router.GET( "/", func(c *gin.Context) { c.HTML(200, "index.html", nil) @@ -66,16 +71,6 @@ func (s *Server) Run() { log.Fatalf("Error starting server: %v", err) } }() - - go func() { - for { - output := <-s.outputChan - s.historyOutput += output - for _, client := range s.clients { - _ = client.conn.WriteMessage(websocket.TextMessage, []byte(output)) - } - } - }() } func (s *Server) handleWebSocket(c *gin.Context) { @@ -83,43 +78,89 @@ func (s *Server) handleWebSocket(c *gin.Context) { if err != nil { return } - defer ws.Close() - client := &Client{conn: ws} - s.clients = append(s.clients, client) - _ = client.conn.WriteMessage(websocket.TextMessage, []byte(s.historyOutput)) + + ws.SetCloseHandler(func(code int, text string) error { + return nil + }) + + id, err := uuid.NewV7() + if err != nil { + log.Printf("generating uuid error: %v", err) + return + } + client := &Client{id: id.String(), conn: ws} + s.mu.Lock() + s.clients[client.id] = client + s.mu.Unlock() + // write history output + s.Single(client.id, mywebsocket.MakeOutput(s.historyOutput)) defer func() { - for i, c := range s.clients { - if c == client { - s.clients = append(s.clients[:i], s.clients[i+1:]...) - break - } + client.mu.Lock() + ws.Close() + client.mu.Unlock() + s.mu.Lock() + delete(s.clients, client.id) + s.mu.Unlock() + if r := recover(); r != nil { + log.Printf("webSocket handler panic: %v", r) } }() for { // recive message - _, msg, err := ws.ReadMessage() + client.mu.Lock() + msg := mywebsocket.Response{} + err := ws.ReadJSON(&msg) + client.mu.Unlock() if err != nil { - log.Printf("Websocket reading message error: %v", err) + if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { + log.Printf("websocket error: %v", err) + } break } - s.dealInputFunc(string(msg)) + s.dealInputFunc(msg) } } -func (s *Server) Print(msg ...any) { - s.outputChan <- fmt.Sprint(msg...) +func AuthMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + if config.GlobalConfig.Auth.Token == "" || + c.Query("token") == config.GlobalConfig.Auth.Token { + c.Next() + } else { + c.AbortWithStatus(http.StatusUnauthorized) + } + } } -func (s *Server) Println(msg ...any) { - s.outputChan <- fmt.Sprintln(msg...) +func (s *Server) Broadcast(obj mywebsocket.Response) { + s.mu.Lock() + if obj.Type == mywebsocket.TypeOutput { + s.historyOutput += obj.Data.(string) + } + for _, client := range s.clients { + obj.UserID = client.id + obj.Timestamp = time.Now().UnixMilli() + if err := client.conn.WriteJSON(obj); err != nil { + log.Printf("websocket writing message error: %v", err) + } + } + s.mu.Unlock() } -func (s *Server) Printf(format string, args ...interface{}) { - s.outputChan <- fmt.Sprintf(format, args...) +func (s *Server) Single(userID string, obj mywebsocket.Response) { + s.mu.Lock() + if client, ok := s.clients[userID]; ok { + obj.UserID = userID + obj.Timestamp = time.Now().UnixMilli() + if err := client.conn.WriteJSON(obj); err != nil { + log.Printf("websocket writing message error: %v", err) + } + } + s.mu.Unlock() } func (s *Server) Close() { - close(s.outputChan) + } diff --git a/server/static/index.html b/server/static/index.html index 64cc76f..cecdfc8 100644 --- a/server/static/index.html +++ b/server/static/index.html @@ -25,6 +25,59 @@ overflow: hidden; } + #token-screen { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background: linear-gradient(135deg, #6e8efb, #4a6cf7); + display: flex; + justify-content: center; + align-items: center; + z-index: 1000; + } + + .token-container { + background: white; + padding: 30px; + border-radius: 15px; + box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1); + width: 90%; + max-width: 400px; + } + + .token-container h2 { + margin-bottom: 20px; + color: #333; + text-align: center; + } + + .token-input-group { + margin-bottom: 20px; + } + + .token-input-group input { + width: 100%; + padding: 12px; + border: 2px solid #e0e0e0; + border-radius: 8px; + font-size: 16px; + transition: border-color 0.3s ease; + } + + .token-input-group input:focus { + border-color: #4a6cf7; + outline: none; + } + + #token-error { + color: #dc3545; + font-size: 14px; + margin-top: 10px; + display: none; + } + .container-fluid { flex: 1; display: flex; @@ -255,6 +308,24 @@ +
+
+

访问验证

+
+ +
+ +
访问令牌无效,请重试
+
+
+

Live Streamer

@@ -295,78 +366,131 @@ diff --git a/streamer/streamer.go b/streamer/streamer.go index 1e0d95c..7f42403 100644 --- a/streamer/streamer.go +++ b/streamer/streamer.go @@ -7,7 +7,8 @@ import ( "fmt" "io" "live-streamer/config" - "live-streamer/logger" + "live-streamer/websocket" + "log" "os/exec" "strings" "sync" @@ -21,18 +22,18 @@ type Streamer struct { ctx context.Context cancel context.CancelFunc mu sync.Mutex - logger logger.Logger + outputer websocket.Outputer } var GlobalStreamer *Streamer -func NewStreamer(videoList []config.InputItem, logger logger.Logger) *Streamer { +func NewStreamer(videoList []config.InputItem, outputer websocket.Outputer) *Streamer { GlobalStreamer = &Streamer{ videoList: videoList, currentVideoIndex: 0, cmd: nil, ctx: nil, - logger: logger, + outputer: outputer, } return GlobalStreamer } @@ -54,7 +55,7 @@ func (s *Streamer) start() { currentVideo := s.videoList[s.currentVideoIndex] videoPath := currentVideo.Path - s.logger.Println("start stream: ", videoPath) + s.outputer.Broadcast(websocket.MakeOutput(fmt.Sprint("start stream: ", videoPath))) s.mu.Lock() s.cmd = exec.CommandContext(s.ctx, "ffmpeg", s.buildFFmpegArgs(currentVideo)...) @@ -62,21 +63,21 @@ func (s *Streamer) start() { pipe, err := s.cmd.StderrPipe() if err != nil { - s.logger.Printf("failed to get pipe: %v", err) + log.Printf("failed to get pipe: %v", err) return } reader := bufio.NewReader(pipe) if err := s.cmd.Start(); err != nil { - s.logger.Printf("starting ffmpeg error: %v\n", err) + s.outputer.Broadcast(websocket.MakeOutput(fmt.Sprintf("starting ffmpeg error: %v\n", err))) return } go s.log(reader) <-s.ctx.Done() - s.logger.Printf("stop stream: %s", videoPath) + s.outputer.Broadcast(websocket.MakeOutput(fmt.Sprintf("stop stream: %s", videoPath))) // stream next video s.currentVideoIndex++ @@ -156,11 +157,11 @@ func (s *Streamer) log(reader *bufio.Reader) { if n > 0 { videoPath, _ := s.GetCurrentVideoPath() buf = append([]byte(videoPath), buf...) - s.logger.Print(string(buf[:n])) + s.outputer.Broadcast(websocket.MakeOutput(string(buf[:n+len(videoPath)]))) } if err != nil { if err != io.EOF { - s.logger.Printf("reading ffmpeg error: %v\n", err) + s.outputer.Broadcast(websocket.MakeOutput(fmt.Sprintf("reading ffmpeg output error: %v\n", err))) } break } diff --git a/websocket/outputer.go b/websocket/outputer.go new file mode 100644 index 0000000..08fa228 --- /dev/null +++ b/websocket/outputer.go @@ -0,0 +1,6 @@ +package websocket + +type Outputer interface { + Broadcast(v Response) + Single(userID string, v Response) +} diff --git a/websocket/websocket.go b/websocket/websocket.go new file mode 100644 index 0000000..0517077 --- /dev/null +++ b/websocket/websocket.go @@ -0,0 +1,47 @@ +package websocket + +type MessageType string + +var ( + TypeOutput MessageType = "Output" + TypeStreamNextVideo MessageType = "StreamNextVideo" + TypeStreamPrevVideo MessageType = "StreamPrevVideo" + TypeGetCurrentVideoPath MessageType = "GetCurrentVideoPath" + TypeGetVideoList MessageType = "GetVideoList" + TypeQuit MessageType = "Quit" + TypeRemoveVideo MessageType = "RemoveVideo" + TypeAddVideo MessageType = "AddVideo" +) + +type Request struct { + Type MessageType `json:"type"` + Args []string `json:"args"` + UserID string `json:"user_id"` + Timestamp int64 `json:"timestamp"` +} + +type Response struct { + Type MessageType `json:"type"` + Success bool `json:"success"` + Data any `json:"data"` + Message string `json:"message"` + UserID string `json:"user_id"` + Timestamp int64 `json:"timestamp"` +} + +func MakeResponse(messageType MessageType, success bool, data any, message string) Response { + return Response{ + Type: messageType, + Success: success, + Data: data, + Message: message, + } +} + +func MakeOutput(output string) Response { + return Response{ + Success: true, + Type: TypeOutput, + Data: output, + } +} diff --git a/websocket_handler.go b/websocket_handler.go new file mode 100644 index 0000000..2829dc5 --- /dev/null +++ b/websocket_handler.go @@ -0,0 +1,60 @@ +package main + +import ( + "live-streamer/server" + "live-streamer/websocket" +) + +func websocketRequestHandler(req websocket.Response) { + if req.UserID == "" { + return + } + var resp websocket.Response + switch websocket.MessageType(req.Type) { + case websocket.TypeStreamNextVideo: + GlobalStreamer.Next() + resp = websocket.Response{ + Type: websocket.TypeStreamNextVideo, + Success: true, + } + server.GlobalServer.Broadcast(resp) + case websocket.TypeStreamPrevVideo: + GlobalStreamer.Prev() + resp = websocket.Response{ + Type: websocket.TypeStreamPrevVideo, + Success: true, + } + server.GlobalServer.Broadcast(resp) + case websocket.TypeGetCurrentVideoPath: + videoPath, err := GlobalStreamer.GetCurrentVideoPath() + if err != nil { + resp = websocket.Response{ + Type: websocket.TypeGetCurrentVideoPath, + Success: false, + Message: err.Error(), + } + } else { + resp = websocket.Response{ + Type: websocket.TypeGetCurrentVideoPath, + Success: true, + Data: videoPath, + } + } + server.GlobalServer.Single(req.UserID, resp) + case websocket.TypeGetVideoList: + resp = websocket.Response{ + Type: websocket.TypeGetVideoList, + Success: true, + Data: GlobalStreamer.GetVideoListPath(), + } + server.GlobalServer.Single(req.UserID, resp) + case websocket.TypeQuit: + server.GlobalServer.Close() + GlobalStreamer.Close() + resp = websocket.Response{ + Type: websocket.TypeQuit, + Success: true, + } + server.GlobalServer.Broadcast(resp) + } +}