修复middleware规则和删除test相关的文件
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
// src/app/api/test/route.ts
|
||||
import { db } from '@/lib/db'
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
// src/app/api/test/route.ts
|
||||
export async function GET() {
|
||||
try {
|
||||
// 测试数据库连接
|
||||
await db.$queryRaw`SELECT 1`
|
||||
|
||||
// 暂时注释掉用户查询,因为表可能还不存在
|
||||
// const users = await db.user.findMany()
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '数据库连接成功',
|
||||
// userCount: users.length,
|
||||
// users: users
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('数据库测试错误:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
message: '数据库连接失败'
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
|
||||
export default function DebugPage() {
|
||||
const [results, setResults] = useState<Record<string, unknown>>({})
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const testAllEndpoints = async () => {
|
||||
setLoading(true)
|
||||
const endpoints = [
|
||||
'gateway_config',
|
||||
'city_config_count',
|
||||
'city_node_count',
|
||||
'allocation_status',
|
||||
'duplicate_nodes'
|
||||
]
|
||||
|
||||
const results: Record<string, unknown> = {}
|
||||
|
||||
for (const endpoint of endpoints) {
|
||||
try {
|
||||
const response = await fetch(`/api/stats?type=${endpoint}`)
|
||||
results[endpoint] = {
|
||||
status: response.status,
|
||||
data: await response.json()
|
||||
}
|
||||
} catch (error) {
|
||||
results[endpoint] = {
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setResults(results)
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<h1 className="text-2xl font-bold mb-4">API调试页面</h1>
|
||||
<button
|
||||
onClick={testAllEndpoints}
|
||||
disabled={loading}
|
||||
className="bg-blue-500 text-white px-4 py-2 rounded disabled:bg-gray-400"
|
||||
>
|
||||
{loading ? '测试中...' : '测试所有API端点'}
|
||||
</button>
|
||||
|
||||
<div className="mt-6">
|
||||
<h2 className="text-xl font-semibold mb-4">测试结果:</h2>
|
||||
<pre className="bg-gray-100 p-4 rounded overflow-auto">
|
||||
{JSON.stringify(results, null, 2)}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<div className="mt-6">
|
||||
<h2 className="text-xl font-semibold mb-4">数据库连接测试:</h2>
|
||||
<button
|
||||
onClick={async () => {
|
||||
try {
|
||||
const response = await fetch('/api/test')
|
||||
const result = await response.json()
|
||||
console.log('数据库测试:', result)
|
||||
alert(JSON.stringify(result, null, 2))
|
||||
} catch (error) {
|
||||
alert('测试失败: ' + (error instanceof Error ? error.message : 'Unknown error'))
|
||||
}
|
||||
}}
|
||||
className="bg-green-500 text-white px-4 py-2 rounded"
|
||||
>
|
||||
测试数据库连接
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
export default async function Home() {
|
||||
const prisma = new PrismaClient()
|
||||
const data = await prisma.change.findMany({ take: 10 })
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>数据库连接成功!</h1>
|
||||
<pre>{JSON.stringify(data, null, 2)}</pre>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
// 测试查询
|
||||
const changes = await prisma.change.findMany({
|
||||
take: 5
|
||||
})
|
||||
console.log('✅ 数据库连接成功!')
|
||||
console.log('获取到的数据:', changes)
|
||||
} catch (error) {
|
||||
console.error('❌ 连接失败:', error)
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -1,8 +0,0 @@
|
||||
// import { PrismaClient } from '@prisma/client'
|
||||
|
||||
// const globalForPrisma = global as { prisma?: PrismaClient }
|
||||
|
||||
// export const db = globalForPrisma.prisma || new PrismaClient()
|
||||
|
||||
// if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db
|
||||
export { prisma as db } from './prisma'
|
||||
@@ -1,29 +1,37 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
'/((?!_next|.well-known|favicon.ico|sitemap.xml|robots.txt).*(?<!\.svg|\.webp|\.jpg)$)',
|
||||
],
|
||||
}
|
||||
|
||||
const isIgnored = [
|
||||
'/login',
|
||||
"/api/auth/login"
|
||||
]
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
const session = request.cookies.get('session')
|
||||
console.log(session, 'sessionsessionsession');
|
||||
|
||||
// 保护的路由需要验证会话
|
||||
if (request.nextUrl.pathname.startsWith('/dashboard')) {
|
||||
if (!session?.value) {
|
||||
return NextResponse.redirect(new URL('/login', request.url))
|
||||
// 检查用户是否未登录
|
||||
if (!session?.value) {
|
||||
// 如果访问的是无需登录的页面,直接允许访问
|
||||
if (isIgnored.some(path => request.nextUrl.pathname.startsWith(path))) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
// 其他路径都视为受保护路径,重定向到登录页
|
||||
console.log(request.nextUrl.pathname, 22222222222222222);
|
||||
return NextResponse.redirect(new URL('/login', request.url))
|
||||
}
|
||||
|
||||
// 已登录用户重定向
|
||||
if (session?.value && (
|
||||
request.nextUrl.pathname === '/login' ||
|
||||
request.nextUrl.pathname === '/register'
|
||||
)) {
|
||||
console.log(session, 'sessionsessionsession');
|
||||
// 给没有页面的路径添加跳转页面
|
||||
if (request.nextUrl.pathname === '/') {
|
||||
console.log(request.nextUrl.pathname, 333333333333333333);
|
||||
return NextResponse.redirect(new URL('/dashboard', request.url))
|
||||
}
|
||||
|
||||
console.log(request.nextUrl.pathname, 44444444444444444444);
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/dashboard/:path*', '/login', '/register']
|
||||
}
|
||||
Reference in New Issue
Block a user