package main import ( //"html/template" "net/http" "fmt" "strconv" ) func indexHandler(w http.ResponseWriter, r *http.Request) { data := struct { User string Title string Items []string }{ User: "", Title: "My page", Items: []string{ "My photos", "My blog", "More", }, } session, _ := sessionsStore.Get(r, "session") var ( password string ok3 bool ) untyped, ok := session.Values["username"] if ok { username, ok1 := untyped.(string) if ok1 { data.User = username untyped, ok2 := session.Values["password"] if ok2 { password, ok3 = untyped.(string) } } } indexTemplate.Execute(w, data) if ok3 { w.Write([]byte(password)) } } func indexPostHandler(w http.ResponseWriter, r *http.Request) { fmt.Print("POST") } func logInGetHandler(w http.ResponseWriter, r *http.Request) { data := struct { Username string Password string Error string }{ Username: "", Password: "", Error: "", } logInTemplate.Execute(w, data) } func logInPostHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() username := r.PostForm.Get("username") password := r.PostForm.Get("password") fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm) if (dBConnector.LogIn(username, password)){ session, _ := sessionsStore.Get(r, "session") session.Values["username"] = username session.Values["password"] = password session.Save(r,w) http.Redirect(w, r, "/", http.StatusFound) } else { data := struct { Username string Password string Error string }{ Username: username, Password: password, Error: "Неверный логин либо пароль", } fmt.Printf("Login error") logInTemplate.Execute(w, data) //w.Write([]byte("Login error")) } } func logOutGetHandler(w http.ResponseWriter, r *http.Request) { session, _ := sessionsStore.Get(r, "session") session.Options.MaxAge = -1 session.Save(r, w) untyped, ok := session.Values["username"] if ok { username, ok1 := untyped.(string) if ok1 { logger.Printf("User %s loged out", username) } } http.Redirect(w, r, "/", http.StatusFound) } func signInGetHandler(w http.ResponseWriter, r *http.Request) { signInTemplate.Execute(w, nil) } func signInPostHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() username := r.PostForm.Get("username") password := r.PostForm.Get("password") passwordRepeat := r.PostForm.Get("password-repeat") data := struct { Username string Password string PasswordRepeat string Error string }{ Username: username, Password: password, PasswordRepeat: passwordRepeat, Error: "", } if dBConnector.IsNameUsed(username) { data.Error = "Имя пользователя занято" } else { if (password != passwordRepeat) { data.Error = "Введенные пароли не совпадают" } else { if dBConnector.SigInUser(username,password) { session, _ := sessionsStore.Get(r, "session") session.Values["username"] = username session.Values["password"] = password session.Save(r,w) http.Redirect(w, r, "/", http.StatusFound) } else { data.Error = "Произошла внутреняя ошибка при регистрации нового пользователя" } } } signInTemplate.Execute(w, data) } func gameGetHandler(w http.ResponseWriter, r *http.Request) { gameTemplate.Execute(w, nil) } func gamePostHandler(w http.ResponseWriter, r *http.Request){ //TODO запись score в таблицу if err := r.ParseForm(); err != nil { fmt.Fprintf(w, "ParseForm() err: %v", err) return } fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm) score_str := r.FormValue("score") fmt.Fprintf(w,"score = %s\n", score_str) fmt.Printf("score = %s\n", score_str) score_int, err := strconv.Atoi(score_str) if err != nil { // handle error fmt.Println(err) logger.Printf("Error extracting score from '%s'",score_str) //os.Exit(2) } else { dBConnector.SubmitScore(score_int) } }