Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

backend works

  • Loading branch information...
commit 37555fc53188cca956687cbc51df558af1defdb1 1 parent 4bf73dd
@rgarcia authored
View
43 app.js
@@ -0,0 +1,43 @@
+define([
+ 'express',
+ 'mongoose',
+ 'controllers/loader',
+ 'module',
+ 'path',
+ './config'
+], function (express, mongoose, controllerLoader, module, path, config) {
+
+ var app = null;
+
+ return {
+ initialize: function() {
+ if ( app ) return;
+
+ console.log('INITIALIZING APP');
+ app = express.createServer();
+ app.listen(config.app_port);
+
+ app.configure(function() {
+ var db = mongoose.connect(
+ 'mongodb://' + config.db_user_prod + ':' + config.db_pass_prod +
+ '@' + config.db_host_prod + ':' + config.db_port_prod + '/' + config.db_name_prod,
+ function(err) {
+ if (err)
+ throw err;
+ });
+ app.use(express.logger({ format: ':method :url :status' }));
+ console.log('initializing static: ' + path.dirname(filename) + '/static');
+ var filename = module.uri;
+ app.use(express.static(path.dirname(filename) + '/static'));
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ controllerLoader.bootControllers(app);
+ console.log('instacss version now running on port ' + config.app_port);
+ });
+ },
+
+ getApp: function() {
+ return app;
+ },
+ };
+});
View
59 controllers/cssproperty.js
@@ -0,0 +1,59 @@
+define([
+ 'underscore',
+ '../models/cssproperty'
+], function(_, CSSPropertyModel) {
+
+ return {
+
+ mapping: {
+ 'read' : {
+ 'url' : '/cssproperty/:name',
+ 'method' : 'get',
+ 'description' : 'returns docuemnt for css property name',
+ 'auth' : false
+ },
+ 'find_cssproperty' : {
+ 'url':'/find_cssproperty',
+ 'method':'get',
+ 'description':'find css property. expects a GET parameter "query"',
+ 'auth':false
+ }
+
+ },
+
+ read: function(req, res) {
+ CSSPropertyModel.findOne({ name: req.params.name }, function(error, cssprop) {
+ if (!error && cssprop) {
+ res.header('Content-Type', 'application/json');
+ res.send(JSON.stringify(cssprop), 200);
+ } else {
+ res.send('could not find css prop with name: ' + req.params.name, 404);
+ }
+ });
+ },
+
+ find_cssproperty: function(req, res) {
+ res.header('Content-Type', 'application/json');
+
+ var query = CSSPropertyModel.find({});
+
+ // TODO: frontend only queries w/ "name"...should offer advanced search
+ _.each(['name', 'description', 'possibleValues'], function(param) {
+ if ( paramVal = req.param(param, null) ) {
+ console.log('querying for ' + param + ': ' + paramVal)
+ query.$regex(param, new RegExp(paramVal,'i'));
+ }
+ });
+
+ query.limit(1);
+
+ query.exec(function(err, cssprops) {
+ if ( err || cssprops.length == 0 ) {
+ res.send(JSON.stringify([]));
+ } else {
+ res.send(JSON.stringify(cssprops));
+ }
+ });
+ }
+ };
+});
View
56 controllers/loader.js
@@ -0,0 +1,56 @@
+define([
+ './cssproperty',
+], function(CSSPropertyActions) {
+
+ var mappingString = "";
+
+ function add_to_mapping_string(method, url, description, auth) {
+ mappingString += "<div style='width: 400px; height: 20px; padding: 5px; background-color: #ccc; color: #000; font-family: Arial, Verdana, sans-serif'><b>" + method + " " + url + "</b></div><div style='width: 400px; padding: 5px; background-color: #eee; color: #000; font-family: Arial, Verdana, sans-serif'>" + description + "<br /><b>Auth:</b> " + auth + "</div><br />";
+ }
+
+ function bootController(app, actions) {
+ var mapping = actions["mapping"];
+
+ Object.keys(actions).map(function(action) {
+ var fn = actions[action];
+
+ if (typeof(fn) === "function") {
+ var a = mapping[action];
+ if (a) {
+ add_to_mapping_string(a.method, a.url, a.description, a.auth);
+ switch(a.method) {
+ case 'get':
+ app.get(a.url, fn);
+ console.log("initialized get " + a.url);
+ break;
+ case 'post':
+ app.post(a.url, fn);
+ console.log("initialized post " + a.url);
+ break;
+ case 'put':
+ app.put(a.url, fn);
+ console.log("initialized put " + a.url);
+ break;
+ case 'delete':
+ app.del(a.url, fn);
+ console.log("initialized delete " + a.url);
+ break;
+ }
+ } else {
+ console.log("WARNING: no mapping for " + action + " defined");
+ }
+ }
+ });
+ }
+
+ return {
+ bootControllers : function(app) {
+ console.log("booting cssproperty controller");
+ bootController(app, CSSPropertyActions);
+
+ app.get("/show_available_interfaces", function(req, res){
+ res.send(mappingString);
+ });
+ }
+ }
+});
View
7 package.json
@@ -1,7 +1,10 @@
{
- "name": "node-example",
+ "name": "instacss-backend",
"version": "0.0.1",
"dependencies": {
- "express": "2.2.0"
+ "requirejs": ">=1.0.0",
+ "express": "2.2.0",
+ "mongoose": ">=2.3.9",
+ "underscore": ">=1.2.1"
}
}
View
18 web.js
@@ -1,12 +1,14 @@
-var express = require('express');
+var requirejs = require('requirejs');
-var app = express.createServer(express.logger());
-
-app.get('/', function(request, response) {
- response.send('Hello World!');
+requirejs.config({
+ // Pass the top-level main.js/index.js require
+ // function to requirejs so that node modules
+ // are loaded relative to the top-level JS file.
+ nodeRequire: require
});
-var port = process.env.PORT || 3000;
-app.listen(port, function() {
- console.log("Listening on " + port);
+requirejs([
+ 'app'
+], function(app) {
+ app.initialize();
});
Please sign in to comment.
Something went wrong with that request. Please try again.