Skip to content

Commit

Permalink
Merge branch 'private'
Browse files Browse the repository at this point in the history
  • Loading branch information
n1try committed Jul 2, 2017
2 parents 9883473 + 185d717 commit 8eb697d
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 193 deletions.
65 changes: 37 additions & 28 deletions app/controllers/shortlink.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
var express = require('express')
, router = express.Router()
, mongoose = require('mongoose')
, Shortlink = mongoose.model('Shortlink')
, utils = require('../../utils')
, log = require('./../../config/middlewares/log')()
, _ = require('underscore');
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose'),
config = require('../../config/config'),
lookup = require('safe-browse-url-lookup')({ apiKey: config.googleApiKey, clientId: 'anchr.io' }),
Shortlink = mongoose.model('Shortlink'),
utils = require('../../utils'),
log = require('./../../config/middlewares/log')(),
_ = require('underscore');

module.exports = function (app, passport) {
app.use('/api/shortlink', router);
app.use('/s', router);
module.exports = function(app, passport) {
app.use('/api/shortlink', router);
app.use('/s', router);
};

router.use(log);

router.get('/:id', function (req, res, next) {
var asJson = req.query.json;
router.get('/:id', function(req, res, next) {
var asJson = req.query.json;

Shortlink.findOne({_id : req.params.id}, {__v : 0}, function (err, obj) {
if (err || !obj) return res.makeError(404, "Not found.");
if (!asJson && obj.url) res.redirect(obj.url);
else res.send(_.omit(obj.toObject(), '__v', 'id'));
});
Shortlink.findOne({ _id: req.params.id }, { __v: 0 }, function(err, obj) {
if (err || !obj) return res.makeError(404, "Not found.");
if (!asJson && obj.url) res.redirect(obj.url);
else res.send(_.omit(obj.toObject(), '__v', 'id'));
});
});

router.post('/', function (req, res, next) {
if (!req.body.url) return res.makeError(400, 'Malformed request: You need to pass a url attribute.')
router.post('/', function(req, res, next) {
if (!req.body.url) return res.makeError(400, 'Malformed request: You need to pass a url attribute.');

var shortlink = new Shortlink({
url: req.body.url,
_id: utils.generateUUID()
});
shortlink.save(function (err, obj) {
if (err) return res.makeError(500, 'Unable to save shortlink to database.', err);
res.status(201).send(_.omit(obj.toObject(), '__v', 'id'));
});
});
lookup.checkSingle(req.body.url)
.then(isMalicious => {
if (isMalicious) return res.makeError(400, 'The link you try to reference is not safe!');
var shortlink = new Shortlink({
url: req.body.url,
_id: utils.generateUUID()
});
shortlink.save(function(err, obj) {
if (err) return res.makeError(500, 'Unable to save shortlink to database.', err);
res.status(201).send(_.omit(obj.toObject(), '__v', 'id'));
});
})
.catch(err => {
return res.makeError(500, 'Something went wrong.' + err);
});
});
30 changes: 15 additions & 15 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var path = require('path')
, rootPath = path.normalize(__dirname + '/..')
, env = process.env.NODE_ENV || 'development'
, _ = require('underscore');
var path = require('path'),
rootPath = path.normalize(__dirname + '/..'),
env = process.env.NODE_ENV || 'development',
_ = require('underscore');

/* Specialized configs (development, test, production) will inherit and possibly override all properties from root */
var config = {
Expand All @@ -16,31 +16,31 @@ var config = {
},
port: 3000,
db: '',
uploadDir : path.normalize('/var/data/anchr'),
maxFileSize : 1000000 * 10,
allowedFileTypes : ['image/'],
secret : 'shhh',
tokenExpire : '30d',
uploadDir: path.normalize('/var/data/anchr'),
maxFileSize: 1000000 * 10,
allowedFileTypes: ['image/'],
secret: 'shhh',
tokenExpire: '30d',
workers: 2,
accessLogPath: path.normalize('/var/log/anchr/access.log'),
errorLogPath: path.normalize('/var/log/anchr/error.log')
errorLogPath: path.normalize('/var/log/anchr/error.log'),
googleApiKey: ''
},

development: {
accessLogPath: path.normalize('./access.log'),
errorLogPath: path.normalize('./error.log')
},

test: {
},
test: {},

production: {
publicUrl: 'https://anchr.io/api',
publicShortlinkUrl: 'https://anchr.io/s',
publicImageUrl: 'https://anchr.io/i',
clientUrl: 'https://anchr.io/#/',
uploadDir : path.normalize('/var/data/anchr.io/'),
secret : '',
uploadDir: path.normalize('/var/data/anchr.io/'),
secret: '',
accessLogPath: path.normalize('/var/log/anchr/access.log'),
errorLogPath: path.normalize('/var/log/anchr/error.log'),
port: 3005
Expand All @@ -50,4 +50,4 @@ var config = {
var resolvedConfig = {};
_.extend(resolvedConfig, config.root, config[env]);

module.exports = resolvedConfig;
module.exports = resolvedConfig;
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,11 @@
"passport-google-oauth": "^0.2.0",
"passport-jwt": "^1.2.1",
"passport-local": "^1.0.0",
"safe-browse-url-lookup": "0.0.2",
"serve-favicon": "^2.3.0",
"underscore": "^1.8.3"
},
"devDependencies": {
"grunt-develop": "^0.4.0",
"grunt-env": "^0.4.4",
"grunt-execute": "^0.2.2",
"load-grunt-tasks": "^3.2.0",
"mongodb": "^2.2.10",
"request": "^2.75.0",
"time-grunt": "^1.2.1",
"winston": "^2.2.0",
"autoprefixer-core": "^5.2.1",
"grunt": "^0.4.5",
"grunt-angular-templates": "^0.5.7",
Expand All @@ -49,6 +42,9 @@
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-uglify": "^0.7.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-develop": "^0.4.0",
"grunt-env": "^0.4.4",
"grunt-execute": "^0.2.2",
"grunt-filerev": "^2.1.2",
"grunt-google-cdn": "^0.4.3",
"grunt-jscs": "^1.8.0",
Expand All @@ -60,7 +56,12 @@
"grunt-wiredep": "^2.0.0",
"jit-grunt": "^0.9.1",
"jshint-stylish": "^1.0.0",
"load-grunt-tasks": "^3.2.0",
"mongodb": "^2.2.10",
"request": "^2.75.0",
"safe-browse": "n1try/node-safe-browse",
"simple-fmt": "^0.1.0",
"time-grunt": "^1.0.0"
"time-grunt": "^1.0.0",
"winston": "^2.2.0"
}
}
1 change: 1 addition & 0 deletions public/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@

<!-- endbower -->
<!-- endbuild -->
<script src="//fezvrasta.github.io/snackbarjs/dist/snackbar.min.js"></script>

<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
Expand Down
88 changes: 47 additions & 41 deletions public/app/scripts/controllers/shortlink.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
'use strict';

angular.module('anchrClientApp')
.controller('ShortlinkCtrl', ['$scope', '$rootScope', 'Shortlink', 'Collection', 'Snackbar', function ($scope, $rootScope, Shortlink, Collection, Snackbar) {

$scope.createShortlink = function (url) {
$scope.data.loading = true;
var sl = new Shortlink({url : url});
sl.$save(function (result) {
if (!result || !result._id) return Snackbar.show("Error while shortening link :-(");
$scope.data.shortlinks.push(result);
$scope.data.loading = false;
$scope.data.linkInput = '';

if ($rootScope.loggedIn()) saveToCollection(result);
});
};

$scope.clear = function () {
$scope.data = {
loading: false,
linkInput: '',
shortlinks : []
};
}

// RUN
init();

function init() {
$scope.clear();
$rootScope.init();
}

function saveToCollection (link) {
new Collection.shortlinks({
url : link.href,
description: 'Shortlink to ' + link.url
}).$save(function (result){}, function (err) {
Snackbar.show('Failed to save shortlink to your collection: ' + err.data.error);
});
};

}]);
.controller('ShortlinkCtrl', ['$scope', '$rootScope', 'Shortlink', 'Collection', 'Snackbar', function($scope, $rootScope, Shortlink, Collection, Snackbar) {

$scope.createShortlink = function(url) {
$scope.data.loading = true;
var sl = new Shortlink({ url: url });
sl.$save(function(result) {
if (!result || !result._id) {
$scope.data.loading = false;
return Snackbar.show("Error while shortening link :-(");
}
$scope.data.shortlinks.push(result);
$scope.data.loading = false;
$scope.data.linkInput = '';

if ($rootScope.loggedIn()) saveToCollection(result);
}, function(err) {
$scope.data.loading = false;
return Snackbar.show(err.data.error);
});
};

$scope.clear = function() {
$scope.data = {
loading: false,
linkInput: '',
shortlinks: []
};
}

// RUN
init();

function init() {
$scope.clear();
$rootScope.init();
}

function saveToCollection(link) {
new Collection.shortlinks({
url: link.href,
description: 'Shortlink to ' + link.url
}).$save(function(result) {}, function(err) {
Snackbar.show('Failed to save shortlink to your collection: ' + err.data.error);
});
};

}]);
58 changes: 27 additions & 31 deletions public/bower.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
{
"name": "anchr-client",
"version": "0.0.0",
"dependencies": {
"angular": "1.4.7",
"bootstrap": "^3.2.0",
"angular-cookies": "^1.4.0",
"angular-resource": "^1.4.0",
"angular-route": "^1.4.0",
"bootstrap-material-design": "~0.4.3",
"ngclipboard": "~1.0.0",
"snackbarjs": "~1.0.0",
"ng-file-upload": "~10.0.2",
"angular-jwt": "~0.0.9",
"cryptojslib": "^3.1.2"
},
"devDependencies": {
"angular-mocks": "^1.4.0"
},
"appPath": "app",
"moduleName": "anchrClientApp",
"overrides": {
"bootstrap": {
"main": [
"less/bootstrap.less",
"dist/css/bootstrap.css",
"dist/js/bootstrap.js"
]
}
},
"resolutions": {
"angular": "1.4.7"
"name": "anchr-client",
"version": "0.0.0",
"dependencies": {
"angular": "1.4.7",
"bootstrap": "^3.2.0",
"angular-cookies": "^1.4.0",
"angular-resource": "^1.4.0",
"angular-route": "^1.4.0",
"bootstrap-material-design": "~0.4.3",
"ngclipboard": "~1.0.0",
"snackbarjs": "~1.0.0",
"ng-file-upload": "~10.0.2",
"angular-jwt": "~0.0.9",
"cryptojslib": "^3.1.2"
},
"devDependencies": {
"angular-mocks": "^1.4.0"
},
"appPath": "app",
"moduleName": "anchrClientApp",
"overrides": {
"bootstrap": {
"main": [
"less/bootstrap.less",
"dist/css/bootstrap.css",
"dist/js/bootstrap.js"
]
}
}
Loading

0 comments on commit 8eb697d

Please sign in to comment.