123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package main
- import (
- "flag"
- "fmt"
- "net/http"
- "strings"
- "time"
- "code.sikey.com.cn/serverbackend/Serverx/dbx"
- "code.sikey.com.cn/serverbackend/Serverx/rdbx"
- "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"
- )
- var configFile = flag.String("f", "./etc/websocket.toml", "the config file")
- func main() {
- time.Local = time.UTC
- flag.Parse()
- config.MustLoadConfig(*configFile)
- app := newApp()
- app.Run(fmt.Sprintf(":%d", config.GetServerPort()))
- }
- 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(dbx.GetConnect(), rdbx.GetConnect()),
- }
- app.GET("/websocket/endpoint", func(ctx *gin.Context) { srv.WebsocketHandler(ctx) })
- app.GET("/websocket/clients", func(ctx *gin.Context) {
- clients := srv.Hub.GetClients()
- ctx.JSON(http.StatusOK, gin.H{"clients": clients})
- })
- 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
- }
|