live-streamer/server/server.go

173 lines
3.6 KiB
Go
Raw Permalink Normal View History

2024-10-23 10:47:37 -04:00
package server
import (
"embed"
"html/template"
2024-10-23 14:35:37 -04:00
"live-streamer/config"
2024-10-23 17:06:19 -04:00
"live-streamer/streamer"
2024-10-23 14:35:37 -04:00
mywebsocket "live-streamer/websocket"
2024-10-23 10:47:37 -04:00
"log"
"net/http"
2024-10-23 14:35:37 -04:00
"sync"
"time"
2024-10-23 10:47:37 -04:00
"github.com/gin-gonic/gin"
2024-10-23 14:35:37 -04:00
uuid "github.com/gofrs/uuid/v5"
2024-10-23 10:47:37 -04:00
"github.com/gorilla/websocket"
)
//go:embed static
var staticFiles embed.FS
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
2024-10-23 17:06:19 -04:00
type InputFunc func(mywebsocket.RequestType)
2024-10-23 10:47:37 -04:00
type Server struct {
addr string
dealInputFunc InputFunc
2024-10-23 14:35:37 -04:00
clients map[string]*Client
mu sync.Mutex
2024-10-23 10:47:37 -04:00
}
type Client struct {
2024-10-23 17:06:19 -04:00
id string
conn *websocket.Conn
mu sync.Mutex
hasSentSize int
2024-10-23 10:47:37 -04:00
}
var GlobalServer *Server
func NewServer(addr string, dealInputFunc InputFunc) {
GlobalServer = &Server{
addr: addr,
dealInputFunc: dealInputFunc,
2024-10-23 14:35:37 -04:00
clients: make(map[string]*Client),
2024-10-23 10:47:37 -04:00
}
}
func (s *Server) Run() {
2024-10-23 14:35:37 -04:00
router := gin.New()
2024-10-23 10:47:37 -04:00
tpl, err := template.ParseFS(staticFiles, "static/*")
if err != nil {
log.Fatalf("Error parsing templates: %v", err)
}
router.SetHTMLTemplate(tpl)
2024-10-23 14:35:37 -04:00
router.GET("/ws", AuthMiddleware(), s.handleWebSocket)
2024-10-23 10:47:37 -04:00
router.GET(
"/", func(c *gin.Context) {
c.HTML(200, "index.html", nil)
},
)
go func() {
if err := router.Run(s.addr); err != nil {
log.Fatalf("Error starting server: %v", err)
}
}()
}
func (s *Server) handleWebSocket(c *gin.Context) {
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return
}
2024-10-23 14:35:37 -04:00
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
}
2024-10-23 17:06:19 -04:00
client := &Client{id: id.String(), conn: ws, hasSentSize: 0}
2024-10-23 14:35:37 -04:00
s.mu.Lock()
s.clients[client.id] = client
s.mu.Unlock()
2024-10-23 10:47:37 -04:00
defer func() {
2024-10-23 14:35:37 -04:00
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)
2024-10-23 10:47:37 -04:00
}
}()
2024-10-23 17:06:19 -04:00
go func() {
ticker := time.NewTicker(1 * time.Second)
for range ticker.C {
currentVideoPath, _ := streamer.GlobalStreamer.GetCurrentVideoPath()
s.Broadcast(mywebsocket.Date{
Timestamp: time.Now().UnixMilli(),
CurrentVideoPath: currentVideoPath,
VideoList: streamer.GlobalStreamer.GetVideoListPath(),
Output: streamer.GlobalStreamer.GetOutput(),
})
}
}()
2024-10-23 10:47:37 -04:00
for {
// recive message
2024-10-23 14:35:37 -04:00
client.mu.Lock()
2024-10-23 17:06:19 -04:00
msg := mywebsocket.Request{}
2024-10-23 14:35:37 -04:00
err := ws.ReadJSON(&msg)
client.mu.Unlock()
2024-10-23 10:47:37 -04:00
if err != nil {
2024-10-23 14:35:37 -04:00
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("websocket error: %v", err)
}
2024-10-23 10:47:37 -04:00
break
}
2024-10-23 17:06:19 -04:00
s.dealInputFunc(msg.Type)
2024-10-23 10:47:37 -04:00
}
}
2024-10-23 14:35:37 -04:00
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
2024-10-24 04:43:06 -04:00
if config.GlobalConfig.Server.Token == "" ||
c.Query("token") == config.GlobalConfig.Server.Token {
2024-10-23 14:35:37 -04:00
c.Next()
} else {
c.AbortWithStatus(http.StatusUnauthorized)
}
}
2024-10-23 10:47:37 -04:00
}
2024-10-23 17:06:19 -04:00
func (s *Server) Broadcast(obj mywebsocket.Date) {
2024-10-23 14:35:37 -04:00
s.mu.Lock()
for _, client := range s.clients {
obj.Timestamp = time.Now().UnixMilli()
if err := client.conn.WriteJSON(obj); err != nil {
log.Printf("websocket writing message error: %v", err)
}
}
s.mu.Unlock()
2024-10-23 10:47:37 -04:00
}
2024-10-23 17:06:19 -04:00
func (s *Server) Single(userID string, obj mywebsocket.Date) {
2024-10-23 14:35:37 -04:00
s.mu.Lock()
if client, ok := s.clients[userID]; ok {
obj.Timestamp = time.Now().UnixMilli()
if err := client.conn.WriteJSON(obj); err != nil {
log.Printf("websocket writing message error: %v", err)
}
}
s.mu.Unlock()
2024-10-23 10:47:37 -04:00
}
func (s *Server) Close() {
2024-10-23 14:35:37 -04:00
2024-10-23 10:47:37 -04:00
}