From 27461dd3f5d71666f223e28c33203f99c39634d9 Mon Sep 17 00:00:00 2001 From: hellerve Date: Tue, 29 Aug 2017 16:11:45 +0200 Subject: [PATCH] initial done --- model.js | 24 ++++++++++++++++ model.json | 1 + package.json | 3 +- public/404.css | 0 public/script.js | 4 +-- public/style.css | 5 ---- routes.js | 30 ++++++------------- shrt.js | 18 ++++-------- views/404.html | 9 ++++++ views/index.html | 75 ++++++++++++++++++++++++++++++++---------------- 10 files changed, 101 insertions(+), 68 deletions(-) create mode 100644 model.js create mode 100644 model.json delete mode 100644 public/404.css delete mode 100644 public/style.css diff --git a/model.js b/model.js new file mode 100644 index 0000000..c4e5996 --- /dev/null +++ b/model.js @@ -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, +} diff --git a/model.json b/model.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/model.json @@ -0,0 +1 @@ +[] diff --git a/package.json b/package.json index bb8215a..f6cf11d 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "dependencies": { "hapi": "^14.1.0", "inert": "^4.0.1", - "joi": "^9.0.4", - "mongoose": "^4.5.8" + "joi": "^9.0.4" } } diff --git a/public/404.css b/public/404.css deleted file mode 100644 index e69de29..0000000 diff --git a/public/script.js b/public/script.js index 6bd1446..26509f1 100644 --- a/public/script.js +++ b/public/script.js @@ -4,9 +4,9 @@ var link = document.getElementById('link'); var shrBox = document.getElementById('shortened'); function displayShortenedUrl(response) { - link.textContent = response.data.shortUrl; + link.textContent = response.data.url; link.setAttribute( - 'href', response.data.shortUrl + 'href', response.data.url ); shrBox.style.opacity = '1'; urlBox.value = ''; diff --git a/public/style.css b/public/style.css deleted file mode 100644 index 3a1fdda..0000000 --- a/public/style.css +++ /dev/null @@ -1,5 +0,0 @@ -#shorten-form { - width: 300px; - margin: auto; - margin-top: 10vh; -} \ No newline at end of file diff --git a/routes.js b/routes.js index 198d51b..c61f773 100644 --- a/routes.js +++ b/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); } }, ]; diff --git a/shrt.js b/shrt.js index 1d2c817..bdaa6c8 100644 --- a/shrt.js +++ b/shrt.js @@ -1,8 +1,6 @@ const Hapi = require('hapi'); const server = new Hapi.Server(); const routes = require('./routes'); -const mongoose = require('mongoose'); -const mongoUri = process.env.MONGOURI || 'mongodb://localhost/shortio'; const options = { server: { @@ -12,23 +10,17 @@ const options = { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } }; -mongoose.connect(mongoUri, options); - -const db = mongoose.connection; server.connection({ port: process.env.PORT || 3000, routes: { cors: true } }); 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 => { - if (err) throw err; + server.start(err => { + if (err) throw err; - console.log(`Server running at port ${server.info.port}`); - }); - }); + console.log(`Server running at port ${server.info.port}`); + }); }); diff --git a/views/404.html b/views/404.html index e69de29..73e3eeb 100644 --- a/views/404.html +++ b/views/404.html @@ -0,0 +1,9 @@ + + + + HTTP 404 + + +

HTTP 404 - Not Found

+ + diff --git a/views/index.html b/views/index.html index e8f405b..667e1dc 100644 --- a/views/index.html +++ b/views/index.html @@ -1,37 +1,64 @@ - shrt + ve.it - + -
-
-
-
- - -
-
-
-
-

- Your shortened link: - Loading... -

-

- Right-click or Tap & Hold the link and copy the address to clipboard and start using it. -

-
-
-
+
+ + +
+
+
+
+

+ Shortened Link: + +

+
-
+ - +