123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- package main
- import (
- "fmt"
- "html/template"
- // "io"
- "net/http"
- "strconv"
- "github.com/gorilla/mux"
- )
- func indexHandler(w http.ResponseWriter, r *http.Request) {
- data := struct {
- User string
- Password string
- Title string
- Items []string
- }{
- User: "",
- Title: "My page",
- Items: []string{
- "My photos",
- "My blog",
- "More",
- },
- }
- session, _ := sessionsStore.Get(r, "session")
- 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)
- if ok3 {
- data.Password = password
- }
- }
- /*
- fmt.Println(data.User)
- fmt.Println(data.Password)
- */
- tindexTemplate, err := template.New("").ParseFiles(INDEX_TEMPLATE, "ui\\templates\\placeholder.html")
- if err != nil {
- fmt.Println("Error in templates loading")
- fmt.Println(err)
- w.Write([]byte("Internal server error"))
- return
- }
- err = tindexTemplate.ExecuteTemplate(w, "index", data)
- if err != nil {
- fmt.Println(err)
- }
- }
- func indexPostHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Print("POST")
- }
- func logInGetHandler(w http.ResponseWriter, r *http.Request) {
- data := struct {
- Username string
- Password string
- Error string
- }{
- Username: "",
- Password: "",
- Error: "",
- }
- logInTemplate.Execute(w, data)
- }
- func logInPostHandler(w http.ResponseWriter, r *http.Request) {
- r.ParseForm()
- username := r.PostForm.Get("username")
- password := r.PostForm.Get("password")
- fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm)
- if dBConnector.LogIn(username, password) {
- session, _ := sessionsStore.Get(r, "session")
- session.Values["username"] = username
- session.Values["password"] = password
- session.Save(r, w)
- http.Redirect(w, r, "/", http.StatusFound)
- } else {
- data := struct {
- Username string
- Password string
- Error string
- }{
- Username: username,
- Password: password,
- Error: "Неверный логин либо пароль",
- }
- fmt.Printf("Login error")
- logInTemplate.Execute(w, data)
- //w.Write([]byte("Login error"))
- }
- }
- func logOutGetHandler(w http.ResponseWriter, r *http.Request) {
- session, _ := sessionsStore.Get(r, "session")
- session.Options.MaxAge = -1
- session.Save(r, w)
- untyped, ok := session.Values["username"]
- if ok {
- username, ok1 := untyped.(string)
- if ok1 {
- logger.Printf("User %s loged out", username)
- }
- }
- http.Redirect(w, r, "/", http.StatusFound)
- }
- func signInGetHandler(w http.ResponseWriter, r *http.Request) {
- signInTemplate.Execute(w, nil)
- }
- func signInPostHandler(w http.ResponseWriter, r *http.Request) {
- r.ParseForm()
- username := r.PostForm.Get("username")
- password := r.PostForm.Get("password")
- passwordRepeat := r.PostForm.Get("password-repeat")
- data := struct {
- Username string
- Password string
- PasswordRepeat string
- Error string
- }{
- Username: username,
- Password: password,
- PasswordRepeat: passwordRepeat,
- Error: "",
- }
- if dBConnector.IsNameUsed(username) {
- data.Error = "Имя пользователя занято"
- } else {
- if password != passwordRepeat {
- data.Error = "Введенные пароли не совпадают"
- } else {
- if dBConnector.SigInUser(username, password) {
- session, _ := sessionsStore.Get(r, "session")
- session.Values["username"] = username
- session.Values["password"] = password
- session.Save(r, w)
- http.Redirect(w, r, "/", http.StatusFound)
- } else {
- data.Error = "Произошла внутреняя ошибка при регистрации нового пользователя"
- }
- }
- }
- signInTemplate.Execute(w, data)
- }
- func gameGetHandler(w http.ResponseWriter, r *http.Request) {
- gameTemplate.Execute(w, nil)
- }
- func gamePostHandler(w http.ResponseWriter, r *http.Request) { //TODO запись score в таблицу
- if err := r.ParseForm(); err != nil {
- fmt.Fprintf(w, "ParseForm() err: %v", err)
- return
- }
- fmt.Printf("Post from website! r.PostFrom = %v\n", r.PostForm)
- score_str := r.FormValue("score")
- fmt.Fprintf(w, "score = %s\n", score_str)
- fmt.Printf("score = %s\n", score_str)
- score_int, err := strconv.Atoi(score_str)
- if err != nil {
- // handle error
- fmt.Println(err)
- logger.Printf("Error extracting score from '%s'", score_str)
- // os.Exit(2)
- } else {
- dBConnector.SubmitScore(score_int)
- }
- }
- func DownloadTorrentHandler(w http.ResponseWriter, r *http.Request) {
- torrentHash := mux.Vars(r)["hash"]
- fmt.Println(r.URL)
- torrent, err := dBConnector.TakeTorrent(torrentHash)
- if err != nil {
- fmt.Println(err)
- w.WriteHeader(http.StatusOK)
- w.Write([]byte("Torrent file not found"))
- return
- }
- if torrent.Deleted {
- w.WriteHeader(http.StatusOK)
- w.Write([]byte("The torrent file you are looking for has been deleted"))
- return
- }
- w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.torrent\"", torrent.Name))
- http.ServeFile(w, r, fmt.Sprintf("files/torrents/%s.torrent", torrent.Hash))
- }
|