Skip to content

Commit 817cf56

Browse files
committed
First pass at framework for new version
1 parent 05a80a2 commit 817cf56

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+880
-3020
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_test
2+
TODO.md
3+
node_modules

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Copyright © 2016 ReadMe.io
12
Copyright © 2011-2012 Paul Vorbach
23

34
Permission is hereby granted, free of charge, to any person obtaining a copy of

api.js

100755100644
Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,45 @@
1-
module.exports = function(version) {
1+
var fs = require('fs');
2+
var path = require('path');
23

3-
if (version !== 'http' && version !== 'https')
4-
throw new Error(version + ' not supported.');
4+
var utils = require('./utils');
55

6-
var util = require('util');
7-
var http = require(version);
8-
var url = require('url');
9-
var api = {};
10-
11-
// API Server extends EventEmitter
12-
function Server () {
13-
if (!(this instanceof Server))
14-
return new Server();
15-
16-
http.Server.call(this);
17-
18-
// An array that contains the listeners for the different methods
19-
this.methods = [];
20-
21-
// Listen for any request and map the request to it's own event.
22-
// 'regularRequest' is emitted, when there are no listeners for the event.
23-
this.addListener('request', function(req, resp) {
24-
var method;
25-
var path;
26-
27-
req.urlParsed = url.parse(req.url, true, true);
28-
path = req.urlParsed.pathname;
29-
30-
if (this.listeners(path).length > 0)
31-
this.emit(path, req, resp);
32-
else {
33-
// check if method is defined
34-
if (method = this.methods[req.method]) {
35-
// for each listener of method
36-
for (var listener in method) {
37-
var match;
38-
var p = method[listener].pattern;
39-
40-
// match it
41-
if ((match = p.exec(req.url)) != null) {
42-
// add to resp
43-
resp.match = match;
44-
// callback
45-
method[listener].callback(req, resp);
46-
return;
47-
}
48-
}
49-
}
50-
this.emit('regularRequest', req, resp);
51-
}
52-
});
53-
54-
this.extend('GET');
55-
this.extend('POST');
56-
this.extend('PUT');
57-
this.extend('DELETE');
58-
this.extend('HEAD');
6+
exports.actions = {
7+
'test': {swagger: true},
8+
'host': {swagger: true},
9+
'generate': {swagger: true},
5910
};
6011

61-
// Inherit from native http.Server
62-
util.inherits(Server, http.Server);
12+
exports.api = function(args, opts) {
13+
opts = opts || {};
6314

64-
// extend server with a new request method
65-
Server.prototype.extend = function (method, override) {
66-
var fn = method.toLowerCase(); // the methods will be lowercase
15+
var action = args[0];
16+
var config = utils.config(opts.env);
6717

68-
if (this[fn] && !override)
69-
return new Error('The property '+fn
70-
+' already exists. Please use another name or set override to true.');
18+
if(!(action in exports.actions)) {
19+
console.log('Action not found');
20+
return;
21+
}
7122

72-
this.methods[method] = [];
23+
if(exports.actions[action].swagger) {
24+
utils.findSwagger(function(err, swagger) {
25+
if(err) {
26+
console.error(err);
27+
return;
28+
}
7329

74-
this[fn] = function (p, cb) {
75-
this.methods[method].push({ pattern: Server.util.regExp(p), callback: cb });
76-
};
77-
};
30+
if(!swagger) {
31+
console.log("We couldn't find a Swagger file. Let's set one up!");
32+
// TODO: Help them set it up
33+
return; // TODO: This is wrong
34+
}
7835

79-
Server.util = {};
80-
Server.util.regExp = function(path) {
81-
return new RegExp(path.replace(/\//g, '\\/'));
36+
exports.load(action)(config, swagger);
37+
});
38+
} else {
39+
exports.load(action)(config);
40+
}
8241
};
8342

84-
api.Server = Server;
85-
86-
return api;
87-
43+
exports.load = function(action) {
44+
return require(path.join(__dirname, 'lib', `${action}.js`)).run;
8845
};

bin/api.js

Lines changed: 0 additions & 106 deletions
This file was deleted.

config/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"host": {
3+
"url": "http://apis.host"
4+
}
5+
}

config/test.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"env": "test",
3+
"host": {
4+
"url": "http://apis.host"
5+
}
6+
}

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var _ = require('lodash');
2+
3+
var parseArgs = require('minimist')(process.argv.slice(2))
4+
var args = parseArgs._;
5+
var opts = _.clone(parseArgs);
6+
delete opts['_'];
7+
8+
require('./api').api(args, opts);

lib/generate.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.run = function(config, swagger) {
2+
console.log(swagger);
3+
};

lib/host.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var request = require('request');
2+
3+
/*
4+
* This will completely change 100%, and all files
5+
* uploaded to apis.host will be removed! It's just
6+
* a placeholder to kinda have something that works.
7+
*/
8+
9+
exports.run = function(config, swagger) {
10+
console.log('Uploading Swagger file...');
11+
12+
request.post(config.host.url + '/save.php', {
13+
'form': {
14+
'swagger': JSON.stringify(swagger),
15+
}
16+
}, function(err, res, data) {
17+
console.log(data);
18+
});
19+
};

lib/serve.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
exports.run = function(config, swagger) {
2+
if (version !== 'http' && version !== 'https')
3+
throw new Error(version + ' not supported.');
4+
5+
var util = require('util');
6+
var http = require(version);
7+
var url = require('url');
8+
var api = {};
9+
10+
// API Server extends EventEmitter
11+
function Server () {
12+
if (!(this instanceof Server))
13+
return new Server();
14+
15+
http.Server.call(this);
16+
17+
// An array that contains the listeners for the different methods
18+
this.methods = [];
19+
20+
// Listen for any request and map the request to it's own event.
21+
// 'regularRequest' is emitted, when there are no listeners for the event.
22+
this.addListener('request', function(req, resp) {
23+
var method;
24+
var path;
25+
26+
req.urlParsed = url.parse(req.url, true, true);
27+
path = req.urlParsed.pathname;
28+
29+
if (this.listeners(path).length > 0)
30+
this.emit(path, req, resp);
31+
else {
32+
// check if method is defined
33+
if (method = this.methods[req.method]) {
34+
// for each listener of method
35+
for (var listener in method) {
36+
var match;
37+
var p = method[listener].pattern;
38+
39+
// match it
40+
if ((match = p.exec(req.url)) != null) {
41+
// add to resp
42+
resp.match = match;
43+
// callback
44+
method[listener].callback(req, resp);
45+
return;
46+
}
47+
}
48+
}
49+
this.emit('regularRequest', req, resp);
50+
}
51+
});
52+
53+
this.extend('GET');
54+
this.extend('POST');
55+
this.extend('PUT');
56+
this.extend('DELETE');
57+
this.extend('HEAD');
58+
};
59+
60+
// Inherit from native http.Server
61+
util.inherits(Server, http.Server);
62+
63+
// extend server with a new request method
64+
Server.prototype.extend = function (method, override) {
65+
var fn = method.toLowerCase(); // the methods will be lowercase
66+
67+
if (this[fn] && !override)
68+
return new Error('The property '+fn
69+
+' already exists. Please use another name or set override to true.');
70+
71+
this.methods[method] = [];
72+
73+
this[fn] = function (p, cb) {
74+
this.methods[method].push({ pattern: Server.util.regExp(p), callback: cb });
75+
};
76+
};
77+
78+
Server.util = {};
79+
Server.util.regExp = function(path) {
80+
return new RegExp(path.replace(/\//g, '\\/'));
81+
};
82+
83+
api.Server = Server;
84+
85+
return api;
86+
};

0 commit comments

Comments
 (0)