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

24
model.js Normal file
View File

@@ -0,0 +1,24 @@
const fs = require("fs");
let urls = require("./model.json");
function save(url, shortened) {
urls.push({url: url, shortened: shortened});
commit();
}
function commit() {
fs.writeFile("./model.json", JSON.stringify(urls));
}
function search(key) {
for (let model of urls) {
if (key == model.shortened) return model;
}
return null;
}
module.exports = {
save: save,
search: search,
}

1
model.json Normal file
View File

@@ -0,0 +1 @@
[]

View File

@@ -15,7 +15,6 @@
"dependencies": { "dependencies": {
"hapi": "^14.1.0", "hapi": "^14.1.0",
"inert": "^4.0.1", "inert": "^4.0.1",
"joi": "^9.0.4", "joi": "^9.0.4"
"mongoose": "^4.5.8"
} }
} }

View File

View File

@@ -4,9 +4,9 @@ var link = document.getElementById('link');
var shrBox = document.getElementById('shortened'); var shrBox = document.getElementById('shortened');
function displayShortenedUrl(response) { function displayShortenedUrl(response) {
link.textContent = response.data.shortUrl; link.textContent = response.data.url;
link.setAttribute( link.setAttribute(
'href', response.data.shortUrl 'href', response.data.url
); );
shrBox.style.opacity = '1'; shrBox.style.opacity = '1';
urlBox.value = ''; urlBox.value = '';

View File

@@ -1,5 +0,0 @@
#shorten-form {
width: 300px;
margin: auto;
margin-top: 10vh;
}

View File

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

18
shrt.js
View File

@@ -1,8 +1,6 @@
const Hapi = require('hapi'); const Hapi = require('hapi');
const server = new Hapi.Server(); const server = new Hapi.Server();
const routes = require('./routes'); const routes = require('./routes');
const mongoose = require('mongoose');
const mongoUri = process.env.MONGOURI || 'mongodb://localhost/shortio';
const options = { const options = {
server: { server: {
@@ -12,23 +10,17 @@ const options = {
socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 }
} }
}; };
mongoose.connect(mongoUri, options);
const db = mongoose.connection;
server.connection({ server.connection({
port: process.env.PORT || 3000, port: process.env.PORT || 3000,
routes: { cors: true } routes: { cors: true }
}); });
server.register(require('inert'), (err) => { server.register(require('inert'), (err) => {
db.on('error', console.error.bind(console, 'connection error:')) server.route(routes);
.once('open', () => {
server.route(routes);
server.start(err => { server.start(err => {
if (err) throw err; if (err) throw err;
console.log(`Server running at port ${server.info.port}`); console.log(`Server running at port ${server.info.port}`);
}); });
});
}); });

View File

@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>HTTP 404</title>
</head>
<body>
<p>HTTP 404 - Not Found</p>
</body>
</html>

View File

@@ -1,37 +1,64 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>shrt</title> <title>ve.it</title>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="public/style.css"> <style>
* {
font-family: Sans-Serif;
}
#shorten-form {
margin: auto;
width: 500px;
margin-top: 20vh;
}
input {
border: none;
border-bottom: 3px solid #000;
border-left: 3px solid #000;
padding: 5px;
padding-bottom: 1px;
margin: 10px;
width: 250px;
font-size: 16px;
}
#submit {
border: none;
padding: 5px;
font-size: 16px;
width: 100px;
background: #333;
color: #fff;
}
#shortened {
width: 480px;
margin: auto;
font-size: 20px;
}
a {
color: #000;
}
</style>
</head> </head>
<body> <body>
<header> <form id="shorten-form" method="POST">
<main> <input id="url" type="text" placeholder="URL" name="url">
<div class="container"> <input id="submit" type="submit" value="Shorten!">
<form id="shorten-form" method="POST"> </form>
<input id="url" type="text" placeholder="URL" name="url"> <div class="row">
<input id="submit" type="submit" value="Shorten!"> <div class="column">
</form> <section id="shortened">
<div class="row"> <p>
<div class="column"> <span>Shortened Link:</span>
<section id="shortened"> <a id="link" href="#" target="_blank"></a>
<p> </p>
<em>Your shortened link: </em> </section>
<a id="link" href="#" target="_blank">Loading...</a>
</p>
<p>
Right-click or Tap &amp; Hold the link and copy the address to clipboard and start using it.
</p>
</section>
</div>
</div>
</div> </div>
</main> </div>
<script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.min.js"></script> <script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.min.js"></script>
<script src="https://npmcdn.com/axios/dist/axios.min.js"></script> <script src="https://npmcdn.com/axios/dist/axios.min.js"></script>
<script src="public/scripts.js"></script> <script src="public/script.js"></script>
</body> </body>
</html> </html>