initial done

This commit is contained in:
2017-08-29 16:11:45 +02:00
parent 6f98b217ac
commit 27461dd3f5
10 changed files with 101 additions and 68 deletions

View File

@@ -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);
}
},
];