55 lines
1.0 KiB
Go
55 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"clickandjoin.app/websocketserver/modules/config"
|
|
"clickandjoin.app/websocketserver/modules/rabbitmq"
|
|
"clickandjoin.app/websocketserver/socketserver"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/websocket/v2"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func init() {
|
|
config.LoadConfig()
|
|
|
|
if config.Cfg.Debug {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
}
|
|
|
|
logrus.Println("Debug:", config.Cfg.Debug)
|
|
|
|
go rabbitmq.Init()
|
|
}
|
|
|
|
func main() {
|
|
app := fiber.New()
|
|
|
|
app.Use("/", 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
|
|
})
|
|
|
|
// wait so that rabbitmq can connect
|
|
// TODO: better way to handle this
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
go socketserver.RunHub()
|
|
|
|
socketserver.WebSocketServer(app)
|
|
|
|
if len(os.Args) < 2 {
|
|
logrus.Fatalln("Please specify port")
|
|
}
|
|
|
|
app.Listen("127.0.0.1:" + os.Args[1])
|
|
}
|