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!