/
controllers.go
105 lines (85 loc) · 2.61 KB
/
controllers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"code.google.com/p/go.crypto/bcrypt"
//"database/sql"
"encoding/json"
"github.com/codegangsta/martini-contrib/render"
"github.com/coopernurse/gorp"
"github.com/go-martini/martini"
_ "github.com/lib/pq"
"github.com/martini-contrib/sessions"
"log"
"net/http"
//"fmt"
//"text/template"
)
func CreateUser(user User) (result string) {
user.Save()
return
}
func Index() string {
log.Println("This is index page")
return "This is index"
}
func Coords() string {
return "coords"
}
func Move(params martini.Params) string { // this is fake function for now. ToDo: change it to use actual Point
return "Hello " + params["x"] + params["y"]
}
func GetState(params martini.Params) (result string) { // Todo: change return type to json
current_state := State{}
serialized_state, err := json.Marshal(current_state)
if err != nil {
result = "{'status', 'failed'}"
} else {
result = string(serialized_state)
}
return
}
func ShowProfile(params martini.Params) (result string) {
result = "Profile"
return result
}
func Login(r *http.Request, render render.Render, db *gorp.DbMap, s sessions.Session) {
user := User{}
email, password := r.FormValue("email"), r.FormValue("password")
err := db.SelectOne(&user, "Select * from users where email=? ", email)
//tmp_pass, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
//err := db.QueryRow("select id, password from users where email=$1", email).Scan(&userId, &dbPasswd)
if err != nil || bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) != nil {
render.HTML(400, "404", "")
}
s.Set("userId", user.Id)
render.HTML(200, "dashboard", "")
}
func LoginForm(w http.ResponseWriter, r render.Render) {
r.HTML(200, "login", "")
}
func Logout(params martini.Params) (result string) {
result = "Logout"
return
}
func Signup(rw http.ResponseWriter, r *http.Request, db *gorp.DbMap) {
name, email, password := r.FormValue("name"), r.FormValue("email"), r.FormValue("password")
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
PanicIf(err)
u := User{
Name: name,
Email: email,
Password: hashedPassword}
//_, err = db.Exec("insert into users (name, email, password) values ($1, $2, $3)",
err = db.Insert(&u)
//name, email, hashedPassword)
PanicIf(err)
http.Redirect(rw, r, "/login", http.StatusFound)
}
func SignupForm(w http.ResponseWriter, r render.Render) {
r.HTML(200, "signup", "")
}
func CreateRoom(w http.ResponseWriter, r render.Render) string {
return "room id"
}
func ListRooms(w http.ResponseWriter, r render.Render) string {
return "here be rooms list"
}