2015-06-15 13 views
10

Contestocaduta dinamica dei gestori in restify

Sto cercando di costruire un server dinamico con restify (2.6.2) in cui i servizi devono essere installati e disinstallati una volta avviato il server. Ho capito che questo può essere visto come qualcosa di strano, ma ha senso nel contesto di un progetto orientato allo DSL. Per raggiungere questo obiettivo ho implementato le seguenti funzioni:

var install = function (path, method, handler) { 
    var id = server[method](path, function (request, response) { // [1] 
     handler (request, response); 
    }); 
    return id; 
} 
var uninstall = function (id) { 
    delete server.routes[id]; // [2] 
} 

La funzione di installazione, installa un gestore nel percorso specificato da un percorso e un nome di metodo [1]. La funzione di disinstallazione, disinstallare il gestore rilasciandolo dalle rotte [2]. Questa funzionalità sono esposti come servizi dal seguente codice:

var db = ... 
var server = restify.createServer() 
    .use (restify.bodyParser ({ mapParams: false })) 
    .use (restify.queryParser()) 
    .use (restify.fullResponse()); 
service.post ('/services', function (request, response) { 
    var path = request.body.path; 
    var method = request.body.method; 
    var handler = createHandler (request.body.dsl) // off-topic 
    var id = install (path, method, handler) 
    db.save (path, method, id); // [3] 
}); 
service.del ('/services', function (request, response) { 
    var path = request.body.path; 
    var method = request.body.method; 
    var id  = db.load (path, method); // [4] 
    uninstall (id); 
}); 

Nel metodo post [3], un gestore è ottenuto dal corpo (è off-topic come questo è intrapresa) e un servizio viene installato memorizzazione del restituita id in un database. Il metodo del [4], recupera l'id dal database e richiama la funzione di disinstallazione.

Problemi

Questo codice è stato unità testato e funziona correttamente, ma un malfunzionamento viene raggiunta quando provo ad eseguire una installazione/disinstallazione sequenza come il seguente. In questo esempio, si prega di supporre che il corpo di tutte le richieste contiene lo stesso path, http verb e contenuto proprio per costruire una corretta handler:

/* 
post: /services : Installed   -> ok 
del: /services : Resource not found -> ok 
post: /services : Resource not found -> Error :(
*/ 

Nella prima installazione, handler viene eseguito quando risorsa acceded via path e verb. La richiesta di disinstallazione è stata soddisfatta correttamente perché un messaggio Resource not found si ottiene quando path viene visitato su verb. Tuttavia, quando lo stesso corpo viene installato in secondo luogo nel server, viene restituito un Resource not found quando si aggiunge path allo verb.

Suppongo che l'errore sia in [2] perché, potrebbe essere, non sto utilizzando la corretta strategia di annullamento della registrazione per restify.

Domanda

Come può essere efficacemente sceso gestori da restify una volta avviato il server?

+0

Per inciso, non è questo incredibilmente rischioso dal punto di vista della sicurezza? Stai consentendo l'inoltro di codice arbitrario che verrà eseguito quando viene colpito un determinato endpoint, che viene anch'esso inoltrato. – HeadCode

risposta

4

Dopo aver esaminato la fonte di restituzione, ho trovato quanto segue, che potresti voler provare invece di semplicemente 'eliminare' (https://github.com/restify/node-restify/blob/master/lib/server.js).

/* 
* Removes a route from the server. 
* You pass in the route 'blob' you got from a mount call. 
* @public 
* @function rm 
* @throws {TypeError} on bad input. 
* @param {String} route the route name. 
* @returns {Boolean}   true if route was removed, false if not. 
*/ 
Server.prototype.rm = function rm(route) { 
    var r = this.router.unmount(route); 

    if (r && this.routes[r]) { 
     delete this.routes[r]; 
    } 

    return (r); 
}; 
Problemi correlati