From b1e1ba0f5f68a102cb5ab2f53399f9c8387c949b Mon Sep 17 00:00:00 2001 From: master Date: Sat, 16 Dec 2023 20:45:58 +0800 Subject: [PATCH] api defined --- heroUnion.mjs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.mjs | 4 +-- router_api.mjs | 45 +++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 heroUnion.mjs create mode 100644 router_api.mjs diff --git a/heroUnion.mjs b/heroUnion.mjs new file mode 100644 index 0000000..ab6f758 --- /dev/null +++ b/heroUnion.mjs @@ -0,0 +1,77 @@ +/** + * Hero管理、调度 + * --使用流程-- + * 1. 本地启动machete_hero爬虫,它会主动连接本联盟,并加入到爬虫队列等待处理任务(爬虫会定时上报自己的状态给联盟); + * 2. 联盟收到新任务时,存入待处理队列,等待在线的爬虫来获取; + * 3. 爬虫获取到新任务处理完成后,将结果回传给联盟; + * 4. 联盟收到爬虫处理结果触发回调通知并将数据结果发送给任务提交者; + * 5. 任务提交者可自行根据任务编号来联盟查询任务结果; + * + * --并发处理规则-- + * 同一个任务可以被分配给多个爬虫 + * 同一个任务可以接收不同爬虫回传的数据,并完成回调 + * + * --数据缓存规则-- + * 任务结果数据最大不超过1M,超过的当任务处理失败处理 + * 任务数据保存最长 1 天 + */ + +import fs from 'node:fs'; +import { readdir, readFile } from 'node:fs/promises'; +import path from 'node:path'; +import cron from 'node-cron'; + + +class HeroUnion { + + //构造函数,设置默认配置 + constructor() { + this.stats = {}; + + } + + //--任务相关功能-- + + //提交新任务 + createTask() { + + } + + //获取 1 个待处理的任务 + getWaitingTask() { + + } + + //保存任务处理结果 + async saveTaskById() { + + } + + //查询某个任务的状态及其数据 + getTaskById() { + + } + + //任务完成触发回调通知 + async handleTaskDone() { + //当任务完成回传回来的时候调用此方法触发回调通知 + + } + + + //--爬虫相关功能-- + + //接收爬虫状态上报 + heroOnboard() { + + } + + //获取联盟状态 + stats() { + return this.stats; + } + +} + + +export default HeroUnion; diff --git a/index.mjs b/index.mjs index 01e786e..b4af2d3 100644 --- a/index.mjs +++ b/index.mjs @@ -4,6 +4,7 @@ import express from 'express'; import bodyParser from 'body-parser'; +import apiRouter from './router_api.mjs'; const app = express(); @@ -19,8 +20,7 @@ app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json()) -//TODO: add api handler - +app.use('/api', apiRouter); app.get('/', (req, res) => { return res.send('Welcome to Hero Union of filesite.io'); diff --git a/router_api.mjs b/router_api.mjs new file mode 100644 index 0000000..027e32c --- /dev/null +++ b/router_api.mjs @@ -0,0 +1,45 @@ +/** + * Express router of api/ + */ + +import express from 'express'; + + +const router = express.Router(); + +router.get('/', async (req, res) => { + + return res.send('api/'); +}); + +router.post('/newtask/', async (req, res) => { + + return res.send('api/newtask/'); +}); + +router.get('/gettask/', async (req, res) => { + + return res.send('api/gettask/'); +}); + +router.post('/savetask/', async (req, res) => { + + return res.send('api/savetask/'); +}); + +router.get('/querytask/', async (req, res) => { + + return res.send('api/querytask/'); +}); + +router.post('/onboard/', async (req, res) => { + + return res.send('api/onboard/'); +}); + +router.get('/stats/', async (req, res) => { + + return res.send('api/stats/'); +}); + +export default router;