feat: 后端初始化2

This commit is contained in:
2026-07-28 15:43:27 +08:00
parent dfe0d6bd95
commit 53305757f8
26 changed files with 734 additions and 36 deletions
+9
View File
@@ -0,0 +1,9 @@
import CryptoJS from 'crypto-js'
export const sha256 = (value: string) => {
return CryptoJS.SHA256(value).toString(CryptoJS.enc.Hex)
}
export const createBridgeSignature = (clientId: string, clientSecret: string, timestamp: string) => {
return sha256(`${clientId}${clientSecret}${timestamp}${clientSecret}`)
}
+1 -1
View File
@@ -9,7 +9,7 @@ export const withErrorBoundary = (handler: FunctionHandler): FunctionHandler =>
return await handler(request, context)
} catch (error) {
console.error(error)
return fail('Internal server error', 500, 'INTERNAL_SERVER_ERROR')
return fail('服务异常,请稍后再试', 500, 'INTERNAL_SERVER_ERROR')
}
}
}
+10 -6
View File
@@ -1,10 +1,17 @@
export type JsonBody = Record<string, unknown> | unknown[]
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS'
}
export const json = (body: JsonBody, init: ResponseInit = {}) =>
Response.json(body, {
...init,
headers: {
'Content-Type': 'application/json',
...corsHeaders,
...(init.headers || {})
}
})
@@ -16,10 +23,11 @@ export const fail = (message: string, status = 400, code = 'BAD_REQUEST') =>
export const methodNotAllowed = (allowedMethods: string[]) =>
json(
{ success: false, error: { code: 'METHOD_NOT_ALLOWED', message: 'Method not allowed' } },
{ success: false, error: { code: 'METHOD_NOT_ALLOWED', message: '请求方式不支持' } },
{
status: 405,
headers: {
...corsHeaders,
Allow: allowedMethods.join(', ')
}
}
@@ -28,9 +36,5 @@ export const methodNotAllowed = (allowedMethods: string[]) =>
export const handleOptions = () =>
new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS'
}
headers: corsHeaders
})
+2 -9
View File
@@ -1,23 +1,16 @@
import { neonConfig } from '@neondatabase/serverless'
import { PrismaNeon } from '@prisma/adapter-neon'
import { PrismaClient } from '@prisma/client'
import ws from 'ws'
neonConfig.webSocketConstructor = ws
const connectionString = process.env.DATABASE_URL
if (!connectionString) {
throw new Error('DATABASE_URL is required')
throw new Error('缺少数据库连接配置 DATABASE_URL')
}
const globalForPrisma = globalThis as typeof globalThis & {
prisma?: PrismaClient
}
const adapter = new PrismaNeon({ connectionString })
export const prisma = globalForPrisma.prisma || new PrismaClient({ adapter })
export const prisma = globalForPrisma.prisma || new PrismaClient()
if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma