-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.go
524 lines (370 loc) · 15 KB
/
server.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package main
import (
"database/sql"
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/binding"
"github.com/codegangsta/martini-contrib/render"
"github.com/codegangsta/martini-contrib/sessions"
"github.com/coopernurse/gorp"
_ "github.com/go-sql-driver/mysql"
"github.com/codegangsta/martini-contrib/sessionauth"
"log"
"net/http"
"time"
"html/template"
"strconv"
"regexp"
"strings"
)
type Post struct {
// db tag lets you specify the column name if it differs from the struct field
Id int64 `db:"post_id"`
Created int64
Title string `form:"Title" binding:"required"`
Body string `form:"Body"`
UserId int64 `form:"UserId"`
Url string
}
type User struct {
Id int64 `form:"id" db:"id"`
Email string `form:"email" db:"email" binding:"required"`
Password string `form:"password" db:"password" binding:"required"`
Name string `form:"name" db:"name"`
authenticated bool `form:"-" db:"-"`
}
func newUser(email string, password string, name string, authenticated bool) User {
return User{
Email: email,
Password: password,
Name: name,
authenticated: authenticated,
}
}
func newPost(title string, body string, user int64) Post {
//let's make pretty urls from title
reg, err := regexp.Compile("[^A-Za-z0-9]+")
if err != nil {
log.Fatal(err)
}
prettyurl := reg.ReplaceAllString(title, "-")
prettyurl = strings.ToLower(strings.Trim(prettyurl, "-"))
return Post{
Created: time.Now().Unix(),
Title: title,
Body: body,
UserId: user,
Url: prettyurl,
}
}
func checkErr(err error, msg string) {
if err != nil {
log.Fatalln(msg, err)
}
}
//BINDING: https://github.com/codegangsta/martini-contrib/tree/master/binding
//sample custom Post struct validation
func (bp Post) Validate(errors *binding.Errors, req *http.Request) {
//custom validation
if len(bp.Title) == 0 {
errors.Fields["title"] = "Title cannot be empty"
}
}
//sample custom User struct validation
func (bp User) Validate(errors *binding.Errors, req *http.Request) {
//custom validation
}
//SESSIONAUTH: https://github.com/codegangsta/martini-contrib/tree/master/sessionauth
// GetAnonymousUser should generate an anonymous user model for all sessions. This should be an unauthenticated 0 value struct.
func GenerateAnonymousUser() sessionauth.User {
return &User{}
}
// Login will preform any actions that are required to make a user model officially authenticated.
func (u *User) Login() {
// Update last login time, add to logged-in user's list
u.authenticated = true
}
// Logout will preform any actions that are required to completely logout a user.
func (u *User) Logout() {
// Remove from logged-in user's list
u.authenticated = false
}
func (u *User) IsAuthenticated() bool {
return u.authenticated
}
func (u *User) UniqueId() interface{} {
return u.Id
}
// GetById will populate a user object from a database model with a matching id.
func (u *User) GetById(id interface{}) error {
err := dbmap.SelectOne(u, "SELECT * FROM users WHERE id = ?", id)
if err != nil {
return err
}
return nil
}
//INITAL DATABASE SETUP
var dbmap *gorp.DbMap
func initDb() *gorp.DbMap {
db, err := sql.Open("mysql", "USERNAME:PASSWORD@unix(/var/run/mysqld/mysqld.sock)/webapp")
checkErr(err, "sql.Open failed")
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
dbmap.AddTableWithName(User{}, "users").SetKeys(true, "Id")
err = dbmap.CreateTablesIfNotExists()
checkErr(err, "Create tables failed")
dbmap.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")
err = dbmap.CreateTablesIfNotExists()
checkErr(err, "Create tables failed")
return dbmap
}
func main() {
//Change secret123 to something more secure and store session in backend instead of cookie
store := sessions.NewCookieStore([]byte("secret123"))
dbmap = initDb()
defer dbmap.Db.Close()
err := dbmap.TruncateTables()
checkErr(err, "TruncateTables failed")
u1 := newUser("[email protected]", "pass", "Bob", false)
//insert rows
err = dbmap.Insert(&u1)
checkErr(err, "Insert failed")
//create two posts, assign to user 1 above
p1 := newPost("Post 1", "Lorem ipsum lorem ipsum",1)
p2 := newPost("Post 2", "This is my second post",1)
// insert rows
err = dbmap.Insert(&p1, &p2)
checkErr(err, "Insert failed")
m := martini.Classic()
m.Use(render.Renderer(render.Options{
Directory: "templates",
Layout: "layout",
Funcs: []template.FuncMap{
{
"formatTime": func(args ...interface{}) string {
t1 := time.Unix(args[0].(int64), 0)
return t1.Format(time.Stamp)
},
},
},
}))
m.Use(sessions.Sessions("my_session", store))
m.Use(sessionauth.SessionUser(GenerateAnonymousUser))
sessionauth.RedirectUrl = "/login"
sessionauth.RedirectParam = "next"
//ROUTES
m.Get("/register", func(r render.Render, user sessionauth.User) {
//redirect to homepage if already authenticated
if(user.IsAuthenticated()){
r.Redirect("/")
} else {
r.HTML(200, "register", nil)
}
})
m.Get("/login", func(r render.Render, user sessionauth.User) {
//redirect to homepage if already authenticated
if(user.IsAuthenticated()){
r.Redirect("/")
} else {
r.HTML(200, "login", nil)
}
})
m.Post("/login", binding.Form(User{}), func(session sessions.Session, postedUser User, r render.Render, ferr binding.Errors, req *http.Request) {
log.Println(ferr)
//Example of server side error validation for the client side form
if ferr.Count() > 0 {
newmap := map[string]interface{}{"metatitle":"Registration", "errormessage":"Error with Form Submission"}
r.HTML(200, "login", newmap)
} else {
user := User{}
//check login credentails with DataBase
err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE email = ? and password = ?", postedUser.Email, postedUser.Password)
if err != nil {
r.Redirect(sessionauth.RedirectUrl)
return
} else {
err := sessionauth.AuthenticateSession(session, &user)
if err != nil {
r.JSON(500, err)
}
params := req.URL.Query()
redirect := params.Get(sessionauth.RedirectParam)
r.Redirect(redirect)
return
}
}
})
m.Get("/logout", sessionauth.LoginRequired, func(session sessions.Session, user sessionauth.User, r render.Render) {
sessionauth.Logout(session, user)
r.Redirect("/")
})
m.Get("/", func(r render.Render, authuser sessionauth.User) {
var posts []Post
_, err = dbmap.Select(&posts, "select * from posts order by post_id")
checkErr(err, "Select failed")
newmap := map[string]interface{}{"metatitle": "HomePage", "authuser": authuser, "posts": posts}
r.HTML(200, "posts", newmap)
})
m.Get("/users", func(r render.Render, authuser sessionauth.User) {
var users []User
_, err = dbmap.Select(&users, "select * from users order by id")
checkErr(err, "Select failed")
newmap := map[string]interface{}{"metatitle": "Users listing", "authuser": authuser, "users": users}
r.HTML(200, "users", newmap)
})
m.Get("/users/:id", sessionauth.LoginRequired, func(args martini.Params, r render.Render, authuser sessionauth.User) {
var user User
err = dbmap.SelectOne(&user, "select * from users where id=?", args["id"])
//simple error check
if err != nil {
newmap := map[string]interface{}{"metatitle":"404 Error", "message":"User not found"}
r.HTML(404, "error", newmap)
} else {
var posts []Post
_, err = dbmap.Select(&posts, "select * from posts where UserId=?", args["id"])
checkErr(err, "Select failed")
newmap := map[string]interface{}{"metatitle": user.Name+" profile page", "authuser": authuser, "user": user, "posts": posts}
r.HTML(200, "user", newmap)
}
})
m.Post("/users", binding.Form(User{}), func(session sessions.Session, user User, ferr binding.Errors, r render.Render) {
//Example of server side error validation for the client side form
if ferr.Count() > 0 {
newmap := map[string]interface{}{"metatitle":"Registration", "errormessage":"Error with Form Submission"}
r.HTML(200, "register", newmap)
} else {
u := newUser(user.Email, user.Password, user.Name, user.authenticated)
err = dbmap.Insert(&u)
checkErr(err, "Insert failed")
//create the session and redirect always to homepage
err := sessionauth.AuthenticateSession(session, &u)
if err != nil {
r.JSON(500, err)
}
r.Redirect("/")
}
})
m.Put("/users/:id", binding.Bind(User{}), func(args martini.Params, user User, r render.Render, authuser sessionauth.User) {
//convert string to int64 so you can match the struct (passing userid via ajax does not work as it comes in as a string)
f, _ := strconv.ParseInt(args["id"],0,64)
//only allow the authenticated user to update his user attributes
if(authuser.UniqueId() == f){
//specify the user id
user.Id = f
count, err := dbmap.Update(&user)
checkErr(err, "Update failed")
log.Println("Rows updated:", count)
if count == 1 {
newmap := map[string]interface{}{"responseText":"success"}
r.JSON(200, newmap)
} else {
newmap := map[string]interface{}{"responseText":"error"}
r.JSON(400, newmap)
}
} else {
newmap := map[string]interface{}{"responseText":"You are not allowed to update this resource."}
r.JSON(403, newmap)
}
})
m.Delete("/users/:id", func(args martini.Params, r render.Render, authuser sessionauth.User) {
//convert id from string to int64
f, _ := strconv.ParseInt(args["id"],0,64)
//only allow the authenticated user to delete him or her
if(authuser.UniqueId() == f){
_, err = dbmap.Exec("delete from users where id=?", args["id"])
checkErr(err, "Delete failed")
if err == nil {
newmap := map[string]interface{}{"responseText":"success"}
r.JSON(200, newmap)
//if you delete yourself, Ajax should redirec you
} else {
newmap := map[string]interface{}{"responseText":"error"}
r.JSON(400, newmap)
}
} else {
newmap := map[string]interface{}{"responseText":"You are not allowed to delete this resource."}
r.JSON(403, newmap)
}
})
m.Post("/posts", sessionauth.LoginRequired, binding.Bind(Post{}), func(post Post, r render.Render, authuser sessionauth.User) {
//convert to int64
f := authuser.UniqueId().(int64)
p1 := newPost(post.Title, post.Body,f)
err = dbmap.Insert(&p1)
checkErr(err, "Insert failed")
r.Redirect("/")
})
m.Get("/posts/:id", func(args martini.Params, r render.Render, authuser sessionauth.User) {
var post Post
err = dbmap.SelectOne(&post, "select * from posts where post_id=?", args["id"])
//simple error check
if err != nil {
newmap := map[string]interface{}{"metatitle":"404 Error", "message":"This is not found"}
r.HTML(404, "error", newmap)
} else {
newmap := map[string]interface{}{"metatitle": post.Title+" more custom","authuser": authuser, "post": post}
r.HTML(200, "post", newmap)
}
})
m.Get("/p/:str", func(args martini.Params, r render.Render, authuser sessionauth.User) {
var post Post
err = dbmap.SelectOne(&post, "select * from posts where url=?", args["str"])
//simple error check
if err != nil {
newmap := map[string]interface{}{"metatitle":"404 Error", "message":"This is not found"}
r.HTML(404, "error", newmap)
} else {
newmap := map[string]interface{}{"metatitle": post.Title+" more custom","authuser": authuser, "post": post}
r.HTML(200, "post", newmap)
}
})
m.Put("/posts/:id", binding.Bind(Post{}), func(args martini.Params, post Post, r render.Render, authuser sessionauth.User) {
var newTitle = post.Title
var newBody = post.Body
err = dbmap.SelectOne(&post, "select * from posts where post_id=?", args["id"])
//simple database error check
if err != nil {
newmap := map[string]interface{}{"message":"Something went wrong."}
r.JSON(400, newmap)
} else {
//owner check
if(authuser.UniqueId() == post.UserId){
post.Title=newTitle
post.Body=newBody
count, err := dbmap.Update(&post)
checkErr(err, "Update failed")
if count == 1 {
newmap := map[string]interface{}{"responseText":"success"}
r.JSON(200, newmap)
} else {
newmap := map[string]interface{}{"responseText":"error"}
r.JSON(400, newmap)
}
} else {
newmap := map[string]interface{}{"responseText":"You are not allowed to modify this resource."}
r.JSON(403, newmap)
}
}
})
m.Delete("/posts/:id", func(args martini.Params, r render.Render, authuser sessionauth.User) {
//retrieve the post to check the real owner
var post Post
err = dbmap.SelectOne(&post, "select * from posts where post_id=?", args["id"])
//simple DB error check
if err != nil {
newmap := map[string]interface{}{"message":"Something went wrong."}
r.JSON(400, newmap)
} else {
//owner check
if(authuser.UniqueId() == post.UserId){
//delete it
_, err := dbmap.Delete(&post)
checkErr(err, "Delete failed")
newmap := map[string]interface{}{"responseText":"success"}
r.JSON(200, newmap)
} else {
newmap := map[string]interface{}{"responseText":"You are not allowed to delete this resource."}
r.JSON(403, newmap)
}
}
})
m.Run()
}