initial done
This commit is contained in:
30
routes.js
30
routes.js
@@ -1,16 +1,9 @@
|
||||
const Joi = require('joi');
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
const hash = require('./hash');
|
||||
const model = require('./model');
|
||||
const HASHLEN = 7;
|
||||
|
||||
const baseUrl = process.env.BASE_URL || 'shrt.in';
|
||||
|
||||
const schema = new Schema({
|
||||
shrt: String,
|
||||
url: String,
|
||||
});
|
||||
const Shrt = mongoose.model('Shrt', schema);
|
||||
const baseUrl = process.env.BASE_URL || 'http://ve.it';
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
@@ -32,12 +25,10 @@ module.exports = [
|
||||
path: '/new',
|
||||
handler(request, reply) {
|
||||
const uid = hash(HASHLEN);
|
||||
const r = new Shrt({
|
||||
short: `${baseUrl}/${uniqueID}`,
|
||||
url: request.payload.url,
|
||||
});
|
||||
const url = `${baseUrl}/${uid}`;
|
||||
model.save(request.payload.url, url);
|
||||
|
||||
r.save((err, redir) => err ? reply(err) : reply(redir));
|
||||
reply({url: url});
|
||||
},
|
||||
config: {
|
||||
validate: {
|
||||
@@ -53,14 +44,9 @@ module.exports = [
|
||||
method: 'GET',
|
||||
path:'/{hash}',
|
||||
handler(request, reply) {
|
||||
const query = {
|
||||
'short': `${baseUrl}/${request.params.hash}`
|
||||
};
|
||||
Shrt.findOne(query, (err, shrt) => {
|
||||
if (err) return reply(err);
|
||||
else if (shrt) reply().redirect(shrt.url);
|
||||
else reply.file('views/404.html').code(404);
|
||||
});
|
||||
const obj = model.search(`${baseUrl}/${request.params.hash}`);
|
||||
if (!obj) return reply.file('views/404.html').code(404);
|
||||
reply().redirect(obj.url);
|
||||
}
|
||||
},
|
||||
];
|
||||
|
Reference in New Issue
Block a user