/
main.go
186 lines (156 loc) · 4.41 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"html/template"
"io/ioutil"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"math/rand"
"net/http"
"strconv"
"strings"
"github.com/robertseaton/neptune/pkgs/bkz"
"github.com/robertseaton/neptune/pkgs/codify"
"github.com/robertseaton/neptune/pkgs/cookies"
"github.com/robertseaton/neptune/pkgs/user"
)
type Page struct {
Title string
Body template.HTML
UserData template.HTML
Bar template.HTML
}
// Loads a page for use
func loadPage(title string, r *http.Request) (*Page, error) {
filename, option, usr, bar := user.LoadUserInfo(title, r)
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: template.HTML(body), UserData: (template.HTML(usr) + template.HTML(option)), Bar: template.HTML(bar)}, nil
}
// Shows a particular page
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[len("/"):]
p, err := loadPage(title, r)
// wonky TODO make better
z := strings.Split(title, "/")
if z[0] == "books" {
http.ServeFile(w, r, title)
return
}
if err != nil && !cookies.IsLoggedIn(r) {
http.Redirect(w, r, "/home", http.StatusFound)
return
} else if err != nil {
http.Redirect(w, r, "/login-succeeded", http.StatusFound)
return
}
t, _ := template.ParseFiles("view.html")
t.Execute(w, p)
}
// Handles the users loggin and gives them a cookie for doing so
func loginHandler(w http.ResponseWriter, r *http.Request) {
usr := new(user.User)
usr.Email = r.FormValue("email")
pass := r.FormValue("pwd")
userProfile := user.FindUser(usr.Email)
if len(pass) > 0 {
usr.Password = codify.SHA(pass)
ok := user.CheckCredentials(usr.Email, usr.Password)
if ok {
usr = userProfile
user.CreateUserFile(usr.Email) // TODO: Createuserfile?
cookie := cookies.LoginCookie(usr.Email)
http.SetCookie(w, &cookie)
usr.SessionID = cookie.Value
_ = user.UpdateUser(usr)
http.Redirect(w, r, "/login-succeeded", http.StatusFound)
} else {
http.Redirect(w, r, "/login-failed", http.StatusFound)
}
} else {
viewHandler(w, r)
}
}
// Registers the new user
func registerHandler(w http.ResponseWriter, r *http.Request) {
usr := new(user.User)
usr.Email = r.FormValue("email")
pass := r.FormValue("pwd")
if len(pass) > 0 {
usr.Password = codify.SHA(pass)
if user.DoesAccountExist(usr.Email) {
http.Redirect(w, r, "/account-exists", http.StatusFound)
} else {
ok := user.CreateAccount(usr)
if ok {
http.Redirect(w, r, "/success", http.StatusFound)
} else {
http.Redirect(w, r, "/failed", http.StatusFound)
}
}
} else {
viewHandler(w, r)
}
}
// Logs out the user, removes their cookie from the database
// TODO: clean up this function
func logoutHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("SessionID")
if err != nil {
fmt.Println(err)
return
}
result := new(user.User)
sessionID := cookie.Value
session, err := mgo.Dial("127.0.0.1:27017/")
if err != nil {
return
}
c := session.DB("test").C("users")
c.Find(bson.M{"sessionid": sessionID}).One(&result)
result.SessionID = result.Email + ":" + codify.SHA(result.SessionID+strconv.Itoa(rand.Intn(100000000)))
err = c.Update(bson.M{"email": result.Email}, result)
if err != nil {
fmt.Println(err)
}
http.Redirect(w, r, "/home", http.StatusFound)
}
// Adds a new book to the database/user
func bookHandler(w http.ResponseWriter, r *http.Request) {
book := new(bkz.Book)
book.Title = r.FormValue("book")
book.Author = r.FormValue("author")
book.ISBN = r.FormValue("isbn")
book.Genre = r.FormValue("genre")
// TODO improve bookId, isbn is being used for testing
book.Id = book.ISBN
if len(book.Title) > 0 {
ok := bkz.CreateBook(book)
if ok {
// TODO add a better succuess page
http.Redirect(w, r, "/add-book-success", http.StatusFound)
} else {
// TODO add another error page
http.Redirect(w, r, "/add-book-failed", http.StatusFound)
}
cookie, _ := r.Cookie("SessionID")
sessionID := cookie.Value
z := strings.Split(sessionID, ":")
username := z[0]
if !user.UpdateCollection(username, book) {
fmt.Println("The user: " + username + " does not exist!")
}
} else {
viewHandler(w, r)
}
}
func main() {
http.HandleFunc("/login", loginHandler)
http.HandleFunc("/logout", logoutHandler)
http.HandleFunc("/register", registerHandler)
http.HandleFunc("/add-book", bookHandler)
http.HandleFunc("/", viewHandler)
http.ListenAndServe(":8080", nil)
}