Files
ChatLab/apps/desktop/main/api/server.ts
T
digua b12c07dc32 refactor: extract Electron shell to apps/desktop/
Move Electron-specific code (main, preload, shared) and configs
(electron.vite, electron-builder) into apps/desktop/ to prepare
for multi-platform support (Web Browser standalone).

- Create apps/desktop/ with dedicated package.json and tsconfig
- Split Electron deps (electron, electron-updater, better-sqlite3,
  etc.) from shared frontend deps in root package.json
- Declare better-sqlite3 and @node-rs/jieba in packages/node-runtime
- Add stream-json to packages/parser where it belongs
- Update all path aliases (@electron/* → apps/desktop/*) in tsconfig,
  vite configs, and relative imports in preload/main
- Keep slim root tsconfig.node.json as shared base for packages
- Update CI/CD release workflow for new directory layout
- Fix pre-existing type errors (pi-ai Model generic, SQLLabTab
  LLMChatStreamChunk, filter.test.ts empty methods)
2026-05-19 00:31:00 +08:00

39 lines
1.0 KiB
TypeScript

/**
* ChatLab API — Fastify server instance
*/
import Fastify, { type FastifyInstance, type FastifyError } from 'fastify'
import { authHook } from './auth'
import { ApiError, ApiErrorCode, errorResponse, serverError } from './errors'
import { apiLogger } from './logger'
const JSON_BODY_LIMIT = 50 * 1024 * 1024 // 50MB
export function createServer(): FastifyInstance {
const server = Fastify({
logger: false,
bodyLimit: JSON_BODY_LIMIT,
})
server.addHook('onRequest', authHook)
server.setErrorHandler((error: FastifyError, _request, reply) => {
if (error instanceof ApiError) {
reply.code(error.statusCode).send(errorResponse(error))
return
}
if (error.statusCode === 413) {
const bodyErr = new ApiError(ApiErrorCode.BODY_TOO_LARGE, 'Request body exceeds 50MB limit')
reply.code(413).send(errorResponse(bodyErr))
return
}
apiLogger.error('Unhandled error', error)
const err = serverError(error.message)
reply.code(err.statusCode).send(errorResponse(err))
})
return server
}