ButlerBin/server.js

97 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2020-06-08 20:56:17 +01:00
var Connect = require("connect");
var ConnectSt = require("st");
var Fs = require("fs");
var Http = require("http");
var Route = require("connect-route");
var Winston = require("winston");
var DocumentHandler = require("./lib/document_handler");
var KeyGenerator = require("./lib/key_generator");
var Store = require("./lib/document_store");
// Load the configuration and set some defaults.
var config = JSON.parse(Fs.readFileSync("./config.js", "utf8"));
config.host = config.host || "127.0.0.1";
config.port = config.port || 7777;
// Set up the logger.
2011-11-18 22:26:25 +00:00
if (config.logging) {
try {
2020-06-08 20:56:17 +01:00
Winston.remove(Winston.transports.Console);
2017-06-26 17:38:17 +01:00
} catch(e) {
2020-06-08 20:56:17 +01:00
// This is fine.
2017-06-26 17:38:17 +01:00
}
2011-11-18 22:26:25 +00:00
var detail, type;
for (var i = 0; i < config.logging.length; i++) {
detail = config.logging[i];
type = detail.type;
delete detail.type;
2020-06-08 20:56:17 +01:00
Winston.add(Winston.transports[type], detail);
2011-11-18 22:26:25 +00:00
}
}
2011-11-18 15:17:41 +00:00
2020-06-08 20:56:17 +01:00
// Configure the data store.
2020-06-09 01:26:48 +01:00
store = new Store({
username: config.mongodbUsername,
password: config.mongodbPassword,
dbName: config.mongodbDbName,
collectionName: config.mongodbCollectionName
});
2020-06-08 20:56:17 +01:00
// Pick up a key generator.
var keyGenerator = new KeyGenerator({
keyLength: config.keyLength,
keySpace: config.keySpace
});
2020-06-08 20:56:17 +01:00
// Configure the document handler.
2011-11-22 14:22:37 +00:00
var documentHandler = new DocumentHandler({
2020-06-08 20:56:17 +01:00
store: store,
keyLength: config.keyLength,
keyGenerator: keyGenerator
2011-11-22 14:22:37 +00:00
});
2020-06-08 20:56:17 +01:00
var app = Connect();
2016-03-06 21:20:40 +00:00
2020-06-08 20:56:17 +01:00
// Configure the API routes.
app.use(Route(function(router) {
router.get("/raw/:id", function(request, response) {
var key = request.params.id.split(".")[0];
return documentHandler.handleRawGet(key, response);
2016-03-06 21:20:40 +00:00
});
2020-06-08 20:56:17 +01:00
router.post("/documents", function(request, response) {
2016-03-06 21:20:40 +00:00
return documentHandler.handlePost(request, response);
});
2020-06-08 20:56:17 +01:00
router.get("/documents/:id", function(request, response) {
var key = request.params.id.split(".")[0];
return documentHandler.handleGet(key, response);
2016-03-06 21:20:40 +00:00
});
}));
2020-06-08 20:56:17 +01:00
// Route static files.
app.use(ConnectSt({
path: __dirname + "/static",
2016-03-06 21:20:40 +00:00
content: { maxAge: config.staticMaxAge },
passthrough: true,
index: false
}));
2020-06-08 20:56:17 +01:00
// All the pastebin IDs.
app.use(Route(function(router) {
router.get("/:id", function(request, response, next) {
request.sturl = "/";
2016-03-06 21:20:40 +00:00
next();
});
}));
2020-06-08 20:56:17 +01:00
// Route index.html.
app.use(ConnectSt({
path: __dirname + "/static",
2016-03-06 21:20:40 +00:00
content: { maxAge: config.staticMaxAge },
2020-06-08 20:56:17 +01:00
index: "index.html"
2016-03-06 21:20:40 +00:00
}));
2020-06-08 20:56:17 +01:00
Http.createServer(app).listen(config.port, config.host);
2011-11-18 21:57:23 +00:00
2020-06-08 20:56:17 +01:00
Winston.info("Listening on " + config.host + ":" + config.port);