1
0
mirror of https://github.com/yldio/copilot.git synced 2024-12-01 07:30:07 +02:00
copilot/legacy/spikes/compose-demo/server.js
Sérgio Ramos 8295bd6882 chore: initial lerna setup
this shall be a progressive process
2017-05-25 10:56:50 +01:00

59 lines
1.3 KiB
JavaScript

const DockerComposeClient = require('./vendor/docker-compose-client');
const Router = require('router');
const { createServer } = require('http');
const bodyParser = require('body-parser');
const next = require('next');
const TritonWatch = require('triton-watch');
const app = next({
dev: process.env.NODE_ENV !== 'production'
});
const tritonWatch = new TritonWatch({
frequency: 16,
triton: {
profileName: 'sw1',
configDir: '~/.triton/'
}
});
const containers = {};
const handle = app.getRequestHandler();
const client = new DockerComposeClient();
const router = Router();
router.use(bodyParser.json());
tritonWatch.on('change', container => (containers[container.id] = container));
router.get('/api/status', (req, res) => {
res.end(JSON.stringify(containers));
});
router.post('/api/provision', (req, res) => {
client
.provision({
projectName: req.body.name,
manifest: req.body.manifest
})
.then(status => res.end(JSON.stringify(status)));
});
router.post('/api/scale', (req, res) => {
client
.provision({
projectName: req.body.name,
manifest: req.body.manifest,
services: req.body.services
})
.then(status => res.end(JSON.stringify(status)));
});
tritonWatch.poll();
app.prepare().then(() =>
createServer((req, res) => {
router(req, res, () => handle(req, res));
}).listen(3000)
);