Skip to content

Instantly share code, notes, and snippets.

@young-steveo
Created March 31, 2016 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save young-steveo/791376a86ec2aed7929b73e2c9189d79 to your computer and use it in GitHub Desktop.
Save young-steveo/791376a86ec2aed7929b73e2c9189d79 to your computer and use it in GitHub Desktop.
const path = require('path');
const glob = require('glob');
const express = require('express');
const Bottle = require('bottlejs');
const bottle = Bottle.pop('MyPdfApp'); // name your instance here.
glob.sync(path.join(path.resolve(__dirname), 'app/**/*.js')).forEach((match) => {
require(match); // just require, no need to pass the bottle reference.
});
// everything below is unchanged from your example
// App provider factory
const createApp = function(PdfController) {
const app = express();
app.post('/pdfs/:content_id', PdfController.create);
return app;
}
bottle.service('app', createApp, 'controller.Pdfs');
// grab the application out of the container and start a server
const server = bottle.container.app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Listening at http://%s:%s', host, port);
});
// Factory
function createPdfsController(container) {
// the dependencies required by the service. This is still lazily evaluated,
// and mockable as long as you register the mock before accessing the service
// in your tests.
const PdfService = container.service.PdfGenerator;
return {
create: (req, res, next) => {
const content_id = req.params.content_id;
const pdf_id = PdfService.generate(content_id);
res.json({ pdf_id: pdf_id });
}
};
}
// Export a function that registers the service provider with the given container.
modules.export = () => {
require('bottlejs').pop('MyPdfApp').factory( // get the named container.
'controller.Pdfs', // the name of the service we're providing
createPdfsController // the factory to create the service instance
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment