Created
June 20, 2017 11:41
-
-
Save Blackening999/00369f075baca1ca22316da7d42d95b9 to your computer and use it in GitHub Desktop.
/var/www/testapp/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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