dipend-graph
Version:
A library for generating a DAG (Directed Acyclic Graph) of dependencies registered in Dipend's dependency container.
68 lines (66 loc) • 2.58 kB
JavaScript
/*
* Copyright 2025 Saulo V. Alvarenga. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestHandler = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const SCRIPT_DIR = __dirname;
const DIRECTORY = path_1.default.join(SCRIPT_DIR, "../public");
class RequestHandler {
graphDataHandler;
constructor(graphDataHandler) {
this.graphDataHandler = graphDataHandler;
}
apiDataRouteAction(res) {
res.writeHead(200, { "Content-Type": "application/json" });
const response = JSON.stringify(this.graphDataHandler.handle());
res.end(response);
}
getContentType(filePath) {
const ext = path_1.default.extname(filePath);
const contentTypes = {
".html": "text/html",
".css": "text/css",
".js": "text/javascript",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
};
return contentTypes[ext] || "application/octet-stream";
}
homeRouteAction(req, res) {
let filePath = path_1.default.join(DIRECTORY, req.url === undefined || req.url === "/" ? "index.html" : req.url);
fs_1.default.access(filePath, fs_1.default.constants.F_OK, (err) => {
if (err)
filePath = path_1.default.join(DIRECTORY, "index.html");
fs_1.default.readFile(filePath, (error, content) => {
if (error) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("500 - Internal Server Error");
}
else {
res.writeHead(200, {
"Content-Type": this.getContentType(filePath),
});
res.end(content);
}
});
});
}
}
exports.RequestHandler = RequestHandler;