From 5a7d2b239675eb989ad63c4d5b434517cfd803c2 Mon Sep 17 00:00:00 2001 From: master Date: Sun, 7 Apr 2024 18:44:27 +0800 Subject: [PATCH] function getUserToken ready --- common.mjs | 27 +++++++++++++++++++++++++++ conf/config.json | 7 +++++++ heroUnion.mjs | 24 ++++++++++++++++++------ test/common.test.mjs | 9 +++++++++ 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 conf/config.json diff --git a/common.mjs b/common.mjs index 0dba365..4b7e3c0 100644 --- a/common.mjs +++ b/common.mjs @@ -4,10 +4,16 @@ import fs from 'node:fs'; import { readdir, readFile } from 'node:fs/promises'; +import { resolve } from 'node:path'; import md5 from 'md5'; class Common { + //构造函数,设置默认配置 + constructor() { + this.configDir = resolve('conf/'); + } + sortDict(obj) { //dict按key排序 return Object.keys(obj).sort().reduce(function (result, key) { result[key] = obj[key]; @@ -41,6 +47,27 @@ class Common { return md5( JSON.stringify(this.sortDict(params)) + token ); } + //从conf/目录读取配置文件内容 + async getConfigFromJsonFile(filename) { + let data = null; + + let filePath = this.configDir + `/${filename}`; + if (fs.existsSync(filePath)) { + try { + const contents = await readFile(filePath, { encoding: 'utf8' }); + if (contents) { + data = JSON.parse(contents); + } + } catch (err) { + console.error(`[FAILED] get config content from %s failed, error: %s`, filePath, err.message); + } + }else { + console.error("[ERROR] file %s not exist.", filePath); + } + + return data; + } + } let commonFuns = new Common(); diff --git a/conf/config.json b/conf/config.json new file mode 100644 index 0000000..1cf4750 --- /dev/null +++ b/conf/config.json @@ -0,0 +1,7 @@ +{ + "name": "Hero Union", + "version": "0.0.1", + "tokens": { + "demo": "hello#world!" + } +} \ No newline at end of file diff --git a/heroUnion.mjs b/heroUnion.mjs index 3fe45f4..acdc98e 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -28,6 +28,8 @@ class HeroUnion { //构造函数,设置默认配置 constructor() { + this.config = null; + //this.task_data_dir = path.resolve('./tmp/data/'); //任务数据保存目录 this.task_cache_time = 86400; //任务数据最长缓存时间,单位:秒 this.task_data_max_size = 1024; //任务数据最大字节数,单位:KB @@ -73,6 +75,14 @@ class HeroUnion { return JSON.stringify(data).length > this.task_data_max_size * 1024; } + async getConfig(forceReload) { + if ( !this.config || (typeof(forceReload) != 'undefined' && forceReload) ) { + this.config = await common.getConfigFromJsonFile('config.json'); + } + + return this.config; + } + //--任务相关功能-- //根据任务提交者ID和时间戳生成任务ID编号 @@ -213,9 +223,10 @@ class HeroUnion { return this.tasks.find((item) => item.id == id); } - //TODO: 根据uuid获取用户的签名密钥 - getUserToken(uuid) { - return 'token'; + //根据uuid获取用户的签名密钥 + async getUserToken(uuid) { + let config = await this.getConfig(); + return config && typeof(config.tokens[uuid]) != 'undefined' ? config.tokens[uuid] : ''; } //任务完成触发回调通知 @@ -232,17 +243,18 @@ class HeroUnion { "task_result": task.results, "timestamp": this.getTimestamp(), }; - params.sign = common.sign(params, this.getUserToken(task.uuid)); + let token = await this.getUserToken(task.uuid); + params.sign = common.sign(params, token); const response = await axios.post(notify_url, params, {timeout: this.notify_timeout*1000}); if (response.status == 200) { notified = true; }else { - console.error('Notify to %s failed, response status: %s, status text: %s, result: %s', + console.error('[FAILED] Notify to %s failed, response status: %s, status text: %s, result: %s', notify_url, response.status, response.statusText, response.daa); } } }catch(err) { - console.error('Notify to %s failed: %s', notify_url, err); + console.error('[ERROR] Notify to %s failed: %s', notify_url, err); } return notified; diff --git a/test/common.test.mjs b/test/common.test.mjs index 5566729..353acca 100644 --- a/test/common.test.mjs +++ b/test/common.test.mjs @@ -32,3 +32,12 @@ test('Common function joinDict test', (t) => { assert.strictEqual(common.joinDict(common.sortDict(params)), expectRes); }); +test('Common function getConfigFromJsonFile test', async (t) => { + let filename = 'config.json'; + let config = await common.getConfigFromJsonFile(filename); + + assert.ok(config); + + const expectName = 'Hero Union'; + assert.strictEqual(config.name, expectName); +});