main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. wss "git.alfi.li/gamelang/frontend/wsserver"
  8. gamelangpb "git.alfi.li/gamelang/protobuf/gamelang"
  9. um "git.alfi.li/gamelang/systems/usermanager"
  10. wm "git.alfi.li/gamelang/systems/worldmanager"
  11. )
  12. type ExampleConfig struct {
  13. Http struct {
  14. Listen string // ip:port
  15. }
  16. Kafka struct {
  17. Enabled bool
  18. UserManagerBroker string
  19. WorldManagerBroker string
  20. }
  21. }
  22. func DefaultConfig() ExampleConfig {
  23. config := ExampleConfig{}
  24. config.Http.Listen = "0.0.0.0:8080"
  25. config.Kafka.Enabled = false
  26. return config
  27. }
  28. type exampleWSServer struct {
  29. wsserver wss.WSServer
  30. usermanager um.UserManager
  31. worldmanager wm.WorldManager
  32. }
  33. func (ewss exampleWSServer) RenderHandler() []wss.Handler {
  34. handler := []wss.Handler{}
  35. handler = append(handler, wss.Handler{MType: "echo", Callback: func(msg wss.WSMsg, user gamelangpb.User) (wss.WSMsg, error) {
  36. log.Printf("echo from \"%s\" with \"%s\"", user, msg.Payload)
  37. remsg := wss.WSMsg{Mtype: "echo", Payload: msg.Payload}
  38. return remsg, nil
  39. }})
  40. handler = append(handler, wss.Handler{MType: "world:create", Callback: func(msg wss.WSMsg, user gamelangpb.User) (wss.WSMsg, error) {
  41. _, err := ewss.worldmanager.Create(rand.Uint32(), msg.Payload)
  42. if err != nil {
  43. remsg := wss.WSMsg{Mtype: "world:create", Payload: "failed"}
  44. return remsg, err
  45. }
  46. remsg := wss.WSMsg{Mtype: "world:create", Payload: "success"}
  47. return remsg, nil
  48. }})
  49. handler = append(handler, wss.Handler{MType: "world:remove", Callback: func(msg wss.WSMsg, user gamelangpb.User) (wss.WSMsg, error) {
  50. err := ewss.worldmanager.Remove(msg.Payload)
  51. if err != nil {
  52. remsg := wss.WSMsg{Mtype: "world:remove", Payload: "failed"}
  53. return remsg, err
  54. }
  55. remsg := wss.WSMsg{Mtype: "world:remove", Payload: "success"}
  56. return remsg, nil
  57. }})
  58. handler = append(handler, wss.Handler{MType: "world:join", Callback: func(msg wss.WSMsg, user gamelangpb.User) (wss.WSMsg, error) {
  59. err := ewss.worldmanager.Join(msg.Payload, &user)
  60. if err != nil {
  61. remsg := wss.WSMsg{Mtype: "world:join", Payload: "failed"}
  62. return remsg, err
  63. }
  64. remsg := wss.WSMsg{Mtype: "world:join", Payload: "success"}
  65. return remsg, nil
  66. }})
  67. handler = append(handler, wss.Handler{MType: "world:list", Callback: func(msg wss.WSMsg, user gamelangpb.User) (wss.WSMsg, error) {
  68. log.Print("world:list")
  69. worldlist := ewss.worldmanager.List()
  70. worldlistJSON, err := json.Marshal(worldlist)
  71. if err != nil {
  72. msg := wss.WSMsg{Mtype: "world:list", Payload: "failed"}
  73. return msg, err
  74. }
  75. log.Print("world:list", "sending list")
  76. remsg := wss.WSMsg{Mtype: "world:list", Payload: string(worldlistJSON)}
  77. return remsg, nil
  78. }})
  79. return handler
  80. }
  81. func main() {
  82. brokers := []string{}
  83. usermanager := um.NewUserManager(brokers)
  84. log.Print("usermanager initialized")
  85. worldmanager := wm.NewWorldManager(brokers)
  86. log.Print("worldmanager initialized")
  87. wsserver := wss.NewWSServer([]string{})
  88. ewss := exampleWSServer{usermanager: *usermanager, worldmanager: *worldmanager, wsserver: *wsserver}
  89. ewss.wsserver.RegisterHandler(ewss.RenderHandler())
  90. http.HandleFunc("/ws", ewss.wsserver.WebsocketHandler)
  91. log.Println("websocket listening on", ":8080")
  92. http.ListenAndServe(":8080", nil)
  93. }