Update main.go

This commit is contained in:
s441433 2018-12-29 16:02:40 +01:00
parent cbe8c7e451
commit 460c5e5878

88
main.go
View File

@ -7,51 +7,72 @@ import (
"log" "log"
"net/http" "net/http"
//"net/url"
//"time" //"time"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
var IDusers int
var User string
var Password string
var PIN int
var RFID int
var Login string
//*************************************************************************** //***************************************************************************
//funkcje sterujące //funkcje sterujące
//*************************************************************************** //***************************************************************************
//#############################################################################
func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) { func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("StronaGlowna.gtpl") t, _ := template.ParseFiles("StronaGlowna.gtpl")
t.Execute(w, nil) t.Execute(w, nil)
} }
//#############################################################################
func panel(w http.ResponseWriter, r *http.Request) { func panel(w http.ResponseWriter, r *http.Request) {
//Here:
//time.Sleep(3 * time.Second) //time.Sleep(3 * time.Second)
//fmt.Println("method:", r.Method)
//t, _ := template.ParseFiles("panel.gtpl")
//t.Execute(w, nil)
//*************************************************************************** fmt.Println("method:", r.Method)
if r.Method == "GET" {
t, _ := template.ParseFiles("panel.gtpl")
d := struct {
UserView string
}{
UserView: User}
t.ExecuteTemplate(w, "panel.gtpl", d)
}
}
/*----------------------------------
db, err := sql.Open("sqlite3", "stolowkaZPM.db") db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err) checkErr(err)
// query // query
rows, err := db.Query("SELECT * FROM users") rows, err := db.Query("SELECT * FROM users")
checkErr(err) checkErr(err)
var IDusers int //fmt.Fprintf(w, "Lista użytkowników:\n")
var User string
var Password string
var PIN int
var RFID int
fmt.Fprintf(w, "Lista użytkowników:\n")
for rows.Next() { for rows.Next() {
err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID) err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID, &Login)
checkErr(err) checkErr(err)
fmt.Printf("%d. %s, %s, %d, %d \n", IDusers, User, Password, PIN, RFID) //fmt.Printf("%d. %s, %s, %d, %d, %s \n", IDusers, User, Password, PIN, RFID, Login)
fmt.Fprintf(w, "%d. %s \n", IDusers, User) //fmt.Fprintf(w, "%d. %s \n", IDusers, User)
if r.Method == "POST" {
if Login == loginFROMsite {
if Password == passwordFROMsite {
}
}
}
} }
rows.Close() rows.Close()
db.Close() db.Close()
//*************************************************************************** //----------------------------------
} */
//#############################################################################
func login(w http.ResponseWriter, r *http.Request) { func login(w http.ResponseWriter, r *http.Request) {
//Here:
fmt.Println("method:", r.Method) fmt.Println("method:", r.Method)
if r.Method == "GET" { if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl") t, _ := template.ParseFiles("login.gtpl")
@ -67,51 +88,49 @@ func login(w http.ResponseWriter, r *http.Request) {
// goto Here // goto Here
//} //}
} }
usernameFROMsite := r.FormValue("usernameFROMsite") loginFROMsite := r.FormValue("loginFROMsite")
passwordFROMsite := r.FormValue("passwordFROMsite") passwordFROMsite := r.FormValue("passwordFROMsite")
zaloguj := "Zalogowano!" //zaloguj := "Zalogowano!"
//*************************************************************************** //**************************************************************************
db, err := sql.Open("sqlite3", "stolowkaZPM.db") db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err) checkErr(err)
// query // query
rows, err := db.Query("SELECT * FROM users") rows, err := db.Query("SELECT * FROM users")
checkErr(err) checkErr(err)
var IDusers int
var User string
var Password string
var PIN int
var RFID int
for rows.Next() { for rows.Next() {
err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID) err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID, &Login)
checkErr(err) checkErr(err)
//fmt.Printf("%d. %s, %s, %d, %d \n", IDusers, User, Password, PIN, RFID) //fmt.Printf("%d. %s, %s, %d, %d, %s \n", IDusers, User, Password, PIN, RFID, Login)
//fmt.Fprint(w, IDusers) //fmt.Fprint(w, IDusers)
//fmt.Fprint(w, ". ") //fmt.Fprint(w, ". ")
//fmt.Fprint(w, User) //fmt.Fprint(w, User)
//fmt.Fprint(w, " \n") //fmt.Fprint(w, " \n")
if r.Method == "POST" { if r.Method == "POST" {
if User == usernameFROMsite { if Login == loginFROMsite {
if Password == passwordFROMsite { if Password == passwordFROMsite {
fmt.Fprintln(w, zaloguj) //fmt.Fprintln(w, zaloguj)
fmt.Fprintln(w, usernameFROMsite) //fmt.Fprintln(w, usernameFROMsite)
//panel(w, r) //time.Sleep(3 * time.Second)
//if zaloguj == "Zalogowano!"
http.Redirect(w, r, "http://localhost:9197/panel", http.StatusSeeOther)
panel(w, r)
break break
} }
} }
} }
} }
if r.Method == "POST" { if r.Method == "POST" {
if usernameFROMsite != User { if loginFROMsite != Login {
t, _ := template.ParseFiles("login.gtpl") t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil) t.Execute(w, nil)
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! *** Spróbuj ponownie. ***") fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
} }
} }
rows.Close() rows.Close()
db.Close() db.Close()
//*************************************************************************** //**************************************************************************
} }
//*************************************************************************** //***************************************************************************
@ -131,6 +150,7 @@ func main() {
http.HandleFunc("/", opisStołówkaZPM) http.HandleFunc("/", opisStołówkaZPM)
http.HandleFunc("/login", login) http.HandleFunc("/login", login)
http.HandleFunc("/panel", panel) http.HandleFunc("/panel", panel)
err := http.ListenAndServe(":9197", nil) err := http.ListenAndServe(":9197", nil)
if err != nil { if err != nil {
log.Fatal("ListenAndServe: ", err) log.Fatal("ListenAndServe: ", err)