-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (63 loc) · 2.02 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
'use strict';
const Koa = require('koa');
const coBody = require('co-body');
const logger = require('koa-logger');
const Router = require('koa-router');
const fs = require('fs');
const { ParseDockerStream, EncodeDockerStream } = require('./transform');
const app = new Koa();
const router = new Router();
const driver = require('./driver')();
router.get('/', ctx => {
ctx.body = {
Implements: ['LogDriver'],
};
});
router.post('/LogDriver.StartLogging', async ctx => {
const stream = fs.createReadStream(ctx.request.body.File).pipe(new ParseDockerStream());
console.dir({ body: ctx.request.body }, { depth: null });
const { File, Info } = ctx.request.body;
ctx.body = await driver.startLogging(stream, File, Info);
});
router.post('/LogDriver.StopLogging', async ctx => {
const { File } = ctx.request.body;
ctx.body = await driver.stopLogging(File);
});
router.post('/LogDriver.Capabilities', ctx => {
ctx.body = {
Cap: {
ReadLogs: !!driver.readLogs,
},
};
});
router.post('/LogDriver.ReadLogs', async ctx => {
ctx.type = 'application/x-json-stream';
console.log({ body: ctx.request.body }, { depth: null });
const { Info, Config } = ctx.request.body;
const stream = (await driver.readLogs(Info, Config))
.pipe(new EncodeDockerStream());
ctx.status = 200;
ctx.response.flushHeaders();
stream.resume();
ctx.body = stream;
});
app
.use(logger())
.use(async (ctx, next) => {
try {
return await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = { Err: err.message };
ctx.app.emit('error', err, ctx);
}
})
.use(async (ctx, next) => {
ctx.request.body = await coBody.json(ctx.req, { limit: '10kb' });
return next();
})
.use(router.routes())
.use(router.allowedMethods())
.listen(process.env.PORT || '/run/docker/plugins/h1-journal.sock', function () {
console.log('listening on', this.address());
});