Skip to content

Instantly share code, notes, and snippets.

@Blackening999
Created June 20, 2017 11:41
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 Blackening999/00369f075baca1ca22316da7d42d95b9 to your computer and use it in GitHub Desktop.
Save Blackening999/00369f075baca1ca22316da7d42d95b9 to your computer and use it in GitHub Desktop.
/var/www/testapp/index.js
'use strict';
const redis = require('redis'),
co = require('co'),
coRedis = require('co-redis'),
Koa = require('koa'),
config = require('./app.json');
const app = exports.app = new Koa(),
client = redis.createClient(
config.env.REDIS_PORT.value,
config.env.REDIS_HOST.value
),
dbCo = coRedis(client);
if (config.env.REDIS_SECRET.value) {
client.auth(config.env.REDIS_SECRET.value);
}
client.on('error', function (err) {
console.log('Redis client error: ' + err);
});
app.use(co.wrap(function* (ctx) {
var indexkey;
if (ctx.request.query.index_key) {
indexkey = config.env.APP_NAME.value +':'+ ctx.request.query.index_key;
} else {
indexkey = config.env.APP_NAME.value +':current-content';
}
var index = yield dbCo.get(indexkey);
if (index) {
ctx.body = index;
} else {
ctx.status = 404;
}
}));
var server = app.listen(process.env.PORT || 8782);
process.on('SIGINT', function () {
server.close(function () {
process.exit(0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment