This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-http.js
125 lines (109 loc) · 3.47 KB
/
server-http.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
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const Spredis = require('./lib/spredis/Spredis');
function go(config) {
const Koa = require('koa');
const Router = require('koa-router');
const body = require('koa-json-body')
const spredis = new Spredis(config ? config.redis : null);
const respondError = (ctx, e) => {
ctx.response.status = 500;
ctx.response.type='json';
ctx.response.body = {error: e.stack};
}
spredis.initialize().then( () => {
// let ns = spredis.defaultNamespace;
const app = new Koa();
app.use(body({ limit: '512kb', fallback: true }))
const router = new Router();
let prefix = config.urlPrefix || '';
// router.get('/', (ctx, next) => {
// ctx.response.body = 'Looking for something?';
// });
router.post(`${prefix}/:ns/search`, async (ctx, next) => {
try {
let ns = await spredis.useNamespace(ctx.params.ns);
if (!ns) throw new Error(`Can not find namespace: '${ctx.params.ns}'`);
let res = await ns.search(ctx.request.body);
ctx.response.type='json';
ctx.response.body = res;
} catch (e) {
respondError(ctx, e);
}
});
router.post(`${prefix}/:ns/addDocuments`, async (ctx, next) => {
try {
let ns = await spredis.useNamespace(ctx.params.ns);
if (!ns) throw new Error(`Can not find namespace: '${ctx.params.ns}'`);
let res = await ns.addDocuments(ctx.request.body);
ctx.response.type='json';
ctx.response.body = res;
} catch (e) {
respondError(ctx, e);
}
});
router.post(`${prefix}/:ns/deleteDocuments`, async (ctx, next) => {
try {
let ns = await spredis.useNamespace(ctx.params.ns);
if (!ns) throw new Error(`Can not find namespace: '${ctx.params.ns}'`);
let res = await ns.deleteDocuments(ctx.request.body);
ctx.response.type='json';
ctx.response.body = res;
} catch (e) {
respondError(ctx, e);
}
});
router.post(`${prefix}/createNamespace`, async (ctx, next) => {
try {
let res = await spredis.createNamespace(ctx.request.body);
let id = ctx.params.id;
ctx.response.type='json';
ctx.response.body = res;
} catch (e) {
respondError(ctx, e);
}
});
router.get(`${prefix}/:ns/doc/:id`, async (ctx, next) => {
try {
let ns = await spredis.useNamespace(ctx.params.ns);
let id = ctx.params.id;
if (!ns) throw new Error(`Can not find namespace: '${ctx.params.ns}'`);
if (!id === null || id === undefined) throw new Error(`No id supplied'`);
let res = await ns.getDocument(id);
ctx.response.type='json';
ctx.response.body = res;
} catch (e) {
respondError(ctx, e);
}
});
router.get(`${prefix}/:ns/namespaceConfig`, async (ctx, next) => {
try {
let ns = await spredis.useNamespace(ctx.params.ns);
if (!ns) throw new Error(`Can not find namespace: '${ctx.params.ns}'`);
let res = await ns.getNamespaceConfig();
ctx.response.type='json';
ctx.response.body = res;
} catch (e) {
respondError(ctx, e);
}
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(process.env.PORT || config.port || 5268);
});
}
module.exports = function(config) {
if (cluster.isMaster && numCPUs > 1) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
go(config);
console.log(`Worker ${process.pid} started`);
}
}