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"
|
|
|
|
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 14:35:37 -04:00
|
|
|
type InputFunc func(mywebsocket.Response)
|
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
|
2024-10-23 10:47:37 -04:00
|
|
|
historyOutput string
|
2024-10-23 14:35:37 -04:00
|
|
|
mu sync.Mutex
|
2024-10-23 10:47:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2024-10-23 14:35:37 -04:00
|
|
|
id string
|
2024-10-23 10:47:37 -04:00
|
|
|
conn *websocket.Conn
|
2024-10-23 14:35:37 -04:00
|
|
|
mu sync.Mutex
|
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),
|
|
|
|
historyOutput: "",
|
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
|
|
|
|
}
|
|
|
|
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))
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// recive message
|
2024-10-23 14:35:37 -04:00
|
|
|
client.mu.Lock()
|
|
|
|
msg := mywebsocket.Response{}
|
|
|
|
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 14:35:37 -04:00
|
|
|
s.dealInputFunc(msg)
|
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) {
|
|
|
|
if config.GlobalConfig.Auth.Token == "" ||
|
|
|
|
c.Query("token") == config.GlobalConfig.Auth.Token {
|
|
|
|
c.Next()
|
|
|
|
} else {
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
}
|
2024-10-23 10:47:37 -04:00
|
|
|
}
|
|
|
|
|
2024-10-23 14:35:37 -04:00
|
|
|
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()
|
2024-10-23 10:47:37 -04:00
|
|
|
}
|
|
|
|
|
2024-10-23 14:35:37 -04:00
|
|
|
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()
|
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
|
|
|
}
|