-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.ls
58 lines (46 loc) · 1.41 KB
/
root.ls
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
/**
* User: gisborne
* Date: 9/21/14
* Time: 13:57
*
* Root scope. Also may be used as prototype for other scopes
*/
require! method_override: 'method-override'
require! bodyParser: 'body-parser'
require! path
require! _: 'prelude-ls'
require! './db'
require! scp: './scope'
export scope = new scp.scope
require! './rootFns'
require! './request'
getMethod = (req) ->
if (method = req.query['_method']) && method.match(/put/i)
'put'
else
req.method
getScope = db.getModelScope
export rootHandle = (req, res, next) ->
method = getMethod req
url = req._parsedUrl.pathname #TODO find an official alternative
url_parts = url.split '/' |> _.tail #We throw away the inevitable blank before the opening /
handle method, url_parts, req, res, next
#GET '/' is probably the only case of this
defaultHandle = (r) ->
r.res.send 'foo ' + r.req.url #ToDo What do we do with GET '/'?
#Polish request details up for standard handle call
handle = (method, url, req, res, next) ->
next_scope_name = _.head url
r = request.create(
method: method
req: req
res: res
next: next
)
if next_scope_name !== ''
r.url = _.tail url
next_scope = getScope next_scope_name
next_scope.handle r, -> #method, scope, rest_url, req, res, -> #next line is the "next" handler
throw new Error 'Unrecognized scope name: ' + next_scope_name
else
defaultHandle r #This is usually GET '/'