From ad107b30ae7c2d84bc5cebdcd6a71e9398a79177 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Fri, 15 Aug 2014 19:28:04 +0100 Subject: [PATCH] Enable destroying a tty.js tunnel. --- src/api.coffee | 11 +++++++++++ src/tty.coffee | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/api.coffee b/src/api.coffee index 2bc0ad68..96e1e797 100644 --- a/src/api.coffee +++ b/src/api.coffee @@ -38,4 +38,15 @@ api.post '/v1/spawn-tty', (req, res) -> .catch (err) -> res.send(404, err) +api.post '/v1/despawn-tty', (req, res) -> + appId = req.body.appId + utils.mixpanelTrack('Despawn tty', appId) + if !appId? + res.send(400, 'Missing app id') + tty.stop(appId) + .then -> + res.send(200) + .catch (err) -> + res.send(404, err) + module.exports = api diff --git a/src/tty.coffee b/src/tty.coffee index 319cbf3a..c17f6838 100644 --- a/src/tty.coffee +++ b/src/tty.coffee @@ -23,3 +23,14 @@ exports.start = (appId) -> .listenAsync(port, null) .then -> ngrok.connectAsync(port) + +DISCONNECTED = 'Disconnected' +disconnectedErr = (err) -> return err.message is DISCONNECTED +exports.stop = (appId) -> + if !apps[appId]? + return Promise.resolve() + apps[appId] = apps[appId].then (url) -> + ngrok.disconnectAsync(url) + .then -> + throw new Error(DISCONNECTED) + return apps[appId].catch disconnectedErr, -> # All good!