websocket.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net/http"
  6. "github.com/DeanThompson/ginpprof"
  7. "github.com/gin-gonic/gin"
  8. "github.com/google/uuid"
  9. "github.com/gorilla/websocket"
  10. "github.com/redis/go-redis/v9"
  11. "sikey.com/websocket/config"
  12. "sikey.com/websocket/repositories"
  13. "sikey.com/websocket/server"
  14. "sikey.com/websocket/utils/mysqlx"
  15. "sikey.com/websocket/utils/zlog"
  16. )
  17. var configFile = flag.String("f", "./etc/websocket.toml", "the config file")
  18. func main() {
  19. flag.Parse()
  20. config.MustLoadConfig(*configFile)
  21. // Zaplog init
  22. zlog.WithZapLogger(zlog.NewLogger(config.MustLoadLogger()))
  23. app := newApp()
  24. app.Run(fmt.Sprintf(":%d", config.Config.Port))
  25. }
  26. func newApp() *gin.Engine {
  27. app := gin.Default()
  28. ginpprof.Wrap(app)
  29. hub := server.NewHub(server.HubConfig{
  30. ServerId: uuid.NewString(),
  31. Rdb: redis.NewUniversalClient(&redis.UniversalOptions{
  32. Addrs: []string{"106.75.230.4:6379"},
  33. Password: "sikey!Q@W#E456",
  34. DB: 0,
  35. }),
  36. })
  37. srv := &server.Server{
  38. Upgrader: websocket.Upgrader{
  39. ReadBufferSize: config.Websocket.ReadBufferSize,
  40. WriteBufferSize: config.Websocket.WriteBufferSize,
  41. CheckOrigin: func(r *http.Request) bool {
  42. return true
  43. },
  44. },
  45. Hub: hub,
  46. Repositories: repositories.NewRepositories(mysqlx.ConnectMysql()),
  47. }
  48. app.GET("/websocket/endpoint", func(ctx *gin.Context) { server.WebsocketHandler(ctx, srv) })
  49. return app
  50. }