main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "time"
  6. "fmt"
  7. "net/http"
  8. "html/template"
  9. "github.com/gorilla/mux"
  10. "github.com/gorilla/sessions"
  11. )
  12. type Page struct {
  13. Title string
  14. Body []byte
  15. }
  16. var (
  17. logger *log.Logger
  18. dBConnector connection
  19. sessionsStore = sessions.NewCookieStore([]byte("mysecretcookie"))
  20. indexTemplate = template.Must(template.ParseFiles("www\\index.html"))
  21. logInTemplate = template.Must(template.ParseFiles("www\\login.html"))
  22. signInTemplate = template.Must(template.ParseFiles("www\\signin.html"))
  23. )
  24. func indexHandler(w http.ResponseWriter, r *http.Request) {
  25. data := struct {
  26. User string
  27. Title string
  28. Items []string
  29. }{
  30. User: "",
  31. Title: "My page",
  32. Items: []string{
  33. "My photos",
  34. "My blog",
  35. "More",
  36. },
  37. }
  38. session, _ := sessionsStore.Get(r, "session")
  39. var (
  40. password string
  41. ok3 bool
  42. )
  43. untyped, ok := session.Values["username"]
  44. if ok {
  45. username, ok1 := untyped.(string)
  46. if ok1 {
  47. data.User = username
  48. untyped, ok2 := session.Values["password"]
  49. if ok2 {
  50. password, ok3 = untyped.(string)
  51. }
  52. }
  53. }
  54. indexTemplate.Execute(w, data)
  55. if ok3 {
  56. w.Write([]byte(password))
  57. }
  58. }
  59. func indexPostHandler(w http.ResponseWriter, r *http.Request) {
  60. fmt.Print("POST")
  61. }
  62. func logInGetHandler(w http.ResponseWriter, r *http.Request) {
  63. data := struct {
  64. Username string
  65. Password string
  66. Error string
  67. }{
  68. Username: "",
  69. Password: "",
  70. Error: "",
  71. }
  72. logInTemplate.Execute(w, data)
  73. }
  74. func logInPostHandler(w http.ResponseWriter, r *http.Request) {
  75. r.ParseForm()
  76. username := r.PostForm.Get("username")
  77. password := r.PostForm.Get("password")
  78. if (dBConnector.LogIn(username, password)){
  79. session, _ := sessionsStore.Get(r, "session")
  80. session.Values["username"] = username
  81. session.Values["password"] = password
  82. session.Save(r,w)
  83. fmt.Printf("%s\n%s\n",username,password)
  84. http.Redirect(w, r, "/", http.StatusFound)
  85. } else {
  86. data := struct {
  87. Username string
  88. Password string
  89. Error string
  90. }{
  91. Username: username,
  92. Password: password,
  93. Error: "Неверный логин либо пароль",
  94. }
  95. fmt.Printf("Login error")
  96. logInTemplate.Execute(w, data)
  97. //w.Write([]byte("Login error"))
  98. }
  99. }
  100. func signInHandler(w http.ResponseWriter, r *http.Request) {
  101. signInTemplate.Execute(w, nil)
  102. }
  103. func pressHandler(w http.ResponseWriter, r *http.Request) {
  104. logger.Print("click")
  105. http.Redirect(w, r, "/", http.StatusFound)
  106. }
  107. func createLogger() {
  108. startTime := time.Now()
  109. logFileName := "logs/go-site_log_" + startTime.Format("2006-01-02_15-04-05") + ".txt"
  110. file, err := os.OpenFile(logFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
  111. logger = log.New(file, "logger: ", log.Lshortfile)
  112. if err != nil {
  113. log.Fatal(err)
  114. }
  115. }
  116. type user struct{
  117. idusers int
  118. username string
  119. password string
  120. }
  121. func main() {
  122. createLogger()
  123. logger.Print("Hello, log file!")
  124. requestRouter := mux.NewRouter()
  125. dBConnector.Init("config.json")
  126. requestRouter.HandleFunc("/", indexHandler).Methods("GET")
  127. requestRouter.HandleFunc("/", indexPostHandler).Methods("POST")
  128. requestRouter.HandleFunc("/login/", logInGetHandler).Methods("GET")
  129. requestRouter.HandleFunc("/login/", logInPostHandler).Methods("POST")
  130. requestRouter.HandleFunc("/signin/", signInHandler).Methods("GET")
  131. //requestRouter.HandleFunc("/", pressHandler).Methods("POST")
  132. http.Handle("/", requestRouter)
  133. logger.Fatal(http.ListenAndServe(":8080", nil))
  134. }