zaliczeniePP/main.go
2018-12-29 16:02:40 +01:00

159 lines
4.3 KiB
Go

package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
//"net/url"
//"time"
_ "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
//***************************************************************************
//#############################################################################
func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("StronaGlowna.gtpl")
t.Execute(w, nil)
}
//#############################################################################
func panel(w http.ResponseWriter, r *http.Request) {
//Here:
//time.Sleep(3 * time.Second)
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")
checkErr(err)
// query
rows, err := db.Query("SELECT * FROM users")
checkErr(err)
//fmt.Fprintf(w, "Lista użytkowników:\n")
for rows.Next() {
err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID, &Login)
checkErr(err)
//fmt.Printf("%d. %s, %s, %d, %d, %s \n", IDusers, User, Password, PIN, RFID, Login)
//fmt.Fprintf(w, "%d. %s \n", IDusers, User)
if r.Method == "POST" {
if Login == loginFROMsite {
if Password == passwordFROMsite {
}
}
}
}
rows.Close()
db.Close()
//----------------------------------
*/
//#############################################################################
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
} else {
r.ParseForm()
//fmt.Println("usernameFROMsite: ", r.Form["usernameFROMsite"])
//fmt.Println("passwordFROMsite: ", r.Form["passwordFROMsite"])
//if len(r.Form["usernameFROMsite"][0]) == 0 {
// goto Here
//}
//if len(r.Form["passwordFROMsite"][0]) == 0 {
// goto Here
//}
}
loginFROMsite := r.FormValue("loginFROMsite")
passwordFROMsite := r.FormValue("passwordFROMsite")
//zaloguj := "Zalogowano!"
//**************************************************************************
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
// query
rows, err := db.Query("SELECT * FROM users")
checkErr(err)
for rows.Next() {
err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID, &Login)
checkErr(err)
//fmt.Printf("%d. %s, %s, %d, %d, %s \n", IDusers, User, Password, PIN, RFID, Login)
//fmt.Fprint(w, IDusers)
//fmt.Fprint(w, ". ")
//fmt.Fprint(w, User)
//fmt.Fprint(w, " \n")
if r.Method == "POST" {
if Login == loginFROMsite {
if Password == passwordFROMsite {
//fmt.Fprintln(w, zaloguj)
//fmt.Fprintln(w, usernameFROMsite)
//time.Sleep(3 * time.Second)
//if zaloguj == "Zalogowano!"
http.Redirect(w, r, "http://localhost:9197/panel", http.StatusSeeOther)
panel(w, r)
break
}
}
}
}
if r.Method == "POST" {
if loginFROMsite != Login {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
}
}
rows.Close()
db.Close()
//**************************************************************************
}
//***************************************************************************
//Bazy danych SQlite
//***************************************************************************
func checkErr(err error) {
if err != nil {
fmt.Println("Błąd")
}
}
//***************************************************************************
//funkcja główna
//***************************************************************************
func main() {
http.HandleFunc("/", opisStołówkaZPM)
http.HandleFunc("/login", login)
http.HandleFunc("/panel", panel)
err := http.ListenAndServe(":9197", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}