| author | Tomas Zeman <tzeman@volny.cz> |
| Sun, 15 Dec 2013 17:14:26 +0100 | |
| changeset 5 | ed2abdd11a1b |
| parent 4 | cbba85b1f677 |
| permissions | -rw-r--r-- |
| 1 | 1 |
Q = require 'q' |
2 |
Http = require 'q-io/http' |
|
3 |
Apps = require 'q-io/http-apps' |
|
4 |
bunyan = require 'bunyan' |
|
5 |
||
6 |
log = bunyan.createLogger {
|
|
7 |
name: 'asa-test-server' |
|
8 |
level: 'debug' |
|
9 |
} |
|
10 |
||
| 4 | 11 |
noContent = |
12 |
status: 204 |
|
13 |
headers: {}
|
|
14 |
||
| 1 | 15 |
route = Apps.Branch {
|
| 4 | 16 |
### |
| 1 | 17 |
'ok': (rq,rs) => {
|
18 |
status: 200 |
|
19 |
headers: {}
|
|
20 |
data: |
|
21 |
result: 'ok' |
|
22 |
request: |
|
23 |
path: rq.path |
|
24 |
pathInfo: rq.pathInfo |
|
25 |
response: rs |
|
26 |
} |
|
27 |
'post': Apps.Method {
|
|
| 2 | 28 |
'POST': Apps.JsonRequest((o, rq, rs) => {
|
| 1 | 29 |
status: 200 |
30 |
headers: {}
|
|
31 |
data: |
|
| 2 | 32 |
data: o |
| 1 | 33 |
request: |
34 |
path: rq.path |
|
35 |
pathInfo: rq.pathInfo |
|
36 |
response: rs |
|
| 2 | 37 |
}) |
| 1 | 38 |
}, Apps.methodNotAllowed |
| 4 | 39 |
### |
40 |
||
|
3
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
41 |
'login': Apps.Method {
|
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
42 |
'POST': Apps.JsonRequest((o, rq, rs) => |
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
43 |
log.info("Login", o)
|
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
44 |
{
|
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
45 |
status: 200 |
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
46 |
headers: {}
|
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
47 |
data: |
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
48 |
configId: 'cfg1' |
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
49 |
sessionId: 'sess1' |
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
50 |
}) |
| 4 | 51 |
}, Apps.methodNotAllowed |
52 |
||
53 |
'orders': (rq, rs) => {
|
|
54 |
status: 200 |
|
55 |
headers: {}
|
|
56 |
data: |
|
57 |
orders: [{
|
|
58 |
orderId: 123 |
|
59 |
name: 'Order 1' |
|
60 |
description: 'Description of order 1' |
|
61 |
}, {
|
|
62 |
orderId: 554 |
|
63 |
name: 'Order 2' |
|
64 |
description: 'Description of order 2' |
|
65 |
}] |
|
|
3
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
66 |
} |
| 4 | 67 |
|
68 |
'event': Apps.Method {
|
|
69 |
'POST': Apps.JsonRequest((o, rq, rs) => |
|
70 |
log.info("Event", o)
|
|
71 |
noContent |
|
72 |
) |
|
73 |
}, Apps.methodNotAllowed |
|
74 |
||
75 |
'ping': Apps.Method {
|
|
76 |
'POST': Apps.JsonRequest((o, rq, rs) => |
|
77 |
log.info("Ping", o)
|
|
78 |
noContent |
|
79 |
) |
|
80 |
}, Apps.methodNotAllowed |
|
81 |
||
82 |
'log': Apps.Method {
|
|
83 |
'POST': Apps.JsonRequest((o, rq, rs) => |
|
84 |
log.info("Ping", o)
|
|
85 |
noContent |
|
86 |
) |
|
87 |
'GET': (rq, rs) => {
|
|
88 |
status: 200 |
|
89 |
headers: {}
|
|
90 |
data: |
|
91 |
lastLog: new Date().toISOString() |
|
92 |
} |
|
93 |
}, Apps.methodNotAllowed |
|
| 1 | 94 |
} |
95 |
||
|
3
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
96 |
app = Apps.Chain() |
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
97 |
.use(() => Apps.Log(Apps.HandleJsonResponses(route, null, " "), |
|
0fa2f12fcb3c
/login handler; request logging via bunyan
Tomas Zeman <tzeman@volny.cz>
parents:
2
diff
changeset
|
98 |
(m) => log.info("Access: " + m)))
|
| 1 | 99 |
.end() |
100 |
||
101 |
Http.Server(app).listen(3000) |
|
102 |
||
103 |
# vim: et ts=2 sw=2 |