-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
93 lines (73 loc) · 2.13 KB
/
app.coffee
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
app = require 'berliner'
app.public = __dirname + '/public'
app.views = __dirname + '/views'
app.session_secret = 'abc123'
repeat = (t,f) -> setInterval f, t
app.helpers
inc_counter: ->
@session.counter ||= 0
@session.counter += 1
site_name: ->
'Awesome.net'
app.before (next) ->
@puts @request.method + ' ' + @request.url
next()
app.get '/', ->
@cookie message: {value: 'Sinatra; it’s a framework', path: '/hello'}
@title = 'Almost Sinatra'
@haml 'index'
app.get 'hello', ->
@ejs 'hello', locals:
name: @params.name
message: @cookies.message
app.get '/counter', ->
@inc_counter()
@render @session.counter
app.context '/auth', (auth) ->
auth.before (next) ->
if @request.headers.authorization
next()
else
@status 401
@render 'Authorization required'
auth.get '/', ->
'Secret stuff'
auth.delete 'info', ->
@status 418
'I am a teapot'
app.get '/words/:category/:id.:format', ->
JSON.stringify @params
app.get '/download/*.*', ->
@params.splat.join ', '
app.put '/download/*.*', (path, ext) ->
[path, ext].join ' // '
app.post '/', ->
@status 201
@headers 'Content-Type': 'application/json'
JSON.stringify @params
app.get '/legacy', ->
@redirect '/hello'
app.options '/', ->
@headers 'Access-Control-Allow-Origin': '*'
@headers 'Access-Control-Allow-Methods': 'GET, PUT, DELETE'
@render ''
app.eventsource '/stream', ->
@headers 'Access-Control-Allow-Origin': '*'
@headers 'Access-Control-Allow-Methods': 'GET, PUT, DELETE'
interval = Math.round((Number) @params.interval)
buffsize = Math.round((Number) @params.buffsize)
resolution = Math.round((Number) @params.resolution)
repeat interval, =>
points = []
for p in [0..buffsize]
points.push [
Math.round(Math.random() * resolution)
Math.round(Math.random() * resolution)
Math.round(Math.random() * resolution)
Math.round(Math.random())
]
@socket.send JSON.stringify(points)
app.template 'hello.ejs', """
Hello <%= name %>, welcome to <%= site_name() %>! Cookie: “<%= message %>”
"""
app.run process.env.PORT || 5000