zaliczeniePP/main.go
2018-12-29 02:31:03 +01:00

105 lines
2.7 KiB
Go

package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
_ "github.com/mattn/go-sqlite3"
)
//***************************************************************************
//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) {
fmt.Println("method:", r.Method)
t, _ := template.ParseFiles("panel.gtpl")
t.Execute(w, nil)
//***************************************************************************
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")
}
rows.Close()
db.Close()
//***************************************************************************
}
func login(w http.ResponseWriter, r *http.Request) {
//Here:
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
//}
}
usernameFROMsite := r.Form["usernameFROMsite"]
passwordFROMsite := r.Form["passwordFROMsite"]
zaloguj := "Wpisane wartości:"
if r.Method == "POST" {
if zaloguj == "Wpisane wartości:" {
fmt.Fprintln(w, zaloguj)
fmt.Fprintln(w, usernameFROMsite)
fmt.Fprintln(w, passwordFROMsite)
}
}
}
//***************************************************************************
//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)
}
}