droboticflow/server.ts
longtao 04c777e3d4 feat: Initialize EmboFlow project structure
Sets up the basic project structure for EmboFlow, including necessary configuration files, README, and initial app components. This commit establishes the foundation for the no-code data processing platform for embodied intelligence scenarios.
2026-04-07 17:13:38 +08:00

106 lines
3.2 KiB
TypeScript

import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* EmboFlow Backend Architecture Simulation
*
* API: Express HTTP Server (this file)
* Worker: Background process polling "Mongo"
* MongoDB: Simulated in-memory store
*/
// Simulated MongoDB
const db = {
projects: [
{ id: "1", name: "具身智能抓取任务", description: "处理机械臂抓取过程中的视觉数据", createdAt: "2024-03-20" },
{ id: "2", name: "自动驾驶感知流程", description: "多传感器融合数据清洗与标注", createdAt: "2024-03-21" },
],
operators: [
{ id: "op-1", name: "数据下载", type: "builtin", category: "IO", description: "从 OSS 下载原始数据" },
{ id: "op-2", name: "图像预处理", type: "custom", category: "CV", description: "缩放、归一化图像数据" },
{ id: "op-3", name: "模型推理", type: "builtin", category: "AI", description: "运行目标检测模型" },
{ id: "op-4", name: "结果上传", type: "builtin", category: "IO", description: "将处理结果上传至 OSS" },
],
tasks: [] as any[],
};
// Worker Simulation
async function runWorker() {
console.log("[Worker] Started polling tasks from Mongo...");
while (true) {
const nextTask = db.tasks.find(t => t.status === "pending");
if (nextTask) {
nextTask.status = "running";
console.log(`[Worker] Executing task: ${nextTask.id} (${nextTask.workflowName})`);
// Simulate long-running job (Docker/K8s)
await new Promise(resolve => setTimeout(resolve, 5000));
nextTask.status = "completed";
console.log(`[Worker] Task completed: ${nextTask.id}`);
}
await new Promise(resolve => setTimeout(resolve, 2000)); // Poll every 2s
}
}
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// API Routes
app.get("/api/health", (req, res) => {
res.json({ status: "ok", service: "EmboFlow API" });
});
app.get("/api/projects", (req, res) => {
res.json(db.projects);
});
app.get("/api/operators", (req, res) => {
res.json(db.operators);
});
// Run Workflow (Push to Mongo for Worker)
app.post("/api/workflow/:id/run", (req, res) => {
const taskId = `run-${Date.now()}`;
db.tasks.push({
id: taskId,
workflowId: req.params.id,
workflowName: req.body.name || "Unknown Workflow",
status: "pending",
createdAt: new Date().toISOString()
});
res.json({ success: true, taskId });
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`[API] EmboFlow Server running on http://localhost:${PORT}`);
runWorker(); // Start worker in background
});
}
startServer();