-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
201 lines (184 loc) · 4.31 KB
/
index.js
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
class IOCore {
constructor (ioFunc) {
this.then = cb => ioFunc((...args) => { cb(...args) });
};
reject (pred) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let result = pred(...args);
if (result !== null) {
if (Array.isArray(result)) {
cb(...result);
} else {
cb(result);
}
};
});
};
return this;
};
mayBeFalse (mv, handler) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let result = mv(...args);
if (result === false) {
handler(...args);
} else {
cb(...args);
}
});
};
return this;
};
mayBeNull (mv, handler) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let result = mv(...args)
if (result === null) {
handler(...args);
} else {
cb(...args);
}
});
};
return this;
};
mayBeErr (mv, handler) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let result = mv(...args);
if (result instanceof Error) {
handler(...args);
} else {
cb(...args);
}
});
};
return this;
};
mayBeTrue (mv, handler) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let result = mv(...args);
if (result === true) {
handler(...args);
} else {
cb(...args);
}
});
};
return this;
};
mayBeUndefined (mv, handler) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let result = mv(...args);
if (result === undefined) {
handler(...args);
} else {
cb(...args);
}
});
};
return this;
};
map (transform) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let result = transform(...args);
if (Array.isArray(result)) {
cb(...result);
} else {
cb(result);
}
});
};
return this;
};
bind (ioFunc) {
let saveThen = this.then;
this.then = cb => {
saveThen((...args) => {
let io = ioFunc(...args);
io.then((...ioargs) => cb(...args, ...ioargs));
});
};
return this;
};
static timer (s) {
var intervalId;
var timer = new IOCore(cb => {
intervalId = setInterval(cb, Math.floor(s * 1000))
});
timer.clear = () => clearInterval(intervalId);
return timer;
};
static createIO (ioFunc) {
return new IOCore(ioFunc);
};
};
const readline = require('readline');
const fs = require('fs');
const rlConfig = {
input: process.stdin,
output: process.stdout
}; /* Config for readline interface */
class IO extends IOCore {
static getLine (str) {
const rl = readline.createInterface(rlConfig);
return new IOCore(cb => rl.question(str, cb))
.map(data => {
rl.close();
return data;
});
};
static putLine (...data) {
return new IOCore(cb => process.nextTick(cb, data))
.map(data => {
console.log(...data);
return data
});
};
static readFile (filename) {
return new IOCore(cb => fs.readFile(filename, cb))
.map((_, data) => data.toString());
};
static writeFile (filename, data) {
return new IOCore(cb => fs.writeFile(filename, data, cb));
};
};
const express = require('express');
const path = require('path');
const sessions = require('client-sessions');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(sessions({
cookieName: 'session',
secret: 'mysecret',
duration: 7 * 24 * 60 * 60 * 1000,
activeDuration: 24 * 60 * 60 * 1000
}));
IO.createIO(cb => app.get('/', cb)).mayBeUndefined((req, res) => req.session.user, (req, res) => res.redirect('/login')).then((req, res) => {
res.send('hello world');
});
IO.createIO(cb => app.get('/login', cb)).then((req, res) => {
res.send('login page');
});
IO.createIO(cb => app.get('/logout', cb)).map((req, res, _) => {
(delete req.session.user)
return [
req,
res,
_
];
}).then((req, res, _) => {
res.redirect('/');
});
app.listen(3000);