-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (40 loc) · 1.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"core/visualizer/env"
"core/visualizer/websockethandler"
"text/template"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/sketch.js", func(c *fiber.Ctx) error {
// Get the environment variable or use a default value
socketServer := env.GetEnv("HOST", "127.0.0.1:3000")
// Parse the JavaScript file as a template
tmpl, err := template.ParseFiles("./public/sketch.js")
if err != nil {
return err
}
err = tmpl.Execute(c, map[string]string{
"socket": socketServer,
})
if err != nil {
return err
}
c.Set("Content-Type", "application/javascript")
return c.SendStatus(fiber.StatusOK)
})
app.Static("/", "./public")
app.Use("/ws", func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
if websocket.IsWebSocketUpgrade(c) {
c.Locals("allowed", true)
return c.Next()
}
return fiber.ErrUpgradeRequired
})
app.Get("/ws", websocket.New(websockethandler.HandleWebSocket))
app.Listen(":" + env.GetEnv("PORT", "3000"))
}