Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Koa middleware first draft, not tested #45

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 2 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,8 @@ app.use('/dash', Agendash(agenda));
// ... start your server
```

By mounting Agendash as middleware on a specific path, you may provide your
own authentication for that path. For example if you have an authenticated
session using passport, you can protect the dashboard path like this:

```
app.use('/dash',
function (req, res, next) {
if (!req.user || !req.user.is_admin) {
res.send(401);
} else {
next();
}
},
Agendash(agenda)
);
```

Other middlewares will come soon in the folder `/lib/middlewares/`.
You'll just have to update the last line to require the middleware you need:
Other middlewares will come soon in the folder [`/lib/middlewares/`](https://github.com/joeframbach/agendash/tree/master/lib/middlewares).
You'll just have to update the last line to require the middleware you need:

```js
app.use('/agendash', Agendash(agenda, {
Expand Down
8 changes: 3 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
const path = require('path');

module.exports = (agenda, options) => {
options = options || {};
module.exports = (agenda, options = {}) => {
if (!options.middleware) {
options.middleware = 'express';
}
Expand All @@ -12,7 +10,7 @@ module.exports = (agenda, options) => {
try {
const middlewarePath = path.join(__dirname, 'lib/middlewares', options.middleware);
return require(middlewarePath)(agendash);
} catch (err) {
throw new Error('No middleware available for ' + options.middleware);
} catch (error) {
throw new Error(`No middleware available for ${options.middleware}`);
}
};
33 changes: 33 additions & 0 deletions lib/middlewares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Middlewares
## Koa
Works with [Koajs](https://github.com/koajs/koa) >= v2
### Usage
First install the dependencies in your app:
```bash
yarn add koa koa-bodyparser koa-router koa-static agenda agendash
```
Then use it as follows:
```javascript
const Agenda = require('agenda');
const agendash = require('agendash');
const Koa = require('koa');

const agenda = new Agenda({
// configure Agenda https://github.com/agenda/agenda#configuring-an-agenda
db: {
address: 'mongodb://localhost/agenda'
}
});

const app = new Koa();
app.use(agendash(agenda, {
middleware: 'koa'
// can place other options (e.g. title) here
}))

app.listen(3000, () => {
console.log('App listening on port 3000');
})
```

Use [`koa-mount`](https://github.com/koajs/mount/tree/next) if you need to serve the app from a different route
75 changes: 33 additions & 42 deletions lib/middlewares/express.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
'use strict';
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

module.exports = agendash => {
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', express.static(path.join(__dirname, '../../public')));

app.get('/api', (req, res) => {
agendash.api(req.query.job, req.query.state, (err, apiResponse) => {
if (err) {
return res.status(400).json(err);
}
res.json(apiResponse);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', express.static(path.join(__dirname, '../../public')));

app.get('/api', (req, res, next) => {
agendash.api(req.query.job, req.query.state, (err, apiResponse) => {
if (err) return res.status(400).json(err);
res.json(apiResponse);
});
});
});

app.post('/api/jobs/requeue', (req, res) => {
agendash.requeueJobs(req.body.jobIds, (err, newJobs) => {
if (err || !newJobs) {
return res.status(404).json(err);
}
res.json(newJobs);

app.post('/api/jobs/requeue', (req, res, next) => {
agendash.requeueJobs(req.body.jobIds, function (err, newJobs) {
if (err || !newJobs) return res.status(404).json(err);
res.json(newJobs);
})
})

app.post('/api/jobs/delete', (req, res, next) => {
agendash.deleteJobs(req.body.jobIds, function (err, deleted) {
if (err) return res.status(404).json(err);
return res.json({deleted: true});
})
})

app.post('/api/jobs/create', (req, res, next) => {
agendash.createJob(req.body.jobName, req.body.jobSchedule, req.body.jobRepeatEvery, req.body.jobData, (err, deleted) => {
if (err) return res.status(404).json(err);
return res.json({created: true});
});
});
});

app.post('/api/jobs/delete', (req, res) => {
agendash.deleteJobs(req.body.jobIds, err => {
if (err) {
return res.status(404).json(err);
}
return res.json({deleted: true});
});
});

app.post('/api/jobs/create', (req, res) => {
agendash.createJob(req.body.jobName, req.body.jobSchedule, req.body.jobRepeatEvery, req.body.jobData, err => {
if (err) {
return res.status(404).json(err);
}
return res.json({created: true});
});
});

return app;

return app;
};
77 changes: 77 additions & 0 deletions lib/middlewares/koa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const serve = require('koa-static');
const path = require('path');

module.exports = agendash => {
const parser = bodyParser({
onerror(error, ctx) {
ctx.throw(`cannot parse request body, ${JSON.stringify(error)}`, 400);
}
})
const router = new Router();
const routes = router.routes();
const serveStatic = serve(path.join(__dirname, '../../public'));

router.get('/api', (ctx, next) => {
agendash.api(ctx.request.query.job, ctx.request.query.state, (err, apiResponse) => {
if (err) {
ctx.status = 400
ctx.body = err
} else {
ctx.body = apiResponse
}
next();
})
})

router.post('/api/jobs/requeue', (ctx, next) => {
agendash.requeueJobs(ctx.request.body.jobIds, (err, newJobs) => {
if (err || !newJobs) {
ctx.status = 404;
ctx.body = err;
} else {
ctx.body = newJobs;
}
next();
})
})

router.post('/api/jobs/delete', (ctx, next) => {
agendash.deleteJobs(ctx.request.body.jobIds, (err, deleted) => {
if (err) {
ctx.status = 404;
ctx.body = err;
} else {
ctx.body = {
deleted: true
};
}
next();
})
})

router.post('/api/jobs/create', (ctx, next) => {
agendash.createJob(ctx.request.body.jobName, ctx.request.body.jobSchedule,
ctx.request.body.jobRepeatEvery, ctx.request.body.jobData, (err, deleted) => {
if (err) {
ctx.status = 404;
ctx.body = err;
} else {
ctx.body = {
created: true
};
}
next();
})
})

const agendashMiddleware = (ctx, next) => {
return parser.call(this, ctx, () => routes.call(this, ctx, () => serveStatic.call(this, ctx, next)))
};

// for external access to the router instance
agendashMiddleware.router = router;

return agendashMiddleware;
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"semver": "^5.3.0"
},
"devDependencies": {
"koa-bodyparser": "^3.2.0",
"koa-router": "^7.0.1",
"koa-static": "^3.0.0",
"mocha": "^5.0.0",
"npm-run-all": "^4.1.2",
"supertest": "^3.0.0",
Expand Down