|
@@ -0,0 +1,172 @@
|
|
|
+
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "log"
|
|
|
+ "database/sql"
|
|
|
+ "os"
|
|
|
+ "time"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
+ "html/template"
|
|
|
+ "github.com/gorilla/mux"
|
|
|
+ "github.com/gorilla/sessions"
|
|
|
+
|
|
|
+ _ "github.com/go-sql-driver/mysql"
|
|
|
+
|
|
|
+)
|
|
|
+
|
|
|
+type Page struct {
|
|
|
+ Title string
|
|
|
+ Body []byte
|
|
|
+}
|
|
|
+
|
|
|
+var (
|
|
|
+ logger *log.Logger
|
|
|
+ sessionsStore = sessions.NewCookieStore([]byte("mysecretcookie"))
|
|
|
+ indexTemplate = template.Must(template.ParseFiles("www\\index.html"))
|
|
|
+ logInTemplate = template.Must(template.ParseFiles("www\\login.html"))
|
|
|
+ signInTemplate = template.Must(template.ParseFiles("www\\signin.html"))
|
|
|
+)
|
|
|
+
|
|
|
+func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ data := struct {
|
|
|
+ User string
|
|
|
+ Title string
|
|
|
+ Items []string
|
|
|
+ }{
|
|
|
+ User: "",
|
|
|
+ Title: "My page",
|
|
|
+ Items: []string{
|
|
|
+ "My photos",
|
|
|
+ "My blog",
|
|
|
+ "More",
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ session, _ := sessionsStore.Get(r, "session")
|
|
|
+
|
|
|
+ var (
|
|
|
+ password string
|
|
|
+ ok3 bool
|
|
|
+ )
|
|
|
+
|
|
|
+ untyped, ok := session.Values["username"]
|
|
|
+ if ok {
|
|
|
+ username, ok1 := untyped.(string)
|
|
|
+ if ok1 {
|
|
|
+ data.User = username
|
|
|
+ untyped, ok2 := session.Values["password"]
|
|
|
+ if ok2 {
|
|
|
+ password, ok3 = untyped.(string)
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ indexTemplate.Execute(w, data)
|
|
|
+
|
|
|
+ if ok3 {
|
|
|
+ w.Write([]byte(password))
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func indexPostHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ fmt.Print("POST")
|
|
|
+}
|
|
|
+
|
|
|
+func logInGetHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ logInTemplate.Execute(w, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func logInPostHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ r.ParseForm()
|
|
|
+ username := r.PostForm.Get("username")
|
|
|
+ password := r.PostForm.Get("password")
|
|
|
+
|
|
|
+ //dBConnector.
|
|
|
+
|
|
|
+ db, err := sql.Open("mysql", "gosite:gositepassword@tcp(192.168.48.5)/gosite")
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ fmt.Print("Error")
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ defer db.Close()
|
|
|
+
|
|
|
+ var counter int
|
|
|
+
|
|
|
+ act_query := "SELECT count(*) FROM gosite.users WHERE username='" + username + "' AND password='" + password + "';"
|
|
|
+ db.QueryRow(act_query).Scan(&counter)
|
|
|
+ fmt.Println("we have", counter, "rows")
|
|
|
+
|
|
|
+ if (counter == 0) {
|
|
|
+ logInTemplate.Execute(w, nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ session, _ := sessionsStore.Get(r, "session")
|
|
|
+ session.Values["username"] = username
|
|
|
+ session.Values["password"] = password
|
|
|
+
|
|
|
+ session.Save(r,w)
|
|
|
+ fmt.Printf("%s\n%s\n",username,password)
|
|
|
+ http.Redirect(w, r, "/", http.StatusFound)
|
|
|
+}
|
|
|
+
|
|
|
+func signInHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ signInTemplate.Execute(w, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func pressHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ logger.Print("click")
|
|
|
+ http.Redirect(w, r, "/", http.StatusFound)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func createLogger() {
|
|
|
+ startTime := time.Now()
|
|
|
+ logFileName := "logs/go-site_log_" + startTime.Format("2006-01-02_15-04-05") + ".txt"
|
|
|
+ file, err := os.OpenFile(logFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
+ logger = log.New(file, "logger: ", log.Lshortfile)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+type user struct{
|
|
|
+ idusers int
|
|
|
+ username string
|
|
|
+ password string
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func main() {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ createLogger()
|
|
|
+
|
|
|
+ logger.Print("Hello, log file!")
|
|
|
+
|
|
|
+ requestRouter := mux.NewRouter()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ requestRouter.HandleFunc("/", indexHandler).Methods("GET")
|
|
|
+ requestRouter.HandleFunc("/", indexPostHandler).Methods("POST")
|
|
|
+ requestRouter.HandleFunc("/login/", logInGetHandler).Methods("GET")
|
|
|
+ requestRouter.HandleFunc("/login/", logInPostHandler).Methods("POST")
|
|
|
+ requestRouter.HandleFunc("/signin/", signInHandler).Methods("GET")
|
|
|
+ //requestRouter.HandleFunc("/", pressHandler).Methods("POST")
|
|
|
+ http.Handle("/", requestRouter)
|
|
|
+ logger.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
+}
|