forked from rfa/radioweb
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"container/ring"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"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.GET("/", func(c *gin.Context) {
|
|
http.ServeFile(c.Writer, c.Request, "index.html")
|
|
})
|
|
|
|
r.GET("/ws", func(c *gin.Context) {
|
|
m.HandleRequest(c.Writer, c.Request)
|
|
})
|
|
|
|
r.POST("/update", func(c *gin.Context) {
|
|
artist := c.PostForm("artist")
|
|
title := c.PostForm("title")
|
|
if artist == "" || title == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "failure",
|
|
})
|
|
return
|
|
}
|
|
metadata := fmt.Sprintf("%s - %s", artist, title)
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "success",
|
|
})
|
|
|
|
for j := 0; j < 10; j++ {
|
|
if history.Value == metadata {
|
|
history = history.Next()
|
|
return
|
|
}
|
|
history = history.Next()
|
|
}
|
|
|
|
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")
|
|
}
|