Kaynağa Gözat

отдельный файл для handler'ов

toomanysugar 3 yıl önce
ebeveyn
işleme
9042d70992
2 değiştirilmiş dosya ile 175 ekleme ve 178 silme
  1. 174 0
      cmd/web/handlers.go
  2. 1 178
      cmd/web/main.go

+ 174 - 0
cmd/web/handlers.go

@@ -0,0 +1,174 @@
+package main
+
+import (
+	//"html/template"
+	"net/http"
+	"fmt"
+)
+
+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")
+
+	if (dBConnector.LogIn(username, password)){
+
+		session, _ := sessionsStore.Get(r, "session")
+		session.Values["username"] = username
+		session.Values["password"] = password
+
+		session.Save(r,w)
+		fmt.Printf("%s\n%s\n",username,password)
+		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 := r.FormValue("score")
+	fmt.Fprintf(w,"score = %s\n", score)
+	fmt.Printf("score = %s\n", score)
+}

+ 1 - 178
cmd/web/main.go

@@ -6,7 +6,7 @@ import (
 	
 	"os"
 	"time"
-	"fmt"
+	//"fmt"
 	"net/http"
 	"html/template"
 	"github.com/gorilla/mux"
@@ -15,11 +15,6 @@ import (
 
 )
 
-type Page struct {
-    Title string
-    Body  []byte
-}
-
 var (
 	logger	*log.Logger
 	dBConnector connection
@@ -30,172 +25,7 @@ var (
 	gameTemplate = template.Must(template.ParseFiles("ui\\templates\\game.html"))
 )
 
-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")
-
-	if (dBConnector.LogIn(username, password)){
-
-		session, _ := sessionsStore.Get(r, "session")
-		session.Values["username"] = username
-		session.Values["password"] = password
-
-		session.Save(r,w)
-		fmt.Printf("%s\n%s\n",username,password)
-		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 := r.FormValue("score")
-	fmt.Fprintf(w,"score = %s\n", score)
-	fmt.Printf("score = %s\n", score)
-}
 
 func createLogger() {
 	startTime := time.Now()
@@ -209,13 +39,6 @@ func createLogger() {
 
 }
 
-type user struct{
-    idusers int
-    username string
-    password string
-}
-
-
 func main() {