Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

4.0 KB
Raw Blame
import { Hono } from 'hono'
import { z } from 'zod'
import { newId, newSecret, type Vars } from '../auth'
import { ACTIVE_STATUSES, now } from '../db'
import type { Ctx } from '../ctx'
import { fail } from '../errors'
import { nameProblem } from '../platform'
import { dropPreview } from '../preview'
import { environmentViews } from '../console-views'
import { envSummary } from '../views'
import { body, envOf } from './util'

export function envRoutes(ctx: Ctx) {
  const r = new Hono<{ Variables: Vars }>()
  const { store } = ctx

  r.get('/envs', async (c) => {
    const app = c.get('app')
    if (c.req.query('view') === 'dashboard') return c.json(await environmentViews(ctx, app))
    return c.json(await Promise.all((await store.envs(app.id)).map(async (e) => await envSummary(ctx, app, e))))
  })

  r.post('/envs', async (c) => {
    const app = c.get('app')
    const b = await body(
      c,
      z.object({ name: z.string(), from: z.string().optional(), with_data: z.boolean().optional() }),
    )
    const problem = nameProblem(b.name)
    if (problem) throw fail('invalid_request', `env name "${b.name}" ${problem}`)
    if (await store.envByName(app.id, b.name))
      throw fail('conflict', `env "${b.name}" already exists`, `use it with \`--env ${b.name}\`, or pick another name`)
    const from = b.from ?? 'main'
    if (!(await store.envByName(app.id, from))) throw fail('not_found_env', `no environment "${from}" to branch from`)
    const id = newId('env')
    await store.run(
      'INSERT INTO envs (id, app_id, name, created_at, from_env, with_data, forward_secret, control_secret) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
      id,
      app.id,
      b.name,
      now(),
      from,
      b.with_data ? 1 : 0,
      newSecret(),
      newSecret(),
    )
    // The database branch is created now if the app already has a Neon project; otherwise on the
    // env's first apply/deploy that declares [database].
    if (app.neon_project_id) {
      try {
        await ctx.resources.ensureDatabase(app.id, id)
      } catch (err) {
        await store.run('DELETE FROM envs WHERE id = ?', id)
        throw err
      }
    }
    const env = (await store.envById(id))!
    return c.json(await envSummary(ctx, app, env), 201)
  })

  r.get('/envs/:env', async (c) => {
    const env = await envOf(ctx, c)
    return c.json(await envSummary(ctx, c.get('app'), env))
  })

  r.delete('/envs/:env', async (c) => {
    const app = c.get('app')
    const env = await envOf(ctx, c)
    if (env.name === 'main') throw fail('forbidden', 'the main environment cannot be deleted')
    const placeholders = ACTIVE_STATUSES.map(() => '?').join(',')
    if (
      (await store.deploymentsWhere(`env_id = ? AND status IN (${placeholders})`, env.id, ...ACTIVE_STATUSES)).length
    ) {
      throw fail('conflict', `env "${env.name}" has a deployment in progress`, 'cancel it or wait for it to finish')
    }
    for (const boxId of await ctx.boxes.list(app, env)) await ctx.boxlite.deleteBox(boxId)
    await ctx.resources.deleteVolumes(app, env)
    if (env.neon_branch_id && app.neon_project_id && env.neon_branch_id !== app.neon_main_branch_id) {
      await ctx.neon.deleteBranch(app.neon_project_id, env.neon_branch_id)
    }
    await ctx.domains.releaseEnvironment(app, env)
    await store.tx(async () => {
      await store.run('DELETE FROM deployment_events WHERE env_id = ?', env.id)
      await store.run('DELETE FROM logs WHERE env_id = ?', env.id)
      await store.run('DELETE FROM envs WHERE id = ?', env.id)
    })
    await dropPreview(ctx, env.id)
    return c.json({ deleted: env.name })
  })

  r.get('/envs/:env/spec', async (c) => {
    const env = await envOf(ctx, c)
    const v = c.req.query('version')
    const spec = await store.spec(env.id, v ? Number(v) : undefined)
    if (!spec)
      throw fail(
        'not_found',
        v ? `no spec version ${v}` : 'no spec yet',
        'run `boxlite app deploy --config-only --yes` or `boxlite app deploy` first',
      )
    return c.json(env.public_url ? spec : { ...spec, assigned: { ...spec.assigned, app_url: null } })
  })

  return r
}