forked from rfa/radioweb
68 lines
1.1 KiB
Go
68 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"container/ring"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
melody "gopkg.in/olahol/melody.v1"
|
|
)
|
|
|
|
func main() {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
r := gin.Default()
|
|
m := melody.New()
|
|
|
|
history := ring.New(10)
|
|
|
|
r.Static("/assets", "./assets")
|
|
r.StaticFile("/", "./index.html")
|
|
|
|
r.GET("/ws", func(c *gin.Context) {
|
|
m.HandleRequest(c.Writer, c.Request)
|
|
})
|
|
|
|
r.POST("/update", func(c *gin.Context) {
|
|
metadata := c.PostForm("metadata")
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
})
|
|
|
|
go func() {
|
|
if metadata == "" {
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(metadata, "/srv/audio/") {
|
|
metadata = strings.Replace(metadata, "/srv/audio/", "", 1)
|
|
}
|
|
|
|
for i := 0; i < history.Len(); i++ {
|
|
history = history.Next()
|
|
if history.Value == metadata {
|
|
return
|
|
}
|
|
}
|
|
|
|
history.Value = metadata
|
|
history = history.Next()
|
|
|
|
m.Broadcast([]byte(metadata))
|
|
}()
|
|
})
|
|
|
|
m.HandleConnect(func(s *melody.Session) {
|
|
for i := 0; i < history.Len(); i++ {
|
|
if history.Value != nil {
|
|
s.Write([]byte(history.Value.(string)))
|
|
}
|
|
history = history.Next()
|
|
}
|
|
})
|
|
|
|
r.Run(":1338")
|
|
}
|