sc-manager/api/core/routes.mjs

50 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

import { HttpError } from 'flaska'
import { getApp, stopSpam } from '../util.mjs'
/*
* POST: /update/:name
*
*/
export async function updateApp(ctx) {
let app = getApp(ctx, ctx.params.name, 'POST.update')
if (!app) {
ctx.status = 404
return ctx.body = {
status: 404,
message: `Application ${ctx.params.name} was not found`
}
}
let t = stopSpam(ctx, 'update', ctx.params.name)
if (t) {
let d2 = new Date()
let timeLeft =
Math.round((new Date(t).setMinutes(t.getMinutes() + 1) - d2) / 1000)
ctx.headers['Retry-After'] = timeLeft
ctx.status = 503
return ctx.body = {
status: 503,
message: `Update request too fast for ${ctx.params.name}. Try again in ${timeLeft} seconds.`
}
}
return app.update()
.then(function(res) {
if (res) {
ctx.body = res
} else {
ctx.body = {
message: 'No update was found',
}
}
}, function(err) {
ctx.log.error(err)
ctx.status = 500
return ctx.body = {
status: 500,
message: `Application ${ctx.params.name} failed to update: ${err.message}`,
}
})
}