zaliczeniePP/main.go

421 lines
11 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 16:02:40 +01:00
//"net/url"
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
2019-01-03 23:57:38 +01:00
var (
//localhost-------------
ID int
localHostDB string
localHost string
//logowanie ------------
loginFROMsite string
passwordFROMsite string
//users ---------------
IDusers int
User string
Password string
PIN int
RFID int
Login string
Blokada bool
Koszt int
//bilety ---------------
IDbiletu int
KtoZabral string
DataCzas string
KosztBiletu int
CzyZaplacony bool
//dania ---------------
IDdania int
NazwaDania string
KosztDania int
)
//const localHost = string("192.168.8.102:9197")
2018-12-29 16:02:40 +01:00
2018-12-29 00:08:27 +01:00
//***************************************************************************
//funkcje sterujące
//***************************************************************************
2019-01-03 23:57:38 +01:00
2018-12-29 16:02:40 +01:00
//#############################################################################
2018-12-27 21:58:43 +01:00
func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) {
2019-01-03 23:57:38 +01:00
//fmt.Println(localHostDB)
2018-12-28 14:22:11 +01:00
t, _ := template.ParseFiles("StronaGlowna.gtpl")
2019-01-03 23:57:38 +01:00
t.Execute(w, localHost)
2018-12-29 01:36:46 +01:00
}
2018-12-29 00:08:27 +01:00
2018-12-29 16:02:40 +01:00
//#############################################################################
2018-12-29 01:36:46 +01:00
func panel(w http.ResponseWriter, r *http.Request) {
2018-12-29 16:02:40 +01:00
//Here:
2018-12-29 04:54:47 +01:00
//time.Sleep(3 * time.Second)
2018-12-29 01:36:46 +01:00
2019-01-02 01:13:39 +01:00
/*
//bilety ----------------------------------
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
rows1, err := db.Query("SELECT * FROM bilety")
checkErr(err)
fmt.Fprintf(w, "Lista zabranych biletów: \n")
for rows1.Next() {
err = rows1.Scan(&IDbiletu, &KtoZabral, &DataCzas, &KosztBiletu)
checkErr(err)
fmt.Printf("%d. %s, %s, %d \n", IDbiletu, KtoZabral, DataCzas, KosztBiletu)
}
//----------------------------------
//dania ----------------------------------
rows2, err := db.Query("SELECT * FROM dania")
checkErr(err)
fmt.Fprintf(w, "Lista dań: \n")
for rows2.Next() {
err = rows2.Scan(&IDdania, &NazwaDania, &KosztDania)
checkErr(err)
fmt.Printf("%d. %s, %d \n", IDdania, NazwaDania, KosztDania)
}
//----------------------------------
rows1.Close()
rows2.Close()
db.Close()
*/
2019-01-02 16:32:13 +01:00
for _, cookie := range r.Cookies() {
fmt.Fprint(w, cookie.Name)
}
2019-01-02 01:13:39 +01:00
2018-12-29 16:02:40 +01:00
fmt.Println("method:", r.Method)
2019-01-02 01:13:39 +01:00
t, _ := template.ParseFiles("panel.gtpl")
d := struct {
2019-01-03 23:57:38 +01:00
UserView string
localHostView string
2019-01-02 01:13:39 +01:00
}{
2019-01-03 23:57:38 +01:00
UserView: User,
localHostView: localHost}
2019-01-02 01:13:39 +01:00
t.ExecuteTemplate(w, "panel.gtpl", d)
2018-12-29 16:02:40 +01:00
}
2019-01-02 16:32:13 +01:00
//#############################################################################
func wylogowano(w http.ResponseWriter, r *http.Request) {
//--------------------------------------------------
t, _ := template.ParseFiles("wylogowano.gtpl")
d := struct {
2019-01-03 23:57:38 +01:00
localHostView string
2019-01-02 16:32:13 +01:00
loginFROMsiteView string
passwordFROMsiteView string
IDusersView int
UserView string
PasswordView string
PINView int
RFIDView int
LoginView string
BlokadaView bool
KosztView int
IDbiletuView int
KtoZabralView string
DataCzasView string
KosztBiletuView int
IDdaniaView int
NazwaDaniaView string
KosztDaniaView int
}{
2019-01-03 23:57:38 +01:00
localHostView: localHost,
2019-01-02 16:32:13 +01:00
loginFROMsiteView: "",
passwordFROMsiteView: "",
IDusersView: 0,
UserView: "",
PasswordView: "",
PINView: 0,
RFIDView: 0,
LoginView: "",
BlokadaView: Blokada,
KosztView: 0,
IDbiletuView: 0,
KtoZabralView: "",
DataCzasView: "",
KosztBiletuView: 0,
IDdaniaView: 0,
NazwaDaniaView: "",
KosztDaniaView: 0}
t.ExecuteTemplate(w, "wylogowano.gtpl", d)
}
2019-01-02 01:13:39 +01:00
//#############################################################################
func login(w http.ResponseWriter, r *http.Request) {
2019-01-03 23:57:38 +01:00
//fmt.Println(localHost)
2019-01-02 16:32:13 +01:00
//cookies start===================================================================
2019-01-03 23:57:38 +01:00
//type Cookie struct {
// Login string
// Password string
//}
2019-01-02 16:32:13 +01:00
//cookies end===================================================================
2019-01-03 23:57:38 +01:00
//**************************************************************************
2019-01-02 01:13:39 +01:00
loginFROMsite := r.FormValue("loginFROMsite")
passwordFROMsite := r.FormValue("passwordFROMsite")
2018-12-29 00:08:27 +01:00
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
// query
2019-01-02 01:13:39 +01:00
rows0, err := db.Query("SELECT * FROM users")
2018-12-29 00:08:27 +01:00
checkErr(err)
2018-12-29 16:02:40 +01:00
2019-01-02 01:13:39 +01:00
for rows0.Next() {
err = rows0.Scan(&IDusers, &User, &Password, &PIN, &RFID, &Login, &Blokada, &Koszt)
checkErr(err)
//fmt.Printf("%d. %s, %s, %d, %d, %s, %d, %d \n", IDusers, User, Password, PIN, RFID, Login, Blokada, Koszt)
//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
2018-12-29 16:02:40 +01:00
}
}
2018-12-29 00:08:27 +01:00
}
2018-12-10 16:09:22 +01:00
2019-01-03 23:57:38 +01:00
//fmt.Println("method:", r.Method)
//r.Method = "GET"
2018-12-10 18:00:33 +01:00
if r.Method == "GET" {
2018-12-28 14:22:11 +01:00
t, _ := template.ParseFiles("login.gtpl")
2019-01-03 23:57:38 +01:00
d := struct {
localHost string
}{
localHost: localHost}
t.ExecuteTemplate(w, "login.gtpl", d)
2018-12-10 18:00:33 +01:00
r.ParseForm()
2018-12-27 21:58:43 +01:00
}
2018-12-29 04:11:48 +01:00
2019-01-03 23:57:38 +01:00
//} else {
//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
//}
2019-01-02 01:13:39 +01:00
//obsługa blędnego wpisania loginu lub hasła
2019-01-03 23:57:38 +01:00
2019-01-02 01:13:39 +01:00
if r.Method == "POST" {
if loginFROMsite != Login {
if loginFROMsite == "" {
if passwordFROMsite != Password {
t, _ := template.ParseFiles("login.gtpl")
2019-01-03 23:57:38 +01:00
d := struct {
localHostView string
}{
localHostView: localHost}
t.ExecuteTemplate(w, "login.gtpl", d)
2019-01-02 01:13:39 +01:00
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
}
} else {
if passwordFROMsite != Password {
t, _ := template.ParseFiles("login.gtpl")
2019-01-03 23:57:38 +01:00
d := struct {
localHostView string
}{
localHostView: localHost}
t.ExecuteTemplate(w, "login.gtpl", d)
2019-01-02 01:13:39 +01:00
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
}
}
}
}
2019-01-03 23:57:38 +01:00
//**************************************************************************
2019-01-02 01:13:39 +01:00
rows0.Close()
db.Close()
2019-01-03 23:57:38 +01:00
}
//#############################################################################
func menuDan(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("menuDanNaglowek.gtpl")
d := struct {
localHostView string
UserView string
}{
localHostView: localHost,
UserView: User}
t.ExecuteTemplate(w, "menuDanNaglowek.gtpl", d)
//dania ----------------------------------
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr2(err)
rows2, err := db.Query("SELECT * FROM dania")
checkErr(err)
for rows2.Next() {
err = rows2.Scan(&IDdania, &NazwaDania, &KosztDania)
checkErr3(err)
//fmt.Printf("%d. %s, %d \n", IDdania, NazwaDania, KosztDania)
t, _ := template.ParseFiles("menuDan.gtpl")
d := struct {
localHostView string
IDdaniaView int
NazwaDaniaView string
KosztDaniaView int
}{
localHostView: localHost,
IDdaniaView: IDdania,
NazwaDaniaView: NazwaDania,
KosztDaniaView: KosztDania}
t.ExecuteTemplate(w, "menuDan.gtpl", d)
}
//----------------------------------
rows2.Close()
db.Close()
2019-01-02 01:13:39 +01:00
}
//#############################################################################
func historiaPosilkow(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("historiaPosilkowNaglowek.gtpl")
d := struct {
2019-01-03 23:57:38 +01:00
localHostView string
UserView string
2019-01-02 01:13:39 +01:00
}{
2019-01-03 23:57:38 +01:00
localHostView: localHost,
UserView: User}
2019-01-02 01:13:39 +01:00
t.ExecuteTemplate(w, "historiaPosilkowNaglowek.gtpl", d)
//bilety ----------------------------------
2018-12-29 04:11:48 +01:00
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
2019-01-02 01:13:39 +01:00
rows3, err := db.Query("SELECT * FROM bilety")
2018-12-29 04:11:48 +01:00
checkErr(err)
2019-01-02 01:13:39 +01:00
for rows3.Next() {
2019-01-02 16:32:13 +01:00
err = rows3.Scan(&IDbiletu, &KtoZabral, &DataCzas, &KosztBiletu, &CzyZaplacony)
2018-12-29 04:11:48 +01:00
checkErr(err)
2019-01-02 01:13:39 +01:00
if KtoZabral == User {
t, _ := template.ParseFiles("historiaPosilkow.gtpl")
d := struct {
2019-01-03 23:57:38 +01:00
localHostView string
2019-01-02 16:32:13 +01:00
IDbiletuView int
KtoZabralView string
DataCzasView string
KosztBiletuView int
CzyZaplaconyView bool
2019-01-02 01:13:39 +01:00
}{
2019-01-03 23:57:38 +01:00
localHostView: localHost,
2019-01-02 16:32:13 +01:00
IDbiletuView: IDbiletu,
KtoZabralView: KtoZabral,
DataCzasView: DataCzas,
KosztBiletuView: KosztBiletu,
CzyZaplaconyView: CzyZaplacony}
2019-01-02 01:13:39 +01:00
t.ExecuteTemplate(w, "historiaPosilkow.gtpl", d)
2018-12-29 04:11:48 +01:00
}
2019-01-02 01:13:39 +01:00
//----------------------------------
2018-12-29 04:11:48 +01:00
}
2019-01-02 01:13:39 +01:00
rows3.Close()
db.Close()
}
//#############################################################################
func platnosci(w http.ResponseWriter, r *http.Request) {
2019-01-02 16:32:13 +01:00
//fmt.Fprint(w, User)
2019-01-02 01:13:39 +01:00
//bilety ----------------------------------
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
2019-01-02 16:32:13 +01:00
rows5, err := db.Query("SELECT SUM(KosztBiletu)FROM bilety WHERE KtoZabral=?", User)
if err != nil {
fmt.Println(err)
// os.Exit(1)
}
/*
//for rows5.Next() {
//err = rows5.Scan(&KtoZabral, &KosztBiletu)
//checkErr3(err)
2019-01-02 01:13:39 +01:00
if KtoZabral == User {
t, _ := template.ParseFiles("platnosci.gtpl")
d := struct {
2019-01-02 16:32:13 +01:00
UserView string
KosztBiletuViewSuma int
2019-01-02 01:13:39 +01:00
}{
2019-01-02 16:32:13 +01:00
UserView: User,
KosztBiletuViewSuma: }
2019-01-02 01:13:39 +01:00
t.ExecuteTemplate(w, "platnosci.gtpl", d)
2019-01-02 16:32:13 +01:00
//break
2018-12-27 23:53:11 +01:00
}
2019-01-02 01:13:39 +01:00
//----------------------------------
2019-01-02 16:32:13 +01:00
//}
*/
for rows5.Next() {
rows5.Scan(&KtoZabral, &KosztBiletu)
fmt.Printf("%v %v\n", KtoZabral, KosztBiletu)
2018-12-17 22:35:22 +01:00
}
2019-01-02 16:32:13 +01:00
rows5.Close()
2018-12-29 04:11:48 +01:00
db.Close()
2018-12-17 22:35:22 +01:00
}
2018-12-29 00:08:27 +01:00
//***************************************************************************
2019-01-02 16:32:13 +01:00
//Obsługa błędów
2018-12-29 00:08:27 +01:00
//***************************************************************************
func checkErr(err error) {
if err != nil {
fmt.Println("Błąd")
}
2018-12-28 22:10:55 +01:00
}
2019-01-02 16:32:13 +01:00
func checkErr2(err error) {
if err != nil {
fmt.Println("Błąd dwa")
}
}
func checkErr3(err error) {
if err != nil {
fmt.Println("Błąd trzy")
}
}
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() {
2019-01-03 23:57:38 +01:00
db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err)
rowsHost, err := db.Query("SELECT * FROM localhost")
checkErr2(err)
for rowsHost.Next() {
err = rowsHost.Scan(&ID, &localHostDB)
checkErr3(err)
}
localHost = localHostDB
rowsHost.Close()
db.Close()
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)
2019-01-02 01:13:39 +01:00
http.HandleFunc("/historiaPosilkow", historiaPosilkow)
http.HandleFunc("/platnosci", platnosci)
2019-01-02 16:32:13 +01:00
http.HandleFunc("/wylogowano", wylogowano)
2019-01-03 23:57:38 +01:00
http.HandleFunc("/menuDan", menuDan)
2018-12-29 16:02:40 +01:00
2019-01-03 23:57:38 +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
}