From 8680362c750ee7eb287a9bf6a8624daccec0a44c Mon Sep 17 00:00:00 2001 From: Lxy Date: Thu, 4 Aug 2022 10:48:18 +0800 Subject: [PATCH] cli: optimize resource services --- doric-cli/src/resourceServer.ts | 57 ++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/doric-cli/src/resourceServer.ts b/doric-cli/src/resourceServer.ts index 0641ad9d..c237b105 100644 --- a/doric-cli/src/resourceServer.ts +++ b/doric-cli/src/resourceServer.ts @@ -1,51 +1,62 @@ import fs from "fs" import path from "path" -import http from 'http'; +import http from "http"; import { IncomingMessage, ServerResponse } from "http"; export async function createResServer() { const port = 7778; const server = http.createServer(); server.listen(port); - console.log(`Resource server is listening on port ${port} !`.green); - server.on('request', (request: IncomingMessage, response: ServerResponse) => { + console.log(`resource server is listening on port ${port} !`.green); + server.on("request", (request: IncomingMessage, response: ServerResponse) => { const { url } = request; if (url) { try { const pathArr = url.split("/"); const resName = pathArr[pathArr.length - 1]; const dirPath = path.join(process.cwd(), url.replace(`/${resName}`, "")); - const fileRealName = findResRealName(dirPath, resName); - const resourcePath = path.join(dirPath, `${fileRealName}`); - if (fs.existsSync(resourcePath)) { + const resourcePath = findFilePath(dirPath, resName); + if (resourcePath.length != 0) { const data = fs.readFileSync(resourcePath); - response.setHeader("content-length", data.length); + response.setHeader("Content-Length", data.length); response.write(data); response.end(); } else { - response.setHeader("content-type", "text/html; charset=utf-8"); - console.error(`Resources ${resName} do not exist !`.red) - response.end(`Resources ${resName} do not exist !`); + console.error(`resource ${resName} does not exist !`.red) + response.writeHead(404, { "Content-Type": "text/plain" }); + response.end("Not Found"); } } catch (error) { - response.setHeader("content-type", "text/html; charset=utf-8"); - console.error(`Server internal error`.red) - response.end("Server internal error" + error); + response.writeHead(500, { "Content-Type": "text/plain" }); + console.error("server internal error".red, error); + response.end("server internal error" + error); } } else { - response.setHeader("content-type", "text/html; charset=utf-8"); - console.error(`url is undefined`.red) + response.setHeader("Content-Type", "text/html; charset=utf-8"); + console.error("url is undefined".red); response.end("url is undefined"); + } }); +} - function findResRealName(dirPath: string, fileName: string) { - let fileFinalName = ""; - const stats = fs.readdirSync(dirPath); - for (let stat of stats) { - if (stat.startsWith(`${fileName}.`) || stat === fileName) { - fileFinalName = stat; - return fileFinalName; +function findFilePath(dirPath: string, fileName: string) { + if (!fs.existsSync(dirPath)) { + return ""; + } + const stats = fs.readdirSync(dirPath); + for (let item of stats) { + const itemPath = path.join(dirPath, item); + const stat = fs.statSync(itemPath); + if (stat.isDirectory()) { + const ret = findFilePath(itemPath, fileName) as string; + if (ret.length != 0) { + return ret; + } + } else { + if (item.startsWith(`${fileName}.`) || item === fileName) { + return path.join(dirPath, item); } } } -} + return ""; +} \ No newline at end of file