Update main.go

This commit is contained in:
s441433 2019-01-03 23:57:38 +01:00
parent 7eb099a017
commit f5580e86e4

181
main.go
View File

@ -14,39 +14,50 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
//logowanie ------------ var (
var loginFROMsite string //localhost-------------
var passwordFROMsite string ID int
localHostDB string
localHost string
//users --------------- //logowanie ------------
var IDusers int loginFROMsite string
var User string passwordFROMsite string
var Password string
var PIN int
var RFID int
var Login string
var Blokada bool
var Koszt int
//bilety --------------- //users ---------------
var IDbiletu int IDusers int
var KtoZabral string User string
var DataCzas string Password string
var KosztBiletu int PIN int
var CzyZaplacony bool RFID int
Login string
Blokada bool
Koszt int
//dania --------------- //bilety ---------------
var IDdania int IDbiletu int
var NazwaDania string KtoZabral string
var KosztDania int DataCzas string
KosztBiletu int
CzyZaplacony bool
//dania ---------------
IDdania int
NazwaDania string
KosztDania int
)
//const localHost = string("192.168.8.102:9197")
//*************************************************************************** //***************************************************************************
//funkcje sterujące //funkcje sterujące
//*************************************************************************** //***************************************************************************
//############################################################################# //#############################################################################
func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) { func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) {
//fmt.Println(localHostDB)
t, _ := template.ParseFiles("StronaGlowna.gtpl") t, _ := template.ParseFiles("StronaGlowna.gtpl")
t.Execute(w, nil) t.Execute(w, localHost)
} }
//############################################################################# //#############################################################################
@ -88,9 +99,11 @@ func panel(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) fmt.Println("method:", r.Method)
t, _ := template.ParseFiles("panel.gtpl") t, _ := template.ParseFiles("panel.gtpl")
d := struct { d := struct {
UserView string UserView string
localHostView string
}{ }{
UserView: User} UserView: User,
localHostView: localHost}
t.ExecuteTemplate(w, "panel.gtpl", d) t.ExecuteTemplate(w, "panel.gtpl", d)
} }
@ -100,6 +113,7 @@ func wylogowano(w http.ResponseWriter, r *http.Request) {
//-------------------------------------------------- //--------------------------------------------------
t, _ := template.ParseFiles("wylogowano.gtpl") t, _ := template.ParseFiles("wylogowano.gtpl")
d := struct { d := struct {
localHostView string
loginFROMsiteView string loginFROMsiteView string
passwordFROMsiteView string passwordFROMsiteView string
IDusersView int IDusersView int
@ -118,6 +132,7 @@ func wylogowano(w http.ResponseWriter, r *http.Request) {
NazwaDaniaView string NazwaDaniaView string
KosztDaniaView int KosztDaniaView int
}{ }{
localHostView: localHost,
loginFROMsiteView: "", loginFROMsiteView: "",
passwordFROMsiteView: "", passwordFROMsiteView: "",
IDusersView: 0, IDusersView: 0,
@ -141,18 +156,18 @@ func wylogowano(w http.ResponseWriter, r *http.Request) {
//############################################################################# //#############################################################################
func login(w http.ResponseWriter, r *http.Request) { func login(w http.ResponseWriter, r *http.Request) {
//fmt.Println(localHost)
//cookies start=================================================================== //cookies start===================================================================
type Cookie struct { //type Cookie struct {
Login string // Login string
Password string // Password string
} //}
//cookies end=================================================================== //cookies end===================================================================
//**************************************************************************
loginFROMsite := r.FormValue("loginFROMsite") loginFROMsite := r.FormValue("loginFROMsite")
passwordFROMsite := r.FormValue("passwordFROMsite") passwordFROMsite := r.FormValue("passwordFROMsite")
//zaloguj := "Zalogowano!"
//**************************************************************************
db, err := sql.Open("sqlite3", "stolowkaZPM.db") db, err := sql.Open("sqlite3", "stolowkaZPM.db")
checkErr(err) checkErr(err)
// query // query
@ -179,56 +194,109 @@ func login(w http.ResponseWriter, r *http.Request) {
break break
} }
} }
//}
} }
fmt.Println("method:", r.Method) //fmt.Println("method:", r.Method)
//r.Method = "GET"
if r.Method == "GET" { if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl") t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil) d := struct {
//} else { localHost string
}{
localHost: localHost}
t.ExecuteTemplate(w, "login.gtpl", d)
r.ParseForm() 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
//}
} }
//} 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
//}
//obsługa blędnego wpisania loginu lub hasła //obsługa blędnego wpisania loginu lub hasła
if r.Method == "POST" { if r.Method == "POST" {
if loginFROMsite != Login { if loginFROMsite != Login {
if loginFROMsite == "" { if loginFROMsite == "" {
if passwordFROMsite != Password { if passwordFROMsite != Password {
t, _ := template.ParseFiles("login.gtpl") t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil) d := struct {
localHostView string
}{
localHostView: localHost}
t.ExecuteTemplate(w, "login.gtpl", d)
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***") fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
} }
} else { } else {
if passwordFROMsite != Password { if passwordFROMsite != Password {
t, _ := template.ParseFiles("login.gtpl") t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil) d := struct {
localHostView string
}{
localHostView: localHost}
t.ExecuteTemplate(w, "login.gtpl", d)
fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***") fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***")
} }
} }
} }
} }
//**************************************************************************
rows0.Close() rows0.Close()
db.Close() db.Close()
//************************************************************************** }
//#############################################################################
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()
} }
//############################################################################# //#############################################################################
func historiaPosilkow(w http.ResponseWriter, r *http.Request) { func historiaPosilkow(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("historiaPosilkowNaglowek.gtpl") t, _ := template.ParseFiles("historiaPosilkowNaglowek.gtpl")
d := struct { d := struct {
UserView string localHostView string
UserView string
}{ }{
UserView: User} localHostView: localHost,
UserView: User}
t.ExecuteTemplate(w, "historiaPosilkowNaglowek.gtpl", d) t.ExecuteTemplate(w, "historiaPosilkowNaglowek.gtpl", d)
//bilety ---------------------------------- //bilety ----------------------------------
@ -242,12 +310,14 @@ func historiaPosilkow(w http.ResponseWriter, r *http.Request) {
if KtoZabral == User { if KtoZabral == User {
t, _ := template.ParseFiles("historiaPosilkow.gtpl") t, _ := template.ParseFiles("historiaPosilkow.gtpl")
d := struct { d := struct {
localHostView string
IDbiletuView int IDbiletuView int
KtoZabralView string KtoZabralView string
DataCzasView string DataCzasView string
KosztBiletuView int KosztBiletuView int
CzyZaplaconyView bool CzyZaplaconyView bool
}{ }{
localHostView: localHost,
IDbiletuView: IDbiletu, IDbiletuView: IDbiletu,
KtoZabralView: KtoZabral, KtoZabralView: KtoZabral,
DataCzasView: DataCzas, DataCzasView: DataCzas,
@ -323,14 +393,27 @@ func checkErr3(err error) {
//funkcja główna //funkcja główna
//*************************************************************************** //***************************************************************************
func main() { func main() {
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()
http.HandleFunc("/", opisStołówkaZPM) http.HandleFunc("/", opisStołówkaZPM)
http.HandleFunc("/login", login) http.HandleFunc("/login", login)
http.HandleFunc("/panel", panel) http.HandleFunc("/panel", panel)
http.HandleFunc("/historiaPosilkow", historiaPosilkow) http.HandleFunc("/historiaPosilkow", historiaPosilkow)
http.HandleFunc("/platnosci", platnosci) http.HandleFunc("/platnosci", platnosci)
http.HandleFunc("/wylogowano", wylogowano) http.HandleFunc("/wylogowano", wylogowano)
http.HandleFunc("/menuDan", menuDan)
err := http.ListenAndServe(":9197", nil) err = http.ListenAndServe(":9197", nil)
if err != nil { if err != nil {
log.Fatal("ListenAndServe: ", err) log.Fatal("ListenAndServe: ", err)
} }