mirror of
https://github.com/pockethost/pockethost.git
synced 2025-03-30 15:08:30 +00:00
enh: add availability check to API
This commit is contained in:
parent
a662a9bc7b
commit
e0871eeffb
@ -1,31 +1,54 @@
|
|||||||
onAfterBootstrap((e) => {
|
onAfterBootstrap((e) => {
|
||||||
$app.dao().db().newQuery(`update instances set status='idle'`).execute()
|
$app.dao().db().newQuery(`update instances set status='idle'`).execute()
|
||||||
$app
|
// $app
|
||||||
.dao()
|
// .dao()
|
||||||
.db()
|
// .db()
|
||||||
.newQuery(`update invocations set endedAt=datetime('now') where endedAt=''`)
|
// .newQuery(`update invocations set endedAt=datetime('now') where endedAt=''`)
|
||||||
.execute()
|
// .execute()
|
||||||
})
|
})
|
||||||
|
|
||||||
routerAdd(
|
routerAdd(
|
||||||
'GET',
|
'GET',
|
||||||
'/api/signup',
|
'/api/signup',
|
||||||
(c) => {
|
(c) => {
|
||||||
const random = require(`${__hooks}/random-words.js`)
|
const isAvailable = (slug) => {
|
||||||
|
try {
|
||||||
|
const record = $app
|
||||||
|
.dao()
|
||||||
|
.findFirstRecordByData('instances', 'subdomain', slug)
|
||||||
|
return false
|
||||||
|
} catch {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const error = (fieldName, slug, description, extra) =>
|
||||||
|
new ApiError(500, description, {
|
||||||
|
[fieldName]: new ValidationError(slug, description),
|
||||||
|
...extra,
|
||||||
|
})
|
||||||
|
|
||||||
const instanceName = (() => {
|
const instanceName = (() => {
|
||||||
let i = 0
|
const name = c.queryParam('name').trim()
|
||||||
while (true) {
|
if (name) {
|
||||||
i++
|
if (isAvailable(name)) {
|
||||||
if (i > 100) {
|
return name
|
||||||
return +new Date()
|
|
||||||
}
|
}
|
||||||
const slug = random.generate(2).join(`-`)
|
throw error(
|
||||||
try {
|
`instanceName`,
|
||||||
const record = $app
|
`exists`,
|
||||||
.dao()
|
`Instance name ${name} is not available.`,
|
||||||
.findFirstRecordByData('instances', 'subdomain', slug)
|
)
|
||||||
} catch {
|
} else {
|
||||||
return slug
|
const random = require(`${__hooks}/random-words.js`)
|
||||||
|
let i = 0
|
||||||
|
while (true) {
|
||||||
|
i++
|
||||||
|
if (i > 100) {
|
||||||
|
return +new Date()
|
||||||
|
}
|
||||||
|
const slug = random.generate(2).join(`-`)
|
||||||
|
if (isAvailable(slug)) return slug
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user