Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/server/src/api/routes/shutdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright 2025 BrowserOS
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Hono } from 'hono'

interface ShutdownRouteConfig {
onShutdown: () => void
}

export function createShutdownRoute(config: ShutdownRouteConfig) {
return new Hono().post('/', (c) => {
setImmediate(config.onShutdown)
return c.json({ status: 'ok' })
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import { Hono } from 'hono'
import type { ControllerContext } from '../../browser/extension/context'

interface ExtensionStatusDeps {
interface StatusDeps {
controllerContext: ControllerContext
}

export function createExtensionStatusRoute(deps: ExtensionStatusDeps) {
export function createStatusRoute(deps: StatusDeps) {
const { controllerContext } = deps

return new Hono().get('/', (c) =>
Expand Down
10 changes: 6 additions & 4 deletions apps/server/src/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import { HttpAgentError } from '../agent/errors'
import { logger } from '../lib/logger'
import { bindPortWithRetry } from '../lib/port-binding'
import { createChatRoutes } from './routes/chat'
import { createExtensionStatusRoute } from './routes/extension-status'
import { createGraphRoutes } from './routes/graph'
import { createHealthRoute } from './routes/health'
import { createKlavisRoutes } from './routes/klavis'
import { createMcpRoutes } from './routes/mcp'
import { createProviderRoutes } from './routes/provider'
import { createSdkRoutes } from './routes/sdk'
import { createShutdownRoute } from './routes/shutdown'
import { createStatusRoute } from './routes/status'
import type { Env, HttpServerConfig } from './types'
import { defaultCorsConfig } from './utils/cors'

Expand All @@ -49,16 +50,17 @@ export async function createHttpServer(config: HttpServerConfig) {
allowRemote,
} = config

const { healthWatchdog } = config
const { healthWatchdog, onShutdown } = config

// DECLARATIVE route composition - chain .route() calls for type inference
const app = new Hono<Env>()
.use('/*', cors(defaultCorsConfig))
.route('/health', createHealthRoute({ watchdog: healthWatchdog }))
.route(
'/extension-status',
createExtensionStatusRoute({ controllerContext }),
'/shutdown',
createShutdownRoute({ onShutdown: onShutdown ?? (() => {}) }),
)
.route('/status', createStatusRoute({ controllerContext }))
.route('/test-provider', createProviderRoutes())
.route('/klavis', createKlavisRoutes({ browserosId: browserosId || '' }))
.route(
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export interface HttpServerConfig {

// For health monitoring
healthWatchdog?: HealthWatchdog

// For shutdown route
onShutdown?: () => void
}

// Graph request schemas
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class Application {
rateLimiter: new RateLimiter(this.getDb(), dailyRateLimit),
codegenServiceUrl: this.config.codegenServiceUrl,
healthWatchdog: this.healthWatchdog ?? undefined,
onShutdown: () => this.stop(),
})
} catch (error) {
this.handleStartupError('HTTP server', this.config.serverPort, error)
Expand Down
2 changes: 1 addition & 1 deletion apps/server/tests/__helpers__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const DEFAULT_BINARY_PATH =

async function isExtensionConnected(port: number): Promise<boolean> {
try {
const response = await fetch(`http://127.0.0.1:${port}/extension-status`, {
const response = await fetch(`http://127.0.0.1:${port}/status`, {
signal: AbortSignal.timeout(1000),
})
if (response.ok) {
Expand Down
4 changes: 2 additions & 2 deletions apps/server/tests/server.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ describe('HTTP Server Integration Tests', () => {
})
})

describe('Extension status endpoint', () => {
describe('Status endpoint', () => {
it('reports extension as connected', async () => {
const response = await fetch(`${getBaseUrl()}/extension-status`)
const response = await fetch(`${getBaseUrl()}/status`)
assert.strictEqual(response.status, 200)

const json = (await response.json()) as {
Expand Down
2 changes: 1 addition & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.