Permalink
Browse files

[refactor] Code cleanup and fixed stuff

  • Loading branch information...
1 parent f62d3a2 commit 14c06b3158552a883586751e10513e6d3653c6ab @alejandro alejandro committed Jun 9, 2012
Showing with 135 additions and 108 deletions.
  1. +135 −108 lib/domains.js
View
@@ -2,125 +2,152 @@
* Nodester :: Open Source Node.JS PaaS
* Domains wrapper
* @license GNU Affero
- * @latestUpdate: 30-03-2012
+ * @latestUpdate: 08-06-2012
*/
+/*jshint node:true, noempty:true, laxcomma:true, laxbreak:false */
-var request = require('request'),
- config = require('../config'),
- cradle = require('cradle'),
- lib = require('./lib');
+var request = require('request')
+ , config = require('../config')
+ , cradle = require('cradle')
+ , lib = require('./lib')
+ ;
-module.exports = {
- post: function(req, res, next) {
- var appname = req.appname,
- domain = req.param("domain"),
- user = req.user,
- app = req.app;
+// Post action for domains
+// this simple add a new domain to the list
+function post (req, res, next) {
+ var appname = req.appname
+ , domain = req.param("domain")
+ , user = req.user
+ , app = req.app
+ ;
- var gooddomain = lib.checkDomain(domain);
- if (gooddomain === true) {
- var aliasdomains = lib.get_couchdb_database('aliasdomains');
- aliasdomains.get(domain, function(err, doc) {
- if (err) {
- if (err.error == 'not_found') {
- aliasdomains.save(domain, {
- appname: app._id,
- host: '127.0.0.1',
- port: app.port,
- username: user._id
- }, function(err, resp) {
- if (err) {
- res.error(400, JSON.stringify({
- status: "failure",
- message: err.error + ' - ' + err.reason
- }) + '\n');
- } else {
- res.send({
- "status": "success",
- "message": "Domain added."
- });
- }
- });
- } else {
- res.error(400, JSON.stringify({
- status: "failure",
- message: err.error + ' - ' + err.reason
- }) + '\n');
- }
+ // validate the TLD
+ var gooddomain = lib.checkDomain(domain);
+ if (gooddomain === true) {
+ var aliasdomains = lib.get_couchdb_database('aliasdomains');
+ aliasdomains.get(domain, function (err, doc) {
+ if (err) {
+ if (err.error == 'not_found') {
+ aliasdomains.save(domain, {
+ appname: app._id,
+ host: '127.0.0.1',
+ port: app.port,
+ username: user._id
+ }, function (err, resp) {
+ if (err) {
+ res.json({
+ status: "failure",
+ message: err.error + ' - ' + err.reason
+ }, 400);
+ } else {
+ res.json({
+ status: "success",
+ message: "Domain added."
+ }, 200);
+ }
+ });
} else {
- res.json(400, {"status": "failure - domain already exists"});
+ res.json({
+ status: "failure",
+ message: err.error + ' - ' + err.reason
+ }, 400);
}
- });
- } else {
- res.json(400, {"status": 'failure - ' + gooddomain });
- }
- },
- delete: function(req, res, next) {
- var appname = req.param("appname").toLowerCase();
- var domain = req.param("domain").toLowerCase();
-
- var user = req.user,
- app = req.app;
+ } else {
+ res.json({
+ status: "failure - domain already exists"
+ }, 400);
+ }
+ });
+ } else {
+ res.json({
+ status: 'failure - ' + gooddomain
+ }, 400);
+ }
+}
+
+// deletes a domain
+function erase (req, res, next) {
+ var appname = req.param("appname").toLowerCase()
+ , domain = req.param("domain").toLowerCase()
+ , user = req.user
+ , app = req.app
+ ;
- var gooddomain = lib.checkDomain(domain);
- if (gooddomain === true) {
- var aliasdomains = lib.get_couchdb_database('aliasdomains');
- aliasdomains.get(domain, function(err, doc) {
- if (err) {
- if (err.error == 'not_found') {
- res.json(400, {"status": "failure - domain not found."});
- } else {
- res.json(400, {
- status: "failure",
- message: err.error + ' - ' + err.reason
- });
- }
+ var gooddomain = lib.checkDomain(domain);
+ if (gooddomain === true) {
+ var aliasdomains = lib.get_couchdb_database('aliasdomains');
+ aliasdomains.get(domain, function (err, doc) {
+ if (err) {
+ if (err.error == 'not_found') {
+ res.json({
+ status: "failure - domain not found."
+ }, 400);
} else {
- if (doc.appname == appname) {
- aliasdomains.remove(domain, function(err, resp) {
- if (err) {
- res.json(400, {
- status: "failure",
- message: err.error + ' - ' + err.reason
- });
- } else {
- res.send({
- "status": "success",
- "message": "Domain deleted."
- });
- }
- });
- } else {
- res.json(400, {
- "status": "failure - domain is not for this app."
- });
- }
+ res.json({
+ status: "failure",
+ message: err.error + ' - ' + err.reason
+ }, 400);
}
- });
- } else {
- res.json(400, {
- "status": "failure - " + gooddomain
- });
- }
- },
- get: function(req, res, next) {
- var user = req.user;
- var db = lib.get_couchdb_database('aliasdomains');
- // db.view('aliasdomains/all', {key: req.user._id}, function (err, resp) {
- db.view('aliasdomains/all', {}, function(err, resp) {
- var domains = [];
- resp.forEach(function(row) {
- if (row.username == user._id) {
- domains.push({
- domain: row._id,
- appname: row.appname,
- host: row.host,
- port: row.port
+ } else {
+ if (doc.appname == appname) {
+ aliasdomains.remove(domain, function (err, resp) {
+ if (err) {
+ res.json({
+ status: "failure",
+ message: err.error + ' - ' + err.reason
+ }, 400);
+ } else {
+ res.json({
+ status: "success",
+ message: "Domain deleted."
+ }, 400);
+ }
});
+ } else {
+ res.json({
+ status: "failure - domain is not for this app."
+ }, 400);
}
- });
- res.send(domains);
+ }
});
+ } else {
+ res.json({
+ status: "failure - " + gooddomain
+ }, 400);
}
-};
+}
+
+
+// get all the domains
+function get (req, res, next) {
+ var user = req.user
+ , db = lib.get_couchdb_database('aliasdomains')
+ ;
+
+
+ db.view('aliasdomains/all', {}, function (err, resp) {
+ if (err) {
+ res.json({status: 'failed to get'}, 400);
+ return;
+ }
+ var domains = [];
+ resp.forEach(function (row) {
+ if (row.username == user._id) {
+ domains.push({
+ domain: row._id,
+ appname: row.appname,
+ host: row.host,
+ port: row.port
+ });
+ }
+ });
+ res.json(domains);
+ });
+}
+
+module.exports = {
+ post: post,
+ delete: erase,
+ get: get
+};

0 comments on commit 14c06b3

Please sign in to comment.