68 lines
1.0 KiB
Go
Executable File
68 lines
1.0 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/gin-contrib/static"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
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 {
|
|
fmt.Println("DEV SOCKET CONNECTED")
|
|
}
|
|
|
|
triggerUpdate = func() {
|
|
conn.WriteMessage(websocket.TextMessage, []byte{})
|
|
}
|
|
}
|
|
|
|
func setupRouter() *gin.Engine {
|
|
|
|
r := gin.Default()
|
|
|
|
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)))
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
|
|
r := setupRouter()
|
|
|
|
r.Run(":" + port)
|
|
}
|