The server for Open Course World
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

remove global config and dataView vars

mm2srv 4b99c101 b24ec218

+28 -30
+24 -28
main.go
··· 30 30 "golang.org/x/crypto/acme/autocert" 31 31 ) 32 32 33 - // Global variables from config or flags 34 - var TestUsers map[string]string = map[string]string{ 35 - "d4LtgY4xDecIBBUUPVw8Vw": "DevUser", 36 - } 37 - 38 - var TestUsersHashMap map[string]string = map[string]string{} 39 - 40 - func init() { 41 - for userId, _ := range TestUsers { 42 - TestUsersHashMap[fmt.Sprintf("%x", md5.Sum([]byte(userId)))] = userId 43 - } 44 - } 45 - 46 - func NewConnection(conn *websocket_gorilla.Conn, authToken string) { 47 - c := connection.New(conn) 48 - c.State.AuthTokenHash = fmt.Sprintf("%x", md5.Sum([]byte(authToken))) 49 - 33 + func initPrudpServers(c *connection.Connection, dataView view.DataPresenter) error { 34 + // setup prudp authentication server 50 35 auth := &prudp.Port{ 51 36 Conn: c, Port: 1, Type: 10, 52 37 Protocols: map[uint16]rmc.ProtocolHandler{ ··· 55 40 } 56 41 c.AddPort(auth.Port, auth.Type, auth) 57 42 58 - c.State.DeployConfig = cfg 59 - 43 + // mario maker 2 datastore protocol server 60 44 smm2, err := smm2Server.New(c.State, dataView) 61 45 if err != nil { 62 - log.Fatalf("server creation error: %s\n", err) 46 + return err 63 47 } 64 48 49 + // setup prudp application server 65 50 app := &prudp.Port{ 66 51 Conn: c, Port: 2, Type: 10, 67 52 Protocols: map[uint16]rmc.ProtocolHandler{ ··· 72 57 } 73 58 c.AddPort(app.Port, app.Type, app) 74 59 60 + return nil 61 + } 62 + 63 + // Full lifecyle a websocket connection is in here 64 + func WebsocketConnectionHandler(conn *websocket_gorilla.Conn, config *config.Config, dataView view.DataPresenter, authToken string) { 65 + c := connection.New(conn) 66 + c.State.AuthTokenHash = fmt.Sprintf("%x", md5.Sum([]byte(authToken))) 67 + c.State.DeployConfig = config 68 + 69 + if err := initPrudpServers(c, dataView); err != nil { 70 + log.Fatalf("prudp servers creation error: %s\n", err) 71 + } 72 + 75 73 if err := c.Read(); err != nil { 76 74 fmt.Printf("connection error: %s\n", err) 77 75 } ··· 81 79 c.Close() 82 80 } 83 81 84 - var cfg *config.Config 85 - var dataView view.DataPresenter 86 - 87 - func AuthTokenCheckHandler(handler http.Handler) http.Handler { 82 + func AuthTokenCheckHandler(handler http.Handler, dataView view.DataPresenter) http.Handler { 88 83 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 89 84 if r.URL.Path == "/generate_login" { 90 85 handler.ServeHTTP(w, r) ··· 122 117 } 123 118 124 119 func main() { 125 - cfg = config.New() 120 + cfg := config.New() 121 + var dataView view.DataPresenter 126 122 127 123 switch cfg.DebugLevel { 128 124 case "none": ··· 179 175 } 180 176 router.Handle("/", proxy.NewProxy(remote)) 181 177 } else { 182 - ws := websocket.Handler(NewConnection) 178 + ws := websocket.Handler(WebsocketConnectionHandler, cfg, dataView) 183 179 router.HandleFunc("/", ws) 184 180 } 185 181 ··· 235 231 236 232 httpsServer := &http.Server{ 237 233 Addr: cfg.ListenAddressHttps, 238 - Handler: AuthTokenCheckHandler(router), 234 + Handler: AuthTokenCheckHandler(router, dataView), 239 235 //IdleTimeout: 500 * time.Millisecond, // instead of SetKeepAlivesEnabled 240 236 } 241 237 httpsServer.SetKeepAlivesEnabled(false) // otherwise get thumbnail hangs 242 238 243 239 httpServer := &http.Server{ 244 240 Addr: cfg.ListenAddressHttp, 245 - Handler: AuthTokenCheckHandler(routerHttp), 241 + Handler: AuthTokenCheckHandler(routerHttp, dataView), 246 242 //IdleTimeout: 500 * time.Millisecond, // instead of SetKeepAlivesEnabled 247 243 } 248 244 httpServer.SetKeepAlivesEnabled(false) // otherwise get thumbnail hangs
+4 -2
nex/websocket/websocket.go
··· 5 5 "log" 6 6 "net/http" 7 7 "net/http/httputil" 8 + "smm2_gameserver/config" 9 + "smm2_gameserver/db/view" 8 10 9 11 "github.com/gorilla/websocket" 10 12 ) 11 13 12 - func Handler(fn func(*websocket.Conn, string)) func(http.ResponseWriter, *http.Request) { 14 + func Handler(fn func(*websocket.Conn, *config.Config, view.DataPresenter, string), config *config.Config, dataView view.DataPresenter) func(http.ResponseWriter, *http.Request) { 13 15 return func(w http.ResponseWriter, req *http.Request) { 14 16 if false { 15 17 requestDump, err := httputil.DumpRequest(req, true) ··· 47 49 return 48 50 } 49 51 50 - fn(conn, authToken) 52 + fn(conn, config, dataView, authToken) 51 53 } 52 54 }