balena-supervisor/src/api.coffee
2015-10-13 13:43:36 -03:00

89 lines
2.3 KiB
CoffeeScript

Promise = require 'bluebird'
fs = Promise.promisifyAll require 'fs'
utils = require './utils'
application = require './application'
tty = require './lib/tty'
knex = require './db'
express = require 'express'
bodyParser = require 'body-parser'
request = require 'request'
config = require './config'
module.exports = (secret) ->
api = express()
api.use(bodyParser())
api.use (req, res, next) ->
if req.query.apikey is secret
next()
else
res.sendStatus(401)
api.get '/ping', (req, res) ->
res.send('OK')
api.post '/v1/blink', (req, res) ->
utils.mixpanelTrack('Device blink')
utils.blink.pattern.start()
setTimeout(utils.blink.pattern.stop, 15000)
res.sendStatus(200)
api.post '/v1/update', (req, res) ->
utils.mixpanelTrack('Update notification')
application.update()
res.sendStatus(204)
api.post '/v1/spawn-tty', (req, res) ->
appId = req.body.appId
utils.mixpanelTrack('Spawn tty', appId)
if !appId?
return res.status(400).send('Missing app id')
knex('app').select().where({ appId })
.then ([ app ]) ->
if !app?
throw new Error('App not found')
tty.start(app)
.then ({url}) ->
res.status(200).send(url)
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')
api.post '/v1/despawn-tty', (req, res) ->
appId = req.body.appId
utils.mixpanelTrack('Despawn tty', appId)
if !appId?
return res.status(400).send('Missing app id')
knex('app').select().where({ appId })
.then ([ app ]) ->
if !app?
throw new Error('App not found')
tty.stop(app)
.then ->
res.sendStatus(200)
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')
api.post '/v1/purge', (req, res) ->
appId = req.body.appId
utils.mixpanelTrack('Purge /data', appId)
if !appId?
return res.status(400).send('Missing app id')
app = null
knex('app').select().where({ appId })
.then ([ appFromDB ]) ->
if !appFromDB?
throw new Error('App not found')
app = appFromDB
application.lockUpdatesAsync()
.tap ->
application.kill(app)
.then (release) ->
request.post config.gosuperAddress + '/v1/purge', { json: true, body: applicationId: appId }, ->
application.start(app)
.then ->
release()
.pipe(res)
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')
return api