package main import ( "database/sql" "fmt" "html/template" "log" "net/http" //"net/url" //"time" _ "github.com/mattn/go-sqlite3" ) 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") //*************************************************************************** //funkcje sterujące //*************************************************************************** //############################################################################# func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) { //fmt.Println(localHostDB) t, _ := template.ParseFiles("StronaGlowna.gtpl") t.Execute(w, localHost) } //############################################################################# func panel(w http.ResponseWriter, r *http.Request) { //Here: //time.Sleep(3 * time.Second) /* //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() */ for _, cookie := range r.Cookies() { fmt.Fprint(w, cookie.Name) } fmt.Println("method:", r.Method) t, _ := template.ParseFiles("panel.gtpl") d := struct { UserView string localHostView string }{ UserView: User, localHostView: localHost} t.ExecuteTemplate(w, "panel.gtpl", d) } //############################################################################# func wylogowano(w http.ResponseWriter, r *http.Request) { //-------------------------------------------------- t, _ := template.ParseFiles("wylogowano.gtpl") d := struct { localHostView string 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 }{ localHostView: localHost, 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) } //############################################################################# func login(w http.ResponseWriter, r *http.Request) { //fmt.Println(localHost) //cookies start=================================================================== //type Cookie struct { // Login string // Password string //} //cookies end=================================================================== //************************************************************************** loginFROMsite := r.FormValue("loginFROMsite") passwordFROMsite := r.FormValue("passwordFROMsite") db, err := sql.Open("sqlite3", "stolowkaZPM.db") checkErr(err) // query rows0, err := db.Query("SELECT * FROM users") checkErr(err) 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 } } } //fmt.Println("method:", r.Method) //r.Method = "GET" if r.Method == "GET" { t, _ := template.ParseFiles("login.gtpl") d := struct { localHost string }{ localHost: localHost} t.ExecuteTemplate(w, "login.gtpl", d) r.ParseForm() } //} 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 if r.Method == "POST" { if loginFROMsite != Login { if loginFROMsite == "" { if passwordFROMsite != Password { t, _ := template.ParseFiles("login.gtpl") 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. ***") } } else { if passwordFROMsite != Password { t, _ := template.ParseFiles("login.gtpl") 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. ***") } } } } //************************************************************************** rows0.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) { t, _ := template.ParseFiles("historiaPosilkowNaglowek.gtpl") d := struct { localHostView string UserView string }{ localHostView: localHost, UserView: User} t.ExecuteTemplate(w, "historiaPosilkowNaglowek.gtpl", d) //bilety ---------------------------------- db, err := sql.Open("sqlite3", "stolowkaZPM.db") checkErr(err) rows3, err := db.Query("SELECT * FROM bilety") checkErr(err) for rows3.Next() { err = rows3.Scan(&IDbiletu, &KtoZabral, &DataCzas, &KosztBiletu, &CzyZaplacony) checkErr(err) if KtoZabral == User { t, _ := template.ParseFiles("historiaPosilkow.gtpl") d := struct { localHostView string IDbiletuView int KtoZabralView string DataCzasView string KosztBiletuView int CzyZaplaconyView bool }{ localHostView: localHost, IDbiletuView: IDbiletu, KtoZabralView: KtoZabral, DataCzasView: DataCzas, KosztBiletuView: KosztBiletu, CzyZaplaconyView: CzyZaplacony} t.ExecuteTemplate(w, "historiaPosilkow.gtpl", d) } //---------------------------------- } rows3.Close() db.Close() } //############################################################################# func platnosci(w http.ResponseWriter, r *http.Request) { //fmt.Fprint(w, User) //bilety ---------------------------------- db, err := sql.Open("sqlite3", "stolowkaZPM.db") checkErr(err) 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) if KtoZabral == User { t, _ := template.ParseFiles("platnosci.gtpl") d := struct { UserView string KosztBiletuViewSuma int }{ UserView: User, KosztBiletuViewSuma: } t.ExecuteTemplate(w, "platnosci.gtpl", d) //break } //---------------------------------- //} */ for rows5.Next() { rows5.Scan(&KtoZabral, &KosztBiletu) fmt.Printf("%v %v\n", KtoZabral, KosztBiletu) } rows5.Close() db.Close() } //*************************************************************************** //Obsługa błędów //*************************************************************************** func checkErr(err error) { if err != nil { fmt.Println("Błąd") } } func checkErr2(err error) { if err != nil { fmt.Println("Błąd dwa") } } func checkErr3(err error) { if err != nil { fmt.Println("Błąd trzy") } } //*************************************************************************** //funkcja główna //*************************************************************************** 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("/login", login) http.HandleFunc("/panel", panel) http.HandleFunc("/historiaPosilkow", historiaPosilkow) http.HandleFunc("/platnosci", platnosci) http.HandleFunc("/wylogowano", wylogowano) http.HandleFunc("/menuDan", menuDan) err = http.ListenAndServe(":9197", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }