handlers.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package main
  2. import (
  3. //"html/template"
  4. "net/http"
  5. "fmt"
  6. "strconv"
  7. )
  8. func indexHandler(w http.ResponseWriter, r *http.Request) {
  9. data := struct {
  10. User string
  11. Title string
  12. Items []string
  13. }{
  14. User: "",
  15. Title: "My page",
  16. Items: []string{
  17. "My photos",
  18. "My blog",
  19. "More",
  20. },
  21. }
  22. session, _ := sessionsStore.Get(r, "session")
  23. var (
  24. password string
  25. ok3 bool
  26. )
  27. untyped, ok := session.Values["username"]
  28. if ok {
  29. username, ok1 := untyped.(string)
  30. if ok1 {
  31. data.User = username
  32. untyped, ok2 := session.Values["password"]
  33. if ok2 {
  34. password, ok3 = untyped.(string)
  35. }
  36. }
  37. }
  38. indexTemplate.Execute(w, data)
  39. if ok3 {
  40. w.Write([]byte(password))
  41. }
  42. }
  43. func indexPostHandler(w http.ResponseWriter, r *http.Request) {
  44. fmt.Print("POST")
  45. }
  46. func logInGetHandler(w http.ResponseWriter, r *http.Request) {
  47. data := struct {
  48. Username string
  49. Password string
  50. Error string
  51. }{
  52. Username: "",
  53. Password: "",
  54. Error: "",
  55. }
  56. logInTemplate.Execute(w, data)
  57. }
  58. func logInPostHandler(w http.ResponseWriter, r *http.Request) {
  59. r.ParseForm()
  60. username := r.PostForm.Get("username")
  61. password := r.PostForm.Get("password")
  62. fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm)
  63. if (dBConnector.LogIn(username, password)){
  64. session, _ := sessionsStore.Get(r, "session")
  65. session.Values["username"] = username
  66. session.Values["password"] = password
  67. session.Save(r,w)
  68. http.Redirect(w, r, "/", http.StatusFound)
  69. } else {
  70. data := struct {
  71. Username string
  72. Password string
  73. Error string
  74. }{
  75. Username: username,
  76. Password: password,
  77. Error: "Неверный логин либо пароль",
  78. }
  79. fmt.Printf("Login error")
  80. logInTemplate.Execute(w, data)
  81. //w.Write([]byte("Login error"))
  82. }
  83. }
  84. func logOutGetHandler(w http.ResponseWriter, r *http.Request) {
  85. session, _ := sessionsStore.Get(r, "session")
  86. session.Options.MaxAge = -1
  87. session.Save(r, w)
  88. untyped, ok := session.Values["username"]
  89. if ok {
  90. username, ok1 := untyped.(string)
  91. if ok1 {
  92. logger.Printf("User %s loged out", username)
  93. }
  94. }
  95. http.Redirect(w, r, "/", http.StatusFound)
  96. }
  97. func signInGetHandler(w http.ResponseWriter, r *http.Request) {
  98. signInTemplate.Execute(w, nil)
  99. }
  100. func signInPostHandler(w http.ResponseWriter, r *http.Request) {
  101. r.ParseForm()
  102. username := r.PostForm.Get("username")
  103. password := r.PostForm.Get("password")
  104. passwordRepeat := r.PostForm.Get("password-repeat")
  105. data := struct {
  106. Username string
  107. Password string
  108. PasswordRepeat string
  109. Error string
  110. }{
  111. Username: username,
  112. Password: password,
  113. PasswordRepeat: passwordRepeat,
  114. Error: "",
  115. }
  116. if dBConnector.IsNameUsed(username) {
  117. data.Error = "Имя пользователя занято"
  118. } else {
  119. if (password != passwordRepeat) {
  120. data.Error = "Введенные пароли не совпадают"
  121. } else {
  122. if dBConnector.SigInUser(username,password) {
  123. session, _ := sessionsStore.Get(r, "session")
  124. session.Values["username"] = username
  125. session.Values["password"] = password
  126. session.Save(r,w)
  127. http.Redirect(w, r, "/", http.StatusFound)
  128. } else {
  129. data.Error = "Произошла внутреняя ошибка при регистрации нового пользователя"
  130. }
  131. }
  132. }
  133. signInTemplate.Execute(w, data)
  134. }
  135. func gameGetHandler(w http.ResponseWriter, r *http.Request) {
  136. gameTemplate.Execute(w, nil)
  137. }
  138. func gamePostHandler(w http.ResponseWriter, r *http.Request){ //TODO запись score в таблицу
  139. if err := r.ParseForm(); err != nil {
  140. fmt.Fprintf(w, "ParseForm() err: %v", err)
  141. return
  142. }
  143. fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm)
  144. score_str := r.FormValue("score")
  145. fmt.Fprintf(w,"score = %s\n", score_str)
  146. fmt.Printf("score = %s\n", score_str)
  147. score_int, err := strconv.Atoi(score_str)
  148. if err != nil {
  149. // handle error
  150. fmt.Println(err)
  151. logger.Printf("Error extracting score from '%s'",score_str)
  152. //os.Exit(2)
  153. } else {
  154. dBConnector.SubmitScore(score_int)
  155. }
  156. }