mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-26 02:04:37 +08:00
refactor(http): organize shared route package
This commit is contained in:
1 parent
bf7004d0e0
commit
4b52a7d2f8
35 files changed
+84
-217
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { describe, it, beforeEach } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { setAuthToken, setRequireAuth, authHook } from '@openchatlab/http-routes/auth'
|
||||
import { setAuthToken, setRequireAuth, authHook } from './auth'
|
||||
|
||||
function fakeRequest(url: string, authorization?: string) {
|
||||
return { url, headers: { authorization } } as never
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/** CLI HTTP authentication policy built on the shared Bearer hook. */
|
||||
|
||||
import { createBearerAuthHook } from '@openchatlab/http-routes/auth'
|
||||
|
||||
let cachedToken: string | null = null
|
||||
let requireAuthEnabled = false
|
||||
|
||||
export function setAuthToken(token: string): void {
|
||||
cachedToken = token
|
||||
}
|
||||
|
||||
/** Require Bearer auth for Web UI routes in headless or remote deployments. */
|
||||
export function setRequireAuth(enabled: boolean): void {
|
||||
requireAuthEnabled = enabled
|
||||
}
|
||||
|
||||
export const authHook = createBearerAuthHook({
|
||||
getToken: () => cachedToken,
|
||||
shouldAuthenticate: (request) =>
|
||||
request.url.startsWith('/api/') || (requireAuthEnabled && request.url.startsWith('/_web/')),
|
||||
})
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
CustomProviderStore,
|
||||
CustomModelStore,
|
||||
createFileConfigStorage,
|
||||
createAuthProfileLlmConfigStore,
|
||||
applyPendingNodeDataDirMigrationIfNeeded,
|
||||
hasPendingElectronDataWarning,
|
||||
verifyCliDataPath,
|
||||
@@ -27,8 +28,7 @@ import {
|
||||
} from '@openchatlab/node-runtime'
|
||||
import type { SemanticIndexRuntime } from '@openchatlab/node-runtime'
|
||||
import { createServer } from './server'
|
||||
import { setAuthToken, setRequireAuth } from '@openchatlab/http-routes/auth'
|
||||
import { createAuthProfileLlmConfigStore } from '@openchatlab/http-routes/ai-config'
|
||||
import { setAuthToken, setRequireAuth } from './auth'
|
||||
import { registerWebRoutes } from './routes/web'
|
||||
import { registerProxyRoutes } from './routes/proxy'
|
||||
import { initServerAiLogger, closeServerAiLogger } from '../ai/logger'
|
||||
|
||||
@@ -3,20 +3,9 @@
|
||||
*/
|
||||
|
||||
import * as fs from 'fs'
|
||||
import type { TimeFilter } from '@openchatlab/shared-types'
|
||||
import type { DatabaseManager } from '@openchatlab/node-runtime'
|
||||
import { resolveCliPath } from '../../../paths'
|
||||
|
||||
export function parseTimeFilter(query: Record<string, string | undefined>): TimeFilter | undefined {
|
||||
const { startTs, endTs, memberId } = query
|
||||
if (!startTs && !endTs && !memberId) return undefined
|
||||
const filter: TimeFilter = {}
|
||||
if (startTs) filter.startTs = parseInt(startTs, 10)
|
||||
if (endTs) filter.endTs = parseInt(endTs, 10)
|
||||
if (memberId) filter.memberId = parseInt(memberId, 10)
|
||||
return filter
|
||||
}
|
||||
|
||||
export function resolveNativeBinding(): string | undefined {
|
||||
if (process.versions.electron) return undefined
|
||||
const nativePath = resolveCliPath('native/better_sqlite3.node')
|
||||
@@ -31,11 +20,3 @@ export function getAiDataDir(dbManager: DatabaseManager): string {
|
||||
}
|
||||
return pathProvider.getAiDataDir()
|
||||
}
|
||||
|
||||
export function ensureDb(dbManager: DatabaseManager, sessionId: string) {
|
||||
const db = dbManager.open(sessionId)
|
||||
if (!db) {
|
||||
throw Object.assign(new Error(`Session not found: ${sessionId}`), { statusCode: 404 })
|
||||
}
|
||||
return db
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import Fastify from 'fastify'
|
||||
import type { SessionRuntimeAdapter } from '@openchatlab/node-runtime'
|
||||
import type { HttpRouteContext } from '@openchatlab/http-routes'
|
||||
import { registerSessionRoutes } from '@openchatlab/http-routes'
|
||||
|
||||
function createMissingSessionAdapter(): SessionRuntimeAdapter {
|
||||
return {
|
||||
listSessionIds: () => [],
|
||||
openReadonly: () => null,
|
||||
openWritable: () => null,
|
||||
closeSession: () => {},
|
||||
getDbPath: (sessionId) => `/tmp/${sessionId}.db`,
|
||||
deleteSessionFile: () => false,
|
||||
ensureReadonly: () => {
|
||||
throw Object.assign(new Error('Session not found'), { statusCode: 404 })
|
||||
},
|
||||
ensureWritable: () => {
|
||||
throw Object.assign(new Error('Session not found'), { statusCode: 404 })
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function createMockContext(adapter: SessionRuntimeAdapter): HttpRouteContext {
|
||||
return {
|
||||
dbManager: {} as any,
|
||||
sessionAdapter: adapter,
|
||||
pathProvider: {} as any,
|
||||
getVersion: () => '0.0.0-test',
|
||||
}
|
||||
}
|
||||
|
||||
describe('shared session routes', () => {
|
||||
it('returns 404 when requesting a missing session by id', async () => {
|
||||
const app = Fastify()
|
||||
const ctx = createMockContext(createMissingSessionAdapter())
|
||||
registerSessionRoutes(app, ctx)
|
||||
|
||||
const response = await app.inject({ method: 'GET', url: '/_web/sessions/missing' })
|
||||
await app.close()
|
||||
|
||||
assert.equal(response.statusCode, 404)
|
||||
})
|
||||
})
|
||||
@@ -5,9 +5,9 @@
|
||||
*/
|
||||
|
||||
import type { FastifyInstance } from 'fastify'
|
||||
import { authHook } from '@openchatlab/http-routes/auth'
|
||||
import { createApiServer } from '@openchatlab/http-routes/server'
|
||||
import { appLogger } from '@openchatlab/node-runtime'
|
||||
import { authHook } from './auth'
|
||||
|
||||
export function createServer(): FastifyInstance {
|
||||
return createApiServer({
|
||||
|
||||
Reference in new issue
Block a user