mirror of
				https://github.com/reality/dbot.git
				synced 2025-11-04 09:37:36 +01:00 
			
		
		
		
	Initial API module [#361]
This commit is contained in:
		
							parent
							
								
									3923a38a17
								
							
						
					
					
						commit
						a628d12361
					
				
							
								
								
									
										50
									
								
								modules/api/api.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								modules/api/api.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Name: API
 | 
			
		||||
 * Description: Expose DBot API functionality with a REST API
 | 
			
		||||
 */
 | 
			
		||||
var _ = require('underscore')._;
 | 
			
		||||
 | 
			
		||||
var api = function(dbot) {
 | 
			
		||||
    this.onLoad = function() {
 | 
			
		||||
        dbot.modules.web.app.get('/api/:module/:method', function(req, res) {
 | 
			
		||||
            var module = req.params.module,
 | 
			
		||||
                method = req.params.method,
 | 
			
		||||
                body = { 'err': null, 'data': null };
 | 
			
		||||
 | 
			
		||||
                if(!_.has(dbot.api, module)) {
 | 
			
		||||
                    body.err = 'No such API module';
 | 
			
		||||
                } else if(!_.has(dbot.api[module], method)) {
 | 
			
		||||
                    body.err = 'No such API function in ' + module;
 | 
			
		||||
                } else if(dbot.api[module][method].external !== true) {
 | 
			
		||||
                    body.err = 'API function ' + module + '.' + method + 
 | 
			
		||||
                        ' not enabled for external access';
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                if(!body.err) {
 | 
			
		||||
                    var func = dbot.api[module][method];
 | 
			
		||||
                    var paramNames = func.extMap;
 | 
			
		||||
                    var args = [];
 | 
			
		||||
                    
 | 
			
		||||
                    // TODO: Use request params to map to extMap args
 | 
			
		||||
 | 
			
		||||
                    var callbackIndex = paramNames.indexOf('callback');
 | 
			
		||||
                    if(callbackIndex != -1) {
 | 
			
		||||
                        args[callbackIndex] = function() {
 | 
			
		||||
                            body.data = Array.prototype.slice.call(arguments, 0);
 | 
			
		||||
                            res.json(body);
 | 
			
		||||
                        };
 | 
			
		||||
                        func.apply(null, args);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        body.data = func.call(null, args);
 | 
			
		||||
                        res.json(body);
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    res.json(body);
 | 
			
		||||
                }
 | 
			
		||||
        }.bind(this));
 | 
			
		||||
    }.bind(this);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.fetch = function(dbot) {
 | 
			
		||||
    return new api(dbot);
 | 
			
		||||
};
 | 
			
		||||
@ -67,6 +67,8 @@ var imgur = function(dbot) {
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    this.api['getRandomImage'].external = true;
 | 
			
		||||
    this.api['getRandomImage'].extMap = [ 'callback' ];
 | 
			
		||||
 | 
			
		||||
    this.commands = {
 | 
			
		||||
        '~ri': function(event) {
 | 
			
		||||
 | 
			
		||||
@ -3,17 +3,17 @@ var express = require('express'),
 | 
			
		||||
    fs = require('fs');
 | 
			
		||||
 | 
			
		||||
var webInterface = function(dbot) {
 | 
			
		||||
    var pub = 'public';
 | 
			
		||||
    var app = express();
 | 
			
		||||
    this.pub = 'public';
 | 
			
		||||
    this.app = express();
 | 
			
		||||
 | 
			
		||||
    app.use(express.static(pub));
 | 
			
		||||
    app.set('view engine', 'jade');
 | 
			
		||||
    this.app.use(express.static(this.pub));
 | 
			
		||||
    this.app.set('view engine', 'jade');
 | 
			
		||||
 | 
			
		||||
    app.get('/', function(req, res) {
 | 
			
		||||
    this.app.get('/', function(req, res) {
 | 
			
		||||
        res.render('index', { 'name': dbot.config.name });
 | 
			
		||||
    });
 | 
			
		||||
   
 | 
			
		||||
    var server = app.listen(dbot.config.web.webPort);
 | 
			
		||||
    var server = this.app.listen(dbot.config.web.webPort);
 | 
			
		||||
 | 
			
		||||
    this.reloadPages = function() {
 | 
			
		||||
        var pages = dbot.pages;
 | 
			
		||||
@ -21,7 +21,7 @@ var webInterface = function(dbot) {
 | 
			
		||||
            if(_.has(pages, p)) {
 | 
			
		||||
                var func = pages[p];
 | 
			
		||||
                var mod = func.module;
 | 
			
		||||
                app.get(p, (function(req, resp) {
 | 
			
		||||
                this.app.get(p, (function(req, resp) {
 | 
			
		||||
                    // Crazy shim to seperate module views.
 | 
			
		||||
                    var shim = Object.create(resp);
 | 
			
		||||
                    shim.render = (function(view, one, two) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user