Browse Source

исправления в main.go

toomanysugar 3 years ago
parent
commit
67cf5e2051
1 changed files with 14 additions and 31 deletions
  1. 14 31
      main.go

+ 14 - 31
main.go

@@ -3,7 +3,7 @@ package main
 
 import (
 	"log"
-	"database/sql"
+	
 	"os"
 	"time"
 	"fmt"
@@ -11,8 +11,7 @@ import (
 	"html/template"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/sessions"
-
-	_ "github.com/go-sql-driver/mysql"
+	
 
 )
 
@@ -23,6 +22,7 @@ type Page struct {
 
 var (
 	logger	*log.Logger
+	dBConnector connection
 	sessionsStore = sessions.NewCookieStore([]byte("mysecretcookie"))
 	indexTemplate = template.Must(template.ParseFiles("www\\index.html"))
 	logInTemplate = template.Must(template.ParseFiles("www\\login.html"))
@@ -87,35 +87,18 @@ func logInPostHandler(w http.ResponseWriter, r *http.Request) {
 	username := r.PostForm.Get("username")
 	password := r.PostForm.Get("password")
 
-	//dBConnector.
-	
-	db, err := sql.Open("mysql", "gosite:gositepassword@tcp(192.168.48.5)/gosite")
-
-	if err != nil {
-		fmt.Print("Error")
-		panic(err)
-	}
-
-	defer db.Close()
+	if (dBConnector.LogIn(username, password)){
 
-	var counter int
+		session, _ := sessionsStore.Get(r, "session")
+		session.Values["username"] = username
+		session.Values["password"] = password
 
-	act_query := "SELECT count(*) FROM gosite.users WHERE username='" + username + "' AND password='" + password + "';"
-	db.QueryRow(act_query).Scan(&counter)
-	fmt.Println("we have", counter, "rows")
-    
-    if (counter == 0) {
-    	logInTemplate.Execute(w, nil)
-    	return
-    }
-
-    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)
+		session.Save(r,w)
+		fmt.Printf("%s\n%s\n",username,password)
+		http.Redirect(w, r, "/", http.StatusFound)
+	} else {
+		logInTemplate.Execute(w, nil)
+	}
 }
 
 func signInHandler(w http.ResponseWriter, r *http.Request) {
@@ -158,7 +141,7 @@ func main() {
 	requestRouter := mux.NewRouter()
 
 	
-    
+    dBConnector.Init("config.json")
     
 
     requestRouter.HandleFunc("/", indexHandler).Methods("GET")