func createAuthBackendFile() (s httpauth.SqlAuthBackend, err error) { if _, err = os.Stat(authBackendFile); os.IsNotExist(err) { _, err = os.Create(authBackendFile) if err != nil { return s, err } } s, err = httpauth.NewSqlAuthBackend("sqlite3", authBackendFile) return s, err }
func main() { var err error // create the backend backend, err = httpauth.NewSqlAuthBackend("mysql", backenddb) if err != nil { panic(err) } defer backend.Close() game = make(map[string]*Buscaminas) players = make(map[int][2]string) connPlayer = make(map[string]*connection) notifications = make(map[string][]int) notif = make(map[int]*Notification) privateGame = make(map[int]bool) // create some default roles roles = make(map[string]httpauth.Role) roles["user"] = 30 roles["admin"] = 80 aaa, err = httpauth.NewAuthorizer(backend, []byte("cookie-encryption-key"), "user", roles) // create a default user hash, err := bcrypt.GenerateFromPassword([]byte("adminadmin"), bcrypt.DefaultCost) if err != nil { panic(err) } defaultUser := httpauth.UserData{Username: "******", Email: "admin@localhost", Hash: hash, Role: "admin"} err = backend.SaveUser(defaultUser) if err != nil { panic(err) } // Handles mux = make(map[string]map[string]func(http.ResponseWriter, *http.Request)) // addRoute(route, handleFunction, methods) mux.addRoute("/", handleHome, []string{"GET", "POST"}) mux.addRoute("/login", getLogin, []string{"GET"}) mux.addRoute("/login", postLogin, []string{"POST"}) mux.addRoute("/register", postRegister, []string{"POST"}) mux.addRoute("/logout", handleLogout, []string{"GET", "POST"}) mux.addRoute("/lobby", handleLobby, []string{"GET", "POST"}) mux.addRoute("/lobby/games", handleLobbyGames, []string{"GET", "POST"}) mux.addRoute("/lobby/players", handleLobbyPlayers, []string{"GET", "POST"}) mux.addRoute("/lobby/challenge", handleLobbyChallenge, []string{"GET", "POST"}) mux.addRoute("/game", handleGame, []string{"GET", "POST"}) mux.addRoute("/game/init", handleGameInit, []string{"GET", "POST"}) mux.addRoute("/game/move", handleGameMove, []string{"GET", "POST"}) mux.addRoute("/game/data", handleGameData, []string{"GET", "POST"}) mux.addRoute("/game/joinGame", handleGameJoinGame, []string{"GET", "POST"}) mux.addRoute("/game/exit", handleGameExit, []string{"GET", "POST"}) mux.addRoute("/misc/notification/get", handleNotificationGet, []string{"GET", "POST"}) mux.addRoute("/misc/notification/reject/game", handleNotificationRejectGame, []string{"GET", "POST"}) mux.addRoute("/misc/notification/seen", handleNotificationSeen, []string{"GET", "POST"}) hub := newHub() go hub.run() filehttp.Handle("/", http.FileServer(http.Dir("../"))) // files wshttp.Handle("/ws", wsHandler{h: hub}) // websocket fmt.Printf("Server running on port %d\n", port) var mh *myHandler go CleanLoggedUsers() http.ListenAndServe(fmt.Sprintf(":%d", port), mh) }
func main() { var err error // create the backend backend, err = httpauth.NewSqlAuthBackend("mysql", backenddb) if err != nil { panic(err) } defer backend.Close() games = make(map[string]*buscaminas2p.Buscaminas) players = make(map[int][2]string) // create some default roles roles = make(map[string]httpauth.Role) roles["user"] = 30 roles["admin"] = 80 aaa, err = httpauth.NewAuthorizer(backend, []byte("cookie-encryption-key"), "user", roles) // create a default user hash, err := bcrypt.GenerateFromPassword([]byte("adminadmin"), bcrypt.DefaultCost) if err != nil { panic(err) } defaultUser := httpauth.UserData{Username: "******", Email: "admin@localhost", Hash: hash, Role: "admin"} err = backend.SaveUser(defaultUser) if err != nil { panic(err) } // Handles mux = make(map[string]map[string]func(http.ResponseWriter, *http.Request)) // addRoute(route, handleFunction, methods) mux.addRoute("/", handleHome, []string{"GET", "POST"}) mux.addRoute("/login", getLogin, []string{"GET"}) mux.addRoute("/login", postLogin, []string{"POST"}) mux.addRoute("/register", postRegister, []string{"POST"}) mux.addRoute("/logout", handleLogout, []string{"GET", "POST"}) mux.addRoute("/lobby", handleLobby, []string{"GET", "POST"}) mux.addRoute("/lobby/games", handleLobbyGames, []string{"GET", "POST"}) mux.addRoute("/lobby/players", handleLobbyPlayers, []string{"GET", "POST"}) mux.addRoute("/game", handleGame, []string{"GET", "POST"}) mux.addRoute("/game/move", handleGameMove, []string{"GET", "POST"}) mux.addRoute("/game/data", handleGameData, []string{"GET", "POST"}) mux.addRoute("/game/joinGame", handleGameJoinGame, []string{"GET", "POST"}) mux.addRoute("/game/exit", handleGameExit, []string{"GET", "POST"}) mux.addRoute("/game/chat", handleGameChat, []string{"GET", "POST"}) filehttp.Handle("/", http.FileServer(http.Dir("../"))) fmt.Printf("Server running on port %d\n", port) var mh *myHandler go CleanLoggedUsers() http.ListenAndServe(fmt.Sprintf(":%d", port), mh) }