balena-supervisor/src/api.coffee

50 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2013-12-23 04:22:54 +00:00
Promise = require 'bluebird'
fs = Promise.promisifyAll require 'fs'
utils = require './utils'
express = require 'express'
application = require './application'
2014-12-22 20:12:38 +00:00
tty = require './lib/tty'
knex = require './db'
api = express()
2014-08-04 11:10:36 +00:00
api.use(express.bodyParser())
2014-03-19 19:54:39 +00:00
api.post '/v1/blink', (req, res) ->
2014-06-18 16:54:36 +00:00
utils.mixpanelTrack('Device blink')
utils.blink.pattern.start()
setTimeout(utils.blink.pattern.stop, 15000)
res.send(200)
2014-03-19 19:54:39 +00:00
api.post '/v1/update', (req, res) ->
2014-06-18 16:54:36 +00:00
utils.mixpanelTrack('Update notification')
application.update()
res.send(204)
2014-08-04 11:10:36 +00:00
api.post '/v1/spawn-tty', (req, res) ->
appId = req.body.appId
utils.mixpanelTrack('Spawn tty', appId)
if !appId?
res.send(400, 'Missing app id')
2014-12-22 20:12:38 +00:00
knex('app').select().where({appId})
.then ([ app ]) ->
if !app?
throw new Error('App not found')
tty.start(app)
2014-08-04 11:10:36 +00:00
.then (url) ->
res.send(200, url)
.catch (err) ->
2014-09-18 10:24:22 +00:00
res.send(503, err?.message or err or 'Unknown error')
2014-08-04 11:10:36 +00:00
2014-08-15 18:28:04 +00:00
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(503, err?.message or err or 'Unknown error')
2014-08-15 18:28:04 +00:00
module.exports = api