2021-03-09 01:13:42 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-13 01:31:32 +01:00
|
|
|
"net/http"
|
2021-03-09 01:13:42 +01:00
|
|
|
"os"
|
|
|
|
|
2021-03-13 01:31:32 +01:00
|
|
|
"github.com/gin-contrib/static"
|
2021-03-09 01:13:42 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-03-13 01:31:32 +01:00
|
|
|
"github.com/gorilla/websocket"
|
2021-03-09 01:13:42 +01:00
|
|
|
)
|
|
|
|
|
2021-03-13 01:31:32 +01:00
|
|
|
var (
|
|
|
|
updateChannel = make(chan bool)
|
|
|
|
upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var triggerUpdate func()
|
|
|
|
|
|
|
|
func serveWs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to set websocket upgrade: %+v", err)
|
|
|
|
return
|
|
|
|
} else {
|
2021-03-16 16:40:40 +01:00
|
|
|
fmt.Println("DEV SOCKET CONNECTED")
|
2021-03-13 01:31:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
triggerUpdate = func() {
|
|
|
|
conn.WriteMessage(websocket.TextMessage, []byte{})
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 01:13:42 +01:00
|
|
|
|
|
|
|
func setupRouter() *gin.Engine {
|
2021-03-13 01:31:32 +01:00
|
|
|
|
2021-03-09 01:13:42 +01:00
|
|
|
r := gin.Default()
|
|
|
|
|
2021-03-13 01:31:32 +01:00
|
|
|
r.GET("/ws", func(c *gin.Context) {
|
|
|
|
serveWs(c.Writer, c.Request)
|
|
|
|
})
|
|
|
|
|
|
|
|
r.POST("/reload", func(c *gin.Context) {
|
|
|
|
triggerUpdate()
|
|
|
|
c.Status(200)
|
|
|
|
c.Done()
|
|
|
|
})
|
|
|
|
|
|
|
|
r.Use(static.Serve("/", static.LocalFile("../view/public", false)))
|
2021-03-09 01:13:42 +01:00
|
|
|
|
|
|
|
return r
|
2021-03-13 01:31:32 +01:00
|
|
|
|
2021-03-09 01:13:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "8080"
|
|
|
|
}
|
|
|
|
|
2021-03-13 01:31:32 +01:00
|
|
|
r := setupRouter()
|
2021-03-09 01:13:42 +01:00
|
|
|
|
|
|
|
r.Run(":" + port)
|
|
|
|
}
|