53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
const Joi = require('joi');
|
|
const hash = require('./hash');
|
|
const model = require('./model');
|
|
const HASHLEN = 7;
|
|
|
|
const baseUrl = process.env.BASE_URL || 'http://ve.it';
|
|
|
|
module.exports = [
|
|
{
|
|
method: 'GET',
|
|
path: '/',
|
|
handler(request, reply) {
|
|
reply.file('views/index.html');
|
|
}
|
|
},
|
|
{
|
|
method: 'GET',
|
|
path: '/public/{file}',
|
|
handler(request, reply) {
|
|
reply.file(`public/${request.params.file}`);
|
|
}
|
|
},
|
|
{
|
|
method: 'POST',
|
|
path: '/new',
|
|
handler(request, reply) {
|
|
const uid = hash(HASHLEN);
|
|
const url = `${baseUrl}/${uid}`;
|
|
model.save(request.payload.url, url);
|
|
|
|
reply({url: url});
|
|
},
|
|
config: {
|
|
validate: {
|
|
payload: {
|
|
url: Joi.string()
|
|
.regex(/^https?:\/\/([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)
|
|
.required()
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
method: 'GET',
|
|
path:'/{hash}',
|
|
handler(request, reply) {
|
|
const obj = model.search(`${baseUrl}/${request.params.hash}`);
|
|
if (!obj) return reply.file('views/404.html').code(404);
|
|
reply().redirect(obj.url);
|
|
}
|
|
},
|
|
];
|