- 替换 react-simple-maps/d3-geo/topojson-client 为 react-map-gl + maplibre-gl - 使用 CARTO dark-matter 免费暗色瓦片,自带国家/城市名标注 - 基于 locale 动态切换地图标注语言(name:zh / name_en) - MapLibre 原生 heatmap + circle 双层渲染替代 SVG 热力图 - 提取 MapPopup 组件,配合 react-map-gl Popup 实现点击弹窗 - continent page 改为 dynamic import (ssr: false) - dev 模式去掉 Turbopack 以兼容 maplibre-gl - 删除 heatmap-layer.tsx 和 react-simple-maps 类型声明
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { nanoid } from "nanoid";
|
|
import { db } from "@/lib/db";
|
|
import { claws } from "@/lib/db/schema";
|
|
import {
|
|
setClawOnline,
|
|
updateActiveClaws,
|
|
incrementGlobalStat,
|
|
incrementRegionCount,
|
|
publishEvent,
|
|
} from "@/lib/redis";
|
|
import { generateApiKey } from "@/lib/auth/api-key";
|
|
import { getGeoLocation } from "@/lib/geo/ip-location";
|
|
import { registerSchema } from "@/lib/validators/schemas";
|
|
|
|
function getClientIp(req: NextRequest): string {
|
|
const forwarded = req.headers.get("x-forwarded-for");
|
|
if (forwarded) return forwarded.split(",")[0].trim();
|
|
const realIp = req.headers.get("x-real-ip");
|
|
if (realIp) return realIp.trim();
|
|
return "127.0.0.1";
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const body = await req.json();
|
|
const parsed = registerSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: "Validation failed", details: parsed.error.flatten() },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const { name, model, platform } = parsed.data;
|
|
const clawId = nanoid(21);
|
|
const apiKey = generateApiKey();
|
|
const clientIp = getClientIp(req);
|
|
const geo = await getGeoLocation(clientIp);
|
|
const now = new Date();
|
|
|
|
await db.insert(claws).values({
|
|
id: clawId,
|
|
apiKey,
|
|
name,
|
|
model: model ?? null,
|
|
platform: platform ?? null,
|
|
ip: clientIp,
|
|
latitude: geo ? String(geo.latitude) : null,
|
|
longitude: geo ? String(geo.longitude) : null,
|
|
city: geo?.city ?? null,
|
|
country: geo?.country ?? null,
|
|
countryCode: geo?.countryCode ?? null,
|
|
region: geo?.region ?? null,
|
|
lastHeartbeat: now,
|
|
totalTasks: 0,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
await setClawOnline(clawId, clientIp);
|
|
await updateActiveClaws(clawId);
|
|
await incrementGlobalStat("total_claws");
|
|
|
|
if (geo?.region) {
|
|
await incrementRegionCount(geo.region);
|
|
}
|
|
|
|
await publishEvent({
|
|
type: "registered",
|
|
clawId,
|
|
clawName: name,
|
|
city: geo?.city ?? null,
|
|
country: geo?.country ?? null,
|
|
});
|
|
|
|
return NextResponse.json({
|
|
clawId,
|
|
apiKey,
|
|
endpoint: `${process.env.NEXT_PUBLIC_APP_URL}/api/v1`,
|
|
});
|
|
} catch (error) {
|
|
console.error("Register error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|