From fc742ddad2ce17a159c887466e8f66547a0e5de0 Mon Sep 17 00:00:00 2001 From: zanllp Date: Tue, 6 Jun 2023 21:16:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E9=87=8D=E5=86=99=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/http_server.as | 30 +++++++++++++++------------- script/playground.as | 46 +++++++++++-------------------------------- 2 files changed, 27 insertions(+), 49 deletions(-) diff --git a/script/http_server.as b/script/http_server.as index f8fdb26..fc018c4 100644 --- a/script/http_server.as +++ b/script/http_server.as @@ -1,7 +1,6 @@ include('lib/server/http_server') include('lib/message_queue/client') - -const port = 8811 +const port = to_num((env().process_arg.port) || 8080) const url = f('http://127.0.0.1:{}', port) const global_store = { @@ -37,19 +36,22 @@ const get_files_html = (dir = '') => { const route = (params, resp) => { const curr_dir = env().curr_dir() - const path = params.path - (!path) ? @{ - const res = get_files_html() - resp.set_status(200).set_data(f('
{}
', res.join(cr))).end() - } : (is_dir(path)) ? @{ - const res = get_files_html(path) - resp.set_status(200).set_data(f('
{}
', res.join(cr))).end() - } : (fs.exist(path)) ? @{ - // path = path_calc(curr_dir, path) - resp.set_status(200).set_data(f('
{}
', fs.read(path))).end() - } : @{ - resp.set_status(404).set_data('

404

').end() + const res = match (const path = params.path) { + null: { + data: f('
{}
', get_files_html().join(cr)), + code: 200 + }, + is_dir(path): { + data: f('
{}
', get_files_html(path).join(cr)), + code: 200 + }, + fs.exist(path): { + data: f('
{}
', fs.read(path)), + code: 200 + }, + _: { code: 404, data: '

404

' } } + resp.set_status(res.code).set_data(res.data).end() } // /* make_http_server(port, { diff --git a/script/playground.as b/script/playground.as index f0051fa..4ac9905 100644 --- a/script/playground.as +++ b/script/playground.as @@ -1,48 +1,24 @@ -/*const dd = () => { - const obj = { id: start_timer(() => log('call'), 30) } - make_promise(cb => { - fetch('http://127.0.0.1:8890', { method: 'get' }).then(v => { - log(v.err_msg) - remove_timer(obj.id) - cb(v) - }) - }) -} -dd().then(v => log(f('end {}', v.keys().join(',')))) - -const delayExec = (fn, timeout = 1000) => start_timer(fn,timeout, true) -*/ const json_str = fs.read("/Users/wuqinchuan/Desktop/project/agumi/canada.json") const j = json.parse(json_str) @{ - const fn = (node) => { - const t = typeof(node) - (t == "object") ? @{ - node.values().select(fn) - } : (t == "array") ? @{ - node.select(fn) - } : log(node) - - } - // fn(j) + const arr = [] + const fn = (node) => match (typeof(node)) { + "object" : node.values().select(fn), + "array": node.select(fn), + _: arr.push(node) + } + fn(j) + fs.write("./res.json", json.stringify(arr)) } +/* const t = time() for(5, () => { - // json.parse(json_str) + json.parse(json_str) }) log((time() - t)/5) - -const match = (params) => { - assert(typeof(params), "object") - full_log(params.values()) -} - -match({ - "hello": 1, - world: 2 -}) +*/ From a50103509e1cbf6cfd266c68852be4635a6941e7 Mon Sep 17 00:00:00 2001 From: zanllp Date: Tue, 6 Jun 2023 21:16:25 +0800 Subject: [PATCH 2/2] lib --- script/lib/index.as | 2 ++ 1 file changed, 2 insertions(+) diff --git a/script/lib/index.as b/script/lib/index.as index 71c42c8..d18b417 100644 --- a/script/lib/index.as +++ b/script/lib/index.as @@ -21,6 +21,8 @@ const for = (count, cb) => { null } +const to_num = (str) => json.parse(str) + const cowsay = msg => { log(f(` ____________