zaliczeniePP/main.go

139 lines
3.7 KiB
Go
Raw Normal View History

2018-11-21 23:47:31 +01:00
package main
2018-11-20 22:17:07 +01:00
2018-12-10 15:52:29 +01:00
import (
2018-12-28 22:10:55 +01:00
"database/sql"
2018-12-10 15:52:29 +01:00
"fmt"
"html/template"
"log"
"net/http"
2018-12-28 22:10:55 +01:00
2018-12-29 04:54:47 +01:00
//"time"
2018-12-28 22:10:55 +01:00
_ "github.com/mattn/go-sqlite3"
2018-12-10 15:52:29 +01:00
)
2018-11-21 23:47:31 +01:00
2018-12-29 00:08:27 +01:00
//***************************************************************************
//funkcje sterujące
//***************************************************************************
2018-12-27 21:58:43 +01:00
func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) {
2018-12-28 14:22:11 +01:00
t, _ := template.ParseFiles("StronaGlowna.gtpl")
2018-12-28 01:48:26 +01:00
t.Execute(w, nil)
2018-12-29 01:36:46 +01:00
}
2018-12-29 00:08:27 +01:00
2018-12-29 01:36:46 +01:00
func panel(w http.ResponseWriter, r *http.Request) {
2018-12-29 04:54:47 +01:00
//time.Sleep(3 * time.Second)
//fmt.Println("method:", r.Method)
//t, _ := template.ParseFiles("panel.gtpl")
//t.Execute(w, nil)
2018-12-29 01:36:46 +01:00
//***************************************************************************
2018-12-29 00:08:27 +01:00
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
// query
rows, err := db.Query("SELECT * FROM users")
checkErr(err)
2018-12-29 01:36:46 +01:00
var IDusers int
2018-12-29 02:31:03 +01:00
var User string
2018-12-29 00:08:27 +01:00
var Password string
var PIN int
2018-12-29 01:36:46 +01:00
var RFID int
2018-12-29 04:54:47 +01:00
fmt.Fprintf(w, "Lista użytkowników:\n")
2018-12-29 00:08:27 +01:00
for rows.Next() {
2018-12-29 02:31:03 +01:00
err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID)
2018-12-29 00:08:27 +01:00
checkErr(err)
2018-12-29 02:31:03 +01:00
fmt.Printf("%d. %s, %s, %d, %d \n", IDusers, User, Password, PIN, RFID)
2018-12-29 04:54:47 +01:00
fmt.Fprintf(w, "%d. %s \n", IDusers, User)
2018-12-29 00:08:27 +01:00
}
rows.Close()
db.Close()
2018-12-29 01:36:46 +01:00
//***************************************************************************
2018-12-10 16:09:22 +01:00
}
2018-12-10 18:00:33 +01:00
func login(w http.ResponseWriter, r *http.Request) {
2018-12-28 22:10:55 +01:00
//Here:
2018-12-10 18:00:33 +01:00
fmt.Println("method:", r.Method)
if r.Method == "GET" {
2018-12-28 14:22:11 +01:00
t, _ := template.ParseFiles("login.gtpl")
2018-12-10 18:00:33 +01:00
t.Execute(w, nil)
} else {
r.ParseForm()
2018-12-29 04:11:48 +01:00
//fmt.Println("usernameFROMsite: ", r.Form["usernameFROMsite"])
//fmt.Println("passwordFROMsite: ", r.Form["passwordFROMsite"])
2018-12-29 01:36:46 +01:00
//if len(r.Form["usernameFROMsite"][0]) == 0 {
2018-12-28 14:54:01 +01:00
// goto Here
//}
2018-12-29 01:36:46 +01:00
//if len(r.Form["passwordFROMsite"][0]) == 0 {
2018-12-28 14:54:01 +01:00
// goto Here
//}
2018-12-27 21:58:43 +01:00
}
2018-12-29 04:11:48 +01:00
usernameFROMsite := r.FormValue("usernameFROMsite")
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)
var IDusers int
var User string
var Password string
var PIN int
var RFID int
for rows.Next() {
err = rows.Scan(&IDusers, &User, &Password, &PIN, &RFID)
checkErr(err)
//fmt.Printf("%d. %s, %s, %d, %d \n", IDusers, User, Password, PIN, RFID)
//fmt.Fprint(w, IDusers)
//fmt.Fprint(w, ". ")
//fmt.Fprint(w, User)
//fmt.Fprint(w, " \n")
if r.Method == "POST" {
if User == usernameFROMsite {
if Password == passwordFROMsite {
fmt.Fprintln(w, zaloguj)
fmt.Fprintln(w, usernameFROMsite)
2018-12-29 04:54:47 +01:00
//panel(w, r)
2018-12-29 04:11:48 +01:00
break
}
}
}
}
2018-12-27 23:53:11 +01:00
if r.Method == "POST" {
2018-12-29 04:11:48 +01:00
if usernameFROMsite != User {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! *** Spróbuj ponownie. ***")
2018-12-27 23:53:11 +01:00
}
2018-12-17 22:35:22 +01:00
}
2018-12-29 04:11:48 +01:00
rows.Close()
db.Close()
//***************************************************************************
2018-12-17 22:35:22 +01:00
}
2018-12-29 00:08:27 +01:00
//***************************************************************************
//Bazy danych SQlite
//***************************************************************************
func checkErr(err error) {
if err != nil {
fmt.Println("Błąd")
}
2018-12-28 22:10:55 +01:00
}
2018-12-29 00:08:27 +01:00
//***************************************************************************
//funkcja główna
//***************************************************************************
2018-11-21 23:47:31 +01:00
func main() {
2018-12-27 21:58:43 +01:00
http.HandleFunc("/", opisStołówkaZPM)
2018-12-10 18:00:33 +01:00
http.HandleFunc("/login", login)
2018-12-17 22:35:22 +01:00
http.HandleFunc("/panel", panel)
2018-12-10 18:00:33 +01:00
err := http.ListenAndServe(":9197", nil)
2018-12-10 15:52:29 +01:00
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
2018-11-21 23:47:31 +01:00
}