initial done
This commit is contained in:
24
model.js
Normal file
24
model.js
Normal 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
1
model.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
[]
|
@@ -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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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 = '';
|
||||||
|
@@ -1,5 +0,0 @@
|
|||||||
#shorten-form {
|
|
||||||
width: 300px;
|
|
||||||
margin: auto;
|
|
||||||
margin-top: 10vh;
|
|
||||||
}
|
|
30
routes.js
30
routes.js
@@ -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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
8
shrt.js
8
shrt.js
@@ -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,17 +10,12 @@ 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:'))
|
|
||||||
.once('open', () => {
|
|
||||||
server.route(routes);
|
server.route(routes);
|
||||||
|
|
||||||
server.start(err => {
|
server.start(err => {
|
||||||
@@ -30,5 +23,4 @@ server.register(require('inert'), (err) => {
|
|||||||
|
|
||||||
console.log(`Server running at port ${server.info.port}`);
|
console.log(`Server running at port ${server.info.port}`);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
@@ -0,0 +1,9 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>HTTP 404</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>HTTP 404 - Not Found</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
@@ -1,15 +1,47 @@
|
|||||||
<!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>
|
|
||||||
<main>
|
|
||||||
<div class="container">
|
|
||||||
<form id="shorten-form" method="POST">
|
<form id="shorten-form" method="POST">
|
||||||
<input id="url" type="text" placeholder="URL" name="url">
|
<input id="url" type="text" placeholder="URL" name="url">
|
||||||
<input id="submit" type="submit" value="Shorten!">
|
<input id="submit" type="submit" value="Shorten!">
|
||||||
@@ -18,20 +50,15 @@
|
|||||||
<div class="column">
|
<div class="column">
|
||||||
<section id="shortened">
|
<section id="shortened">
|
||||||
<p>
|
<p>
|
||||||
<em>Your shortened link: </em>
|
<span>Shortened Link:</span>
|
||||||
<a id="link" href="#" target="_blank">Loading...</a>
|
<a id="link" href="#" target="_blank"></a>
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
Right-click or Tap & Hold the link and copy the address to clipboard and start using it.
|
|
||||||
</p>
|
</p>
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<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>
|
||||||
|
Reference in New Issue
Block a user