forked from leafo/lapis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.moon
43 lines (34 loc) · 1.09 KB
/
example.moon
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
lapis = require "lapis"
csrf = require "lapis.csrf"
import Model from require "lapis.db.model"
import respond_to, capture_errors from require "lapis.application"
class Users extends Model
url_params: =>
"user", id: @id
class App extends lapis.Application
-- Execute code before every action
@before_filter =>
@csrf_token = csrf.generate_token @
[list_users: "/users"]: =>
users = Users\select! -- `select` all the users
-- Render HTML inline for simplicity
@html ->
ul ->
for user in *users
li ->
a href: @url_for(user), user.name
[user: "/profile/:id"]: =>
user = Users\find id: @params.id
return status: 404 unless user
@html -> h2 user.name
[new_user: "/user/new"]: respond_to {
POST: capture_errors =>
csrf.assert_token @
Users\create name: @params.username
redirect_to: @url_for "list_users"
GET: =>
@html ->
form method: "POST", action: @url_for("new_user"), ->
input type: "hidden", name: "csrf_token", value: @csrf_token
input type: "text", name: "username"
}