Permalink
Browse files

Extract API v1 into its own app

  • Loading branch information...
1 parent f0a7a4e commit de873e020f04d3d53c4edf13a9f9bbd905061d39 @evdb evdb committed May 10, 2012
Showing with 33 additions and 11 deletions.
  1. +1 −1 instance-app/app.js
  2. +16 −10 lib/apps/{api.js → api/api_v1.js}
  3. +16 −0 lib/apps/api/index.js
View
2 instance-app/app.js
@@ -131,7 +131,7 @@ app.configure(function(){
next();
});
- app.use('/api/v1', require('../lib/apps/api') );
+ app.use('/api', require('../lib/apps/api') );
app.use('/info', require('../lib/apps/info') );
app.use('/token', require('../lib/apps/token') );
View
26 lib/apps/api.js → lib/apps/api/api_v1.js
@@ -52,13 +52,19 @@ current implementation state:
***/
+/*
+ FIXME: the url handling is not working elegantly. It is not possible to get
+ the correct url as we are an app in an app. Hardcoded atm, but needs
+ adrdessing. Might go away when Express 3.0 comes out.
+*/
+
var express = require('express'),
_ = require('underscore'),
regexp_quote = require('regexp-quote'),
- schemas = require('../schemas'),
- utils = require('../utils'),
- base_url = require('../middleware/route').base_url,
- current_absolute_url = require('../middleware/route').current_absolute_url;
+ schemas = require('../../schemas'),
+ utils = require('../../utils'),
+ base_url = require('../../middleware/route').base_url,
+ current_absolute_url = require('../../middleware/route').current_absolute_url;
var app = module.exports = express.createServer();
@@ -92,10 +98,10 @@ app.mounted( function (parent) {
res.json([]);
});
- app.all('*', function(req, res, next) {
- // 404
- res.json({ error: "page not found"}, 404);
- });
+ // app.all('*', function(req, res, next) {
+ // // 404
+ // res.json({ error: "page not found"}, 404);
+ // });
});
@@ -123,7 +129,7 @@ function create_api_endpoint ( options ) {
// if the document_id is not the id redirect to it
if ( document_id != doc.id ) {
- res.redirect(req.app.set('basepath') + '/' + doc.id );
+ res.redirect(base_url(req) + '/api' + req.app.set('basepath') + '/' + doc.id );
} else {
res.local('doc', doc);
return next();
@@ -182,7 +188,7 @@ function create_api_endpoint ( options ) {
var schema = model.schema;
var where = {};
- var api_base_url = base_url(req) + req.app.set('basepath') + '/';
+ var api_base_url = base_url(req) + '/api' + req.app.set('basepath') + '/';
var edit_base_url = base_url(req);
// for each query parameter that we know about create a regex search.
View
16 lib/apps/api/index.js
@@ -0,0 +1,16 @@
+var express = require('express');
+
+var app
+ = module.exports
+ = express.createServer();
+
+app.use( '/v1', require('./api_v1') );
+
+app.get('/', function (req, res) {
+ res.send('home');
+});
+
+app.all('*', function(req, res, next) {
+ // 404
+ res.json({ error: "page not found"}, 404);
+});

0 comments on commit de873e0

Please sign in to comment.