2013-12-23 04:22:54 +00:00
|
|
|
Promise = require 'bluebird'
|
|
|
|
fs = Promise.promisifyAll require 'fs'
|
|
|
|
utils = require './utils'
|
2014-12-22 20:12:38 +00:00
|
|
|
tty = require './lib/tty'
|
|
|
|
knex = require './db'
|
2015-03-03 13:55:14 +00:00
|
|
|
express = require 'express'
|
|
|
|
bodyParser = require 'body-parser'
|
2015-07-24 14:26:33 -03:00
|
|
|
request = require 'request'
|
|
|
|
config = require './config'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2015-09-02 19:19:24 +00:00
|
|
|
module.exports = (secret, application) ->
|
2015-01-28 15:13:26 +00:00
|
|
|
api = express()
|
2015-03-03 13:55:14 +00:00
|
|
|
api.use(bodyParser())
|
2015-01-28 15:13:26 +00:00
|
|
|
api.use (req, res, next) ->
|
2015-10-02 17:18:23 +00:00
|
|
|
if req.query.apikey is secret
|
2015-01-28 15:13:26 +00:00
|
|
|
next()
|
|
|
|
else
|
2015-03-09 12:06:24 +00:00
|
|
|
res.sendStatus(401)
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2015-07-24 20:59:58 +00:00
|
|
|
api.get '/ping', (req, res) ->
|
|
|
|
res.send('OK')
|
|
|
|
|
2015-01-28 15:13:26 +00:00
|
|
|
api.post '/v1/blink', (req, res) ->
|
|
|
|
utils.mixpanelTrack('Device blink')
|
|
|
|
utils.blink.pattern.start()
|
|
|
|
setTimeout(utils.blink.pattern.stop, 15000)
|
2015-03-09 12:06:24 +00:00
|
|
|
res.sendStatus(200)
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2015-01-28 15:13:26 +00:00
|
|
|
api.post '/v1/update', (req, res) ->
|
|
|
|
utils.mixpanelTrack('Update notification')
|
2015-08-12 20:24:18 +00:00
|
|
|
application.update(req.body.force)
|
2015-03-09 12:06:24 +00:00
|
|
|
res.sendStatus(204)
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2015-01-28 15:13:26 +00:00
|
|
|
api.post '/v1/spawn-tty', (req, res) ->
|
|
|
|
appId = req.body.appId
|
|
|
|
utils.mixpanelTrack('Spawn tty', appId)
|
|
|
|
if !appId?
|
2015-07-24 18:03:37 -03:00
|
|
|
return res.status(400).send('Missing app id')
|
2015-08-03 16:06:03 -03:00
|
|
|
knex('app').select().where({ appId })
|
2015-01-28 15:13:26 +00:00
|
|
|
.then ([ app ]) ->
|
|
|
|
if !app?
|
|
|
|
throw new Error('App not found')
|
|
|
|
tty.start(app)
|
2015-03-16 13:29:45 +00:00
|
|
|
.then ({url}) ->
|
2015-03-09 12:06:24 +00:00
|
|
|
res.status(200).send(url)
|
2015-01-28 15:13:26 +00:00
|
|
|
.catch (err) ->
|
2015-03-09 12:06:24 +00:00
|
|
|
res.status(503).send(err?.message or err or 'Unknown error')
|
2014-08-04 12:10:36 +01:00
|
|
|
|
2015-01-28 15:13:26 +00:00
|
|
|
api.post '/v1/despawn-tty', (req, res) ->
|
|
|
|
appId = req.body.appId
|
|
|
|
utils.mixpanelTrack('Despawn tty', appId)
|
|
|
|
if !appId?
|
2015-07-24 18:03:37 -03:00
|
|
|
return res.status(400).send('Missing app id')
|
2015-08-03 16:06:03 -03:00
|
|
|
knex('app').select().where({ appId })
|
2015-03-16 13:20:41 +00:00
|
|
|
.then ([ app ]) ->
|
|
|
|
if !app?
|
|
|
|
throw new Error('App not found')
|
|
|
|
tty.stop(app)
|
2015-01-28 15:13:26 +00:00
|
|
|
.then ->
|
2015-03-09 12:06:24 +00:00
|
|
|
res.sendStatus(200)
|
2015-01-28 15:13:26 +00:00
|
|
|
.catch (err) ->
|
2015-03-09 12:06:24 +00:00
|
|
|
res.status(503).send(err?.message or err or 'Unknown error')
|
2014-08-15 19:28:04 +01:00
|
|
|
|
2015-08-21 00:21:41 +00:00
|
|
|
api.post '/v1/reboot', (req, res) ->
|
|
|
|
utils.mixpanelTrack('Reboot')
|
2015-08-25 19:40:54 +00:00
|
|
|
request.post(config.gosuperAddress + '/v1/reboot')
|
|
|
|
.pipe(res)
|
2015-08-21 00:21:41 +00:00
|
|
|
|
|
|
|
api.post '/v1/shutdown', (req, res) ->
|
|
|
|
utils.mixpanelTrack('Shutdown')
|
2015-08-25 19:40:54 +00:00
|
|
|
request.post(config.gosuperAddress + '/v1/shutdown')
|
|
|
|
.pipe(res)
|
2015-08-21 00:21:41 +00:00
|
|
|
|
2015-07-24 14:26:33 -03:00
|
|
|
api.post '/v1/purge', (req, res) ->
|
2015-07-24 20:59:58 +00:00
|
|
|
appId = req.body.appId
|
|
|
|
utils.mixpanelTrack('Purge /data', appId)
|
|
|
|
if !appId?
|
|
|
|
return res.status(400).send('Missing app id')
|
2015-08-24 18:42:21 +00:00
|
|
|
Promise.using application.lockUpdates(appId, true), ->
|
2015-08-21 19:48:07 +00:00
|
|
|
knex('app').select().where({ appId })
|
|
|
|
.then ([ app ]) ->
|
|
|
|
if !app?
|
|
|
|
throw new Error('App not found')
|
2015-08-17 22:08:52 +00:00
|
|
|
application.kill(app)
|
2015-08-24 18:42:21 +00:00
|
|
|
.then ->
|
|
|
|
new Promise (resolve, reject) ->
|
2015-08-26 22:42:10 +00:00
|
|
|
request.post(config.gosuperAddress + '/v1/purge', { json: true, body: applicationId: appId })
|
2015-08-24 18:42:21 +00:00
|
|
|
.on 'error', reject
|
|
|
|
.on 'response', -> resolve()
|
|
|
|
.pipe(res)
|
|
|
|
.finally ->
|
|
|
|
application.start(app)
|
2015-07-31 17:17:57 +00:00
|
|
|
.catch (err) ->
|
|
|
|
res.status(503).send(err?.message or err or 'Unknown error')
|
2015-07-24 14:26:33 -03:00
|
|
|
|
2015-08-26 19:10:22 +05:30
|
|
|
api.post '/tcp-ping', (req, res) ->
|
2015-09-02 17:42:11 +05:30
|
|
|
utils.disableCheck(false)
|
|
|
|
res.sendStatus(204)
|
2015-08-26 19:10:22 +05:30
|
|
|
|
|
|
|
api.delete '/tcp-ping', (req, res) ->
|
2015-09-02 17:42:11 +05:30
|
|
|
utils.disableCheck(true)
|
|
|
|
res.sendStatus(204)
|
2015-08-26 19:10:22 +05:30
|
|
|
|
2015-08-20 22:42:13 +00:00
|
|
|
api.post '/v1/restart', (req, res) ->
|
|
|
|
appId = req.body.appId
|
2015-09-08 15:38:38 +00:00
|
|
|
force = req.body.force
|
2015-08-20 22:42:13 +00:00
|
|
|
utils.mixpanelTrack('Restart container', appId)
|
|
|
|
if !appId?
|
|
|
|
return res.status(400).send('Missing app id')
|
2015-09-08 15:38:38 +00:00
|
|
|
Promise.using application.lockUpdates(appId, force), ->
|
2015-08-20 22:42:13 +00:00
|
|
|
knex('app').select().where({ appId })
|
|
|
|
.then ([ app ]) ->
|
|
|
|
if !app?
|
|
|
|
throw new Error('App not found')
|
|
|
|
application.kill(app)
|
|
|
|
.then ->
|
|
|
|
application.start(app)
|
|
|
|
.then ->
|
|
|
|
res.status(200).send('OK')
|
|
|
|
.catch (err) ->
|
|
|
|
res.status(503).send(err?.message or err or 'Unknown error')
|
|
|
|
|
2015-01-28 15:13:26 +00:00
|
|
|
return api
|