remove open | define JssteStates | add simple jsste routing

This commit is contained in:
lucaspalomodevelop 2022-06-23 18:45:15 +02:00
parent 60dd9404b4
commit 9c291b4590
8 changed files with 94 additions and 16 deletions

View File

@ -2,7 +2,7 @@ version: '3'
services:
jsste:
build: .
volumes:
- ./:/app
ports:
- "8080:8080"
- "8080:8080"
volumes:
- .:/app

View File

@ -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"]
CMD ["npm", "start"]

View File

@ -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",

View File

@ -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 = {

View File

@ -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");
})
});

View File

@ -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;

View File

@ -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, () => {

View File

@ -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;