123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package main
- import (
- "flag"
- "fmt"
- "net/http"
- "strings"
- "github.com/DeanThompson/ginpprof"
- "github.com/denisbrodbeck/machineid"
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- "github.com/gorilla/websocket"
- "sikey.com/websocket/config"
- "sikey.com/websocket/repositories"
- "sikey.com/websocket/server"
- "sikey.com/websocket/utils/mysqlx"
- "sikey.com/websocket/utils/redisx"
- "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)
- id := serverId()
- srv := &server.Server{
- ID: id,
- Upgrader: websocket.Upgrader{
- ReadBufferSize: config.Websocket.ReadBufferSize,
- WriteBufferSize: config.Websocket.WriteBufferSize,
- CheckOrigin: func(r *http.Request) bool {
- return true
- },
- },
- Hub: server.NewHub(id),
- Repositories: repositories.NewRepositories(mysqlx.ConnectMysql(), redisx.RedisConnect()),
- }
- app.GET("/websocket/endpoint", func(ctx *gin.Context) { srv.WebsocketHandler(ctx) })
- return app
- }
- func serverId() string {
- var id string
- id, err := machineid.ID()
- if err != nil {
- id = uuid.NewString()
- } else {
- id = strings.ToLower(id)
- }
- return id
- }
|