From 9c291b459026044e5c59ca6b206d80451c750153 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Thu, 23 Jun 2022 18:45:15 +0200 Subject: [PATCH] remove open | define JssteStates | add simple jsste routing --- docker-compose.yml | 6 ++--- dockerfile | 9 +++---- package.json | 6 ++++- src/helper/states.js | 6 ++++- src/index.js | 2 +- src/jsste/index.js | 30 ++++++++++++++++++++- src/websrv/index.js | 41 +++++++++++++++++++++++++++++ src/websrv/routes/internalRouter.js | 10 ++++--- 8 files changed, 94 insertions(+), 16 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index fee56ee..b846b2e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '3' services: jsste: build: . - volumes: - - ./:/app ports: - - "8080:8080" \ No newline at end of file + - "8080:8080" + volumes: + - .:/app \ No newline at end of file diff --git a/dockerfile b/dockerfile index 7798322..5517be7 100644 --- a/dockerfile +++ b/dockerfile @@ -1,10 +1,7 @@ -FROM node:18 +FROM node:16 COPY . /app WORKDIR /app -RUN npm install -WORKDIR /app/dashboard -RUN npm install +RUN npm run install-full RUN npm run build -WORKDIR /app EXPOSE 8080 -CMD ["npm", "start"] \ No newline at end of file +CMD ["npm", "start"] diff --git a/package.json b/package.json index 4ed3d39..f93af83 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,11 @@ "license": "MIT", "scripts": { "test": "mocha ./test/index.js", - "start": "node ./src/index.js" + "start": "node ./src/index.js", + "build": "cd ./dashboard && yarn build ", + "install-full": "yarn && cd ./dashboard && yarn", + "setup":"yarn install-full && yarn build && yarn start", + "dev":"yarn build && yarn start" }, "dependencies": { "body-parser": "^1.19.2", diff --git a/src/helper/states.js b/src/helper/states.js index ac1da05..4cfe59a 100644 --- a/src/helper/states.js +++ b/src/helper/states.js @@ -1,4 +1,8 @@ -let JssteState = { statusMSG: "could not started JSSTE", status: 1, pageStatus:[] }; +let JssteState = { + statusMSG: "could not started JSSTE", + status: 1, + pageStatus: [], +}; let WebsrvState = { port: undefined, status: undefined }; module.exports = { diff --git a/src/index.js b/src/index.js index 95f0143..ced4c5c 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,7 @@ let os = require("os"); websrv.slisten((host, port) => { console.log("Server started on http://" + host + ":" + port); websrv.dashboardExist(()=>{ - open("http://" + host + ":" + port + "/dashboard"); + // open("http://" + host + ":" + port + "/dashboard"); }) }); diff --git a/src/jsste/index.js b/src/jsste/index.js index dae9c56..c13d424 100644 --- a/src/jsste/index.js +++ b/src/jsste/index.js @@ -1,17 +1,45 @@ +let conf = require("../helper/conf")(); +let { JssteState } = require("../helper/states"); +const fs = require("fs"); app = {}; app.instance = {}; app.static = function (constante, cb, instance = app.instance) {}; app.specialvars = function (variable_regex, cb, instance = app.instance) {}; +JssteState.status = 2; +JssteState.statusMSG = "JSSTE is only loaded"; +JssteState.pageStatus.push({ + page: "this", + msg: "JSSTE is only loaded", + status: 2, +}); + /** * * @param {*} pagecode * @param {*} templatecode */ app.render = function (pagecode, templatecode) { + JssteState.status = 0; + if (pagecode == undefined || pagecode == null) { + console.log("pagecode is undefined or null"); + JssteState.status = 1; + JssteState.statusMSG = "pagecode is undefined or null"; + } + if (typeof pagecode == "string") { + pagecode = JSON.parse(pagecode); + } + if (templatecode == undefined || templatecode == null) { + templatecode = fs.readFileSync(pagecode["_template_"]); + JssteState.status = 3; + JssteState.statusMSG = "templatecode is undefined or null"; + } - + templatecode = templatecode.replaceAll("<[VAR]>", "Hallo"); + + console.log("render " + JSON.stringify(templatecode)); + return templatecode; }; module.exports = app; diff --git a/src/websrv/index.js b/src/websrv/index.js index 5c611c2..c07a1cb 100644 --- a/src/websrv/index.js +++ b/src/websrv/index.js @@ -1,4 +1,5 @@ const res = require("express/lib/response"); +const { JssteState } = require("../helper/states"); module.exports = function (conf) { const websrvConfig = conf.webserver; @@ -10,6 +11,7 @@ module.exports = function (conf) { const fs = require("fs"); const path = require("path"); let State = require("../helper/states").WebsrvState; + let jsste = require("../jsste"); const internalRouter = require("./routes/internalRouter"); @@ -30,6 +32,7 @@ module.exports = function (conf) { `${req.method} ${req.baseUrl + req.path} ${res.statusCode} | ${time}` ); }); + // jsste.render("{}", ""); next(); }); app.use(cors()); @@ -54,6 +57,44 @@ module.exports = function (conf) { ); }); + let folders = { + jsste: "pages", + css: "styles", + }; + + function getFolderFromFileEnding(filename) { + let regex_isAnDotfile = /\w+\.[a-z]*[A-Z]*/; + if (regex_isAnDotfile.test(filename)) { + let ending = filename.split(".").pop(); + // let ending = path.extname(filename).replace(".", ""); + return "" + folders[ending]; + } + return "" + folders.jsste; + } + + + function defaultUse(req, res, next) { + let regex_isAnDotfile = /\w+\.[a-z]*[A-Z]*/; + let filePath = path.join( + __dirname.toString(), + getFolderFromFileEnding(req.url), + req.url.toString() + ); + if (regex_isAnDotfile.test(req.url) && !filePath.endsWith(".jsste")) { + res.sendFile(filePath); + } else if (fs.existsSync(filePath + ".jsste")) { + let content = jsste.renderFile(filePath + ".jsste"); + res.send(content); + } else if (fs.lstatSync(filePath).isDirectory()) { + let content = jsste.renderFile( + decode(path.join(filePath, "index.jsste")) + ); + res.send(content); + } else next(); + } + + app.get("/*", defaultUse); + app.slisten = function (cb) { app.ServerInstance = app .listen(websrvConfig.port, websrvConfig.host, () => { diff --git a/src/websrv/routes/internalRouter.js b/src/websrv/routes/internalRouter.js index 7bf7c68..09fb18c 100644 --- a/src/websrv/routes/internalRouter.js +++ b/src/websrv/routes/internalRouter.js @@ -9,17 +9,21 @@ router.get("/conf", (req, res) => { res.json(conf); }); +router.get("/jsste/status/obj/:obj", (req, res) => { + res.json(States.JssteState[req.params.obj]); +}); router.get("/jsste/status", (req, res) => { res.json(States.JssteState); }); router.get("/websrv/status", (req, res) => { res.json(States.WebsrvState); }); -router.get("/shortlogs", (req, res) => { - res.json(logger.getCurrentlog(32)); + +router.get("/logs/length/:leng", (req, res) => { + res.json(logger.getCurrentlog(req.params.leng)); }); router.get("/logs", (req, res) => { - res.json(logger.getCurrentlog(64)); + res.json(logger.getCurrentlog(-1)); }); module.exports = router;