diff options
| -rw-r--r-- | lib/db/index.js | 6 | ||||
| -rw-r--r-- | lib/index.js | 16 |
2 files changed, 19 insertions, 3 deletions
diff --git a/lib/db/index.js b/lib/db/index.js index 9720235..b2ea59c 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -30,6 +30,12 @@ db.getRandom = function () { qb.orderBy(knex.raw('RAND()')).limit(1) }).fetch() } +db.getIndex = function(limit, offset) { + return Image.query(function(qb){ + qb.orderBy("id", "desc").limit(limit) + if (offset) qb.offset(offset) + }).fetch() +} db.createImage = function(url){ return new Image({ url: url }).save() } diff --git a/lib/index.js b/lib/index.js index 5100967..6d07dda 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,14 +30,21 @@ site.init = function(){ console.log('plop listening at http://5.k:%s', server.address().port) }) - app.get("/p/:id", function(req, res){ - res.sendFile("index.html", {root: './public'}) - }) app.get("/p/get/random", function(req, res){ db.getRandom().then(function(img){ res.json(img) }) }) + app.get("/p/list/", function(req, res){ + limit = parseInt(req.params.limit) + if (isNaN(limit)) limit = 20 + else limit = Math.max(limit, 100) + offset = parseInt(req.params.offset) + if (isNaN(offset)) offset = 0 + db.getIndex(limit, offset).then(function(img){ + res.json(img) + }) + }) app.get("/p/get/", function(req, res){ db.getLatest().then(function(img){ res.json(img) @@ -102,6 +109,9 @@ site.init = function(){ }) /* public urls last */ + app.get("/p/:id", function(req, res){ + res.sendFile("index.html", {root: './public'}) + }) app.use('/p/', express.static(path.join(__dirname, '../public'))) } |
