handlers.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. // "io"
  6. "net/http"
  7. "strconv"
  8. "github.com/gorilla/mux"
  9. )
  10. func indexHandler(w http.ResponseWriter, r *http.Request) {
  11. data := struct {
  12. User string
  13. Password string
  14. Title string
  15. Items []string
  16. }{
  17. User: "",
  18. Title: "My page",
  19. Items: []string{
  20. "My photos",
  21. "My blog",
  22. "More",
  23. },
  24. }
  25. session, _ := sessionsStore.Get(r, "session")
  26. untyped, ok := session.Values["username"]
  27. if ok {
  28. username, ok1 := untyped.(string)
  29. if ok1 {
  30. data.User = username
  31. }
  32. }
  33. untyped, ok2 := session.Values["password"]
  34. if ok2 {
  35. password, ok3 := untyped.(string)
  36. if ok3 {
  37. data.Password = password
  38. }
  39. }
  40. /*
  41. fmt.Println(data.User)
  42. fmt.Println(data.Password)
  43. */
  44. tindexTemplate, err := template.New("").ParseFiles(INDEX_TEMPLATE, "ui\\templates\\placeholder.html")
  45. if err != nil {
  46. fmt.Println("Error in templates loading")
  47. fmt.Println(err)
  48. w.Write([]byte("Internal server error"))
  49. return
  50. }
  51. err = tindexTemplate.ExecuteTemplate(w, "index", data)
  52. if err != nil {
  53. fmt.Println(err)
  54. }
  55. }
  56. func indexPostHandler(w http.ResponseWriter, r *http.Request) {
  57. fmt.Print("POST")
  58. }
  59. func logInGetHandler(w http.ResponseWriter, r *http.Request) {
  60. data := struct {
  61. Username string
  62. Password string
  63. Error string
  64. }{
  65. Username: "",
  66. Password: "",
  67. Error: "",
  68. }
  69. logInTemplate.Execute(w, data)
  70. }
  71. func logInPostHandler(w http.ResponseWriter, r *http.Request) {
  72. r.ParseForm()
  73. username := r.PostForm.Get("username")
  74. password := r.PostForm.Get("password")
  75. fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm)
  76. if dBConnector.LogIn(username, password) {
  77. session, _ := sessionsStore.Get(r, "session")
  78. session.Values["username"] = username
  79. session.Values["password"] = password
  80. session.Save(r, w)
  81. http.Redirect(w, r, "/", http.StatusFound)
  82. } else {
  83. data := struct {
  84. Username string
  85. Password string
  86. Error string
  87. }{
  88. Username: username,
  89. Password: password,
  90. Error: "Неверный логин либо пароль",
  91. }
  92. fmt.Printf("Login error")
  93. logInTemplate.Execute(w, data)
  94. //w.Write([]byte("Login error"))
  95. }
  96. }
  97. func logOutGetHandler(w http.ResponseWriter, r *http.Request) {
  98. session, _ := sessionsStore.Get(r, "session")
  99. session.Options.MaxAge = -1
  100. session.Save(r, w)
  101. untyped, ok := session.Values["username"]
  102. if ok {
  103. username, ok1 := untyped.(string)
  104. if ok1 {
  105. logger.Printf("User %s loged out", username)
  106. }
  107. }
  108. http.Redirect(w, r, "/", http.StatusFound)
  109. }
  110. func signInGetHandler(w http.ResponseWriter, r *http.Request) {
  111. signInTemplate.Execute(w, nil)
  112. }
  113. func signInPostHandler(w http.ResponseWriter, r *http.Request) {
  114. r.ParseForm()
  115. username := r.PostForm.Get("username")
  116. password := r.PostForm.Get("password")
  117. passwordRepeat := r.PostForm.Get("password-repeat")
  118. data := struct {
  119. Username string
  120. Password string
  121. PasswordRepeat string
  122. Error string
  123. }{
  124. Username: username,
  125. Password: password,
  126. PasswordRepeat: passwordRepeat,
  127. Error: "",
  128. }
  129. if dBConnector.IsNameUsed(username) {
  130. data.Error = "Имя пользователя занято"
  131. } else {
  132. if password != passwordRepeat {
  133. data.Error = "Введенные пароли не совпадают"
  134. } else {
  135. if dBConnector.SigInUser(username, password) {
  136. session, _ := sessionsStore.Get(r, "session")
  137. session.Values["username"] = username
  138. session.Values["password"] = password
  139. session.Save(r, w)
  140. http.Redirect(w, r, "/", http.StatusFound)
  141. } else {
  142. data.Error = "Произошла внутреняя ошибка при регистрации нового пользователя"
  143. }
  144. }
  145. }
  146. signInTemplate.Execute(w, data)
  147. }
  148. func gameGetHandler(w http.ResponseWriter, r *http.Request) {
  149. gameTemplate.Execute(w, nil)
  150. }
  151. func gamePostHandler(w http.ResponseWriter, r *http.Request) { //TODO запись score в таблицу
  152. if err := r.ParseForm(); err != nil {
  153. fmt.Fprintf(w, "ParseForm() err: %v", err)
  154. return
  155. }
  156. fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm)
  157. score_str := r.FormValue("score")
  158. fmt.Fprintf(w, "score = %s\n", score_str)
  159. fmt.Printf("score = %s\n", score_str)
  160. score_int, err := strconv.Atoi(score_str)
  161. if err != nil {
  162. // handle error
  163. fmt.Println(err)
  164. logger.Printf("Error extracting score from '%s'", score_str)
  165. // os.Exit(2)
  166. } else {
  167. dBConnector.SubmitScore(score_int)
  168. }
  169. }
  170. func DownloadTorrentHandler(w http.ResponseWriter, r *http.Request) {
  171. torrentHash := mux.Vars(r)["hash"]
  172. fmt.Println(r.URL)
  173. torrent, err := dBConnector.TakeTorrent(torrentHash)
  174. if err != nil {
  175. fmt.Println(err)
  176. w.WriteHeader(http.StatusOK)
  177. w.Write([]byte("Torrent file not found"))
  178. return
  179. }
  180. if torrent.Deleted {
  181. w.WriteHeader(http.StatusOK)
  182. w.Write([]byte("The torrent file you are looking for has been deleted"))
  183. return
  184. }
  185. w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.torrent\"", torrent.Name))
  186. http.ServeFile(w, r, fmt.Sprintf("files/torrents/%s.torrent", torrent.Hash))
  187. }