2017-08-05 20:14:17 +00:00
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
|
|
|
const path = require('path');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const config = require('./config');
|
|
|
|
const base58 = require('./base58.js');
|
|
|
|
const log4js = require('log4js');
|
|
|
|
const logger = log4js.getLogger();
|
2017-08-05 20:54:35 +00:00
|
|
|
const compression = require('compression');
|
2017-08-07 21:21:36 +00:00
|
|
|
const favicon = require('express-favicon');
|
2017-08-05 20:54:35 +00:00
|
|
|
|
2017-08-05 20:14:17 +00:00
|
|
|
logger.level = 'debug';
|
2016-02-07 21:16:07 +00:00
|
|
|
|
|
|
|
// grab the url model
|
2017-08-05 20:14:17 +00:00
|
|
|
const Url = require('./models/url');
|
2016-02-07 21:16:07 +00:00
|
|
|
|
2017-08-05 22:28:59 +00:00
|
|
|
let isProduction = false;
|
2020-04-17 13:27:14 +00:00
|
|
|
let sitePath = 'public/v2';
|
|
|
|
let indexView = 'index.html';
|
2017-09-25 16:48:55 +00:00
|
|
|
const listView = 'views/list.html';
|
2017-08-05 22:28:59 +00:00
|
|
|
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'dev';
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'prod') {
|
|
|
|
isProduction = true;
|
2020-04-17 13:27:14 +00:00
|
|
|
sitePath = 'public/v2';
|
|
|
|
indexView = 'index.html';
|
2017-09-05 14:53:07 +00:00
|
|
|
config.webhost = 'http://nurl.co/';
|
2017-08-05 22:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn(`isProduction:${isProduction}`);
|
|
|
|
|
2017-09-25 16:48:55 +00:00
|
|
|
mongoose.connect(`mongodb://${ config.db.host }/${ config.db.name}`);
|
2017-08-05 20:54:35 +00:00
|
|
|
app.use(compression());
|
2016-02-07 21:16:07 +00:00
|
|
|
app.use(bodyParser.json());
|
2017-09-25 16:48:55 +00:00
|
|
|
app.use(bodyParser.urlencoded({ 'extended': true }));
|
2016-02-07 21:16:07 +00:00
|
|
|
|
2017-08-05 22:28:59 +00:00
|
|
|
app.use(express.static(path.join(__dirname, sitePath)));
|
2020-04-17 13:27:14 +00:00
|
|
|
// app.use(favicon(`${__dirname }/live/favicon-16x16.png`));
|
2016-02-07 21:16:07 +00:00
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
app.all('/*', (req, res, next) => {
|
2017-08-05 20:14:17 +00:00
|
|
|
// CORS headers
|
2017-09-05 14:53:07 +00:00
|
|
|
res.header('Access-Control-Allow-Origin', '*'); // restrict it to the required domain
|
2017-08-05 20:14:17 +00:00
|
|
|
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
|
|
|
|
// Set custom headers for CORS
|
|
|
|
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
|
2017-09-05 14:53:07 +00:00
|
|
|
if (req.method === 'OPTIONS')
|
2017-08-05 20:14:17 +00:00
|
|
|
res.status(200).end();
|
2017-09-05 14:53:07 +00:00
|
|
|
else
|
2017-08-05 20:14:17 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2020-04-17 13:45:53 +00:00
|
|
|
app.use('/.well-known/acme-challenge', express.static('.well-known/acme-challenge'));
|
2020-04-17 13:40:59 +00:00
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
app.get('/', function(req, res) {
|
2017-08-07 21:15:19 +00:00
|
|
|
res.sendFile(path.join(__dirname, indexView));
|
|
|
|
});
|
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
app.get('/admin/list', function(req, res) {
|
2017-08-07 21:15:19 +00:00
|
|
|
res.sendFile(path.join(__dirname, listView));
|
2016-02-07 21:16:07 +00:00
|
|
|
});
|
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
function postShort(req, res) {
|
|
|
|
const longUrl = req.body.url;
|
|
|
|
let shortUrl = '';
|
2020-04-17 13:27:14 +00:00
|
|
|
console.log('Shortening...', longUrl);
|
2016-02-07 21:16:07 +00:00
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
// check if url already exists in database
|
2017-09-25 16:48:55 +00:00
|
|
|
Url.findOne({ 'long_url': longUrl }, (err, doc) => {
|
2020-04-17 13:27:14 +00:00
|
|
|
if(err)
|
|
|
|
console.error('findone error', err);
|
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
if (doc) {
|
2016-02-07 21:16:07 +00:00
|
|
|
shortUrl = config.webhost + base58.encode(doc._id);
|
|
|
|
|
|
|
|
// the document exists, so we return it without creating a new entry
|
2017-09-05 14:53:07 +00:00
|
|
|
res.send({ 'shortUrl': shortUrl });
|
2017-09-25 16:48:55 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-02-07 21:16:07 +00:00
|
|
|
// since it doesn't exist, let's go ahead and create it:
|
2017-09-05 14:53:07 +00:00
|
|
|
const newUrl = Url({
|
2017-09-25 16:48:55 +00:00
|
|
|
'long_url': longUrl
|
2017-09-05 14:53:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// save the new link
|
|
|
|
newUrl.save(function (err) {
|
|
|
|
if (err)
|
|
|
|
logger.error(err);
|
2016-02-07 21:16:07 +00:00
|
|
|
|
|
|
|
shortUrl = config.webhost + base58.encode(newUrl._id);
|
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
res.send({ 'shortUrl': shortUrl });
|
2016-02-07 21:16:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-09-05 14:53:07 +00:00
|
|
|
}
|
2017-08-07 21:15:19 +00:00
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
function getList(req, res) {
|
|
|
|
Url.find({}, (err, doc) => {
|
|
|
|
if (doc)
|
|
|
|
logger.debug(doc);
|
|
|
|
});
|
|
|
|
}
|
2017-08-07 21:15:19 +00:00
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
function getEncodedID(req, res) {
|
|
|
|
const base58Id = req.params.encoded_id;
|
|
|
|
const id = base58.decode(base58Id);
|
2016-02-07 21:16:07 +00:00
|
|
|
|
2017-09-05 14:53:07 +00:00
|
|
|
// check if url already exists in database
|
2017-09-25 16:48:55 +00:00
|
|
|
Url.findOneAndUpdate({ '_id': id }, { '$inc': { 'visits': 1 } }, (err, doc) => {
|
2016-02-07 21:16:07 +00:00
|
|
|
if (doc) {
|
2017-09-05 14:53:07 +00:00
|
|
|
logger.debug(`Redirect: ${doc.long_url}`);
|
2017-08-05 20:58:08 +00:00
|
|
|
res.redirect(doc.long_url);
|
2017-09-25 16:48:55 +00:00
|
|
|
}
|
|
|
|
else
|
2016-02-07 21:16:07 +00:00
|
|
|
res.redirect(config.webhost);
|
|
|
|
});
|
2017-09-05 14:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.post('/api/v1/shorten', postShort);
|
|
|
|
app.get('/api/v1/list', getList);
|
|
|
|
app.get('/:encoded_id', getEncodedID);
|
|
|
|
|
2020-04-17 13:27:14 +00:00
|
|
|
// Old pre letsencrypt
|
2017-09-05 14:53:07 +00:00
|
|
|
const server = app.listen(config.port, () => {
|
|
|
|
logger.info(`Server listening on port ${config.port}`);
|
2016-02-07 21:16:07 +00:00
|
|
|
});
|
2020-04-17 13:40:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2020-04-17 13:27:14 +00:00
|
|
|
|
|
|
|
if (require.main === module)
|
|
|
|
app.listen(config.port, () => {
|
|
|
|
logger.info(`Server listening on port ${config.port}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Instead do export the app:
|
|
|
|
module.exports = app;
|
2020-04-17 13:40:59 +00:00
|
|
|
*/
|