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 } } m.Broadcast([]byte(metadata)) history.Value = metadata history = history.Next() }() }) m.HandleConnect(func(s *melody.Session) { for j := 0; j < 10; j++ { if history.Value != nil { s.Write([]byte(history.Value.(string))) } history = history.Next() } }) r.Run(":1338") }