1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package main
- import (
- "flag"
- "fmt"
- "net/http"
- "github.com/DeanThompson/ginpprof"
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- "github.com/gorilla/websocket"
- "github.com/redis/go-redis/v9"
- "sikey.com/websocket/config"
- "sikey.com/websocket/repositories"
- "sikey.com/websocket/server"
- "sikey.com/websocket/utils/mysqlx"
- "sikey.com/websocket/utils/zlog"
- )
- var configFile = flag.String("f", "./etc/websocket.toml", "the config file")
- func main() {
- flag.Parse()
- config.MustLoadConfig(*configFile)
- // Zaplog init
- zlog.WithZapLogger(zlog.NewLogger(config.MustLoadLogger()))
- app := newApp()
- app.Run(fmt.Sprintf(":%d", config.Config.Port))
- }
- func newApp() *gin.Engine {
- app := gin.Default()
- ginpprof.Wrap(app)
- hub := server.NewHub(server.HubConfig{
- ServerId: uuid.NewString(),
- Rdb: redis.NewUniversalClient(&redis.UniversalOptions{
- Addrs: []string{"106.75.230.4:6379"},
- Password: "sikey!Q@W#E456",
- DB: 0,
- }),
- })
- srv := &server.Server{
- Upgrader: websocket.Upgrader{
- ReadBufferSize: config.Websocket.ReadBufferSize,
- WriteBufferSize: config.Websocket.WriteBufferSize,
- CheckOrigin: func(r *http.Request) bool {
- return true
- },
- },
- Hub: hub,
- Repositories: repositories.NewRepositories(mysqlx.ConnectMysql()),
- }
- app.GET("/websocket/endpoint", func(ctx *gin.Context) { server.WebsocketHandler(ctx, srv) })
- app.GET("/index", func(ctx *gin.Context) {
- clients := hub.GetClients()
- ctx.JSON(http.StatusOK, gin.H{
- "clients": len(clients),
- })
- })
- return app
- }
|