1
0
forked from apxtri/apxtri

reindex auto on start

This commit is contained in:
philc 2024-08-07 08:02:47 +02:00
parent 57db217ee4
commit 53fad87b25
2 changed files with 34 additions and 26 deletions

View File

@ -7,13 +7,14 @@ const path = require("path");
const cors = require("cors"); const cors = require("cors");
const express = require("express"); const express = require("express");
const process = require("process"); const process = require("process");
const Odmdb = require("./models/Odmdb.js");
/******************************************* /*******************************************
SEE README.md to have a quick start SEE README.md to have a quick start
********************************************/ ********************************************/
if (!fs.existsSync("/etc/nginx/nginx.conf")) { if (!fs.existsSync("/etc/nginx/nginx.conf")) {
setupdone=false; setupdone = false;
console.log( console.log(
"\x1b[31m Check documentation, nginx have to be installed on this server first, no /etc/nginx/nginx.conf available, install then rerun yarn command."); "\x1b[31m Check documentation, nginx have to be installed on this server first, no /etc/nginx/nginx.conf available, install then rerun yarn command.");
} }
@ -35,32 +36,39 @@ let tribeIds = Object.keys(tribelist);
// context is store in /itm/tribename.json ={contexte:{routes:[],models:[{model:,tplstringslg:[]}]} // context is store in /itm/tribename.json ={contexte:{routes:[],models:[{model:,tplstringslg:[]}]}
// routes={url,route} check how to add plugin tribe route later // routes={url,route} check how to add plugin tribe route later
// keep only the 2 last part (.) of domain name to validate cors with it (generic domain) // keep only the 2 last part (.) of domain name to validate cors with it (generic domain)
let routes=[] let routes = []
tribeIds.forEach((t) => { tribeIds.forEach((t) => {
tribelist[t].dns.forEach((d) => { tribelist[t].dns.forEach((d) => {
const dm = d.split(".").slice(-2).join("."); const dm = d.split(".").slice(-2).join(".");
if (!doms.includes(dm)) doms.push(dm); if (!doms.includes(dm)) doms.push(dm);
//reindex database
if (t == "smatchit") {
glob.sync(`../../${t}/objects/*`).forEach(o => {
console.log(t, o)
Odmdb.runidx(o)
})
}
}); });
const context={}; const context = {};
const pathtr=path.resolve(`../../${t}`); const pathtr = path.resolve(`../../${t}`);
context.routes=[] context.routes = []
tribroutes=glob.sync(`${pathtr}/ap*/routes/*.js`).map(f=>{ tribroutes = glob.sync(`${pathtr}/ap*/routes/*.js`).map(f => {
const rt=`/${t}/${path.basename(f, ".js")}` const rt = `/${t}/${path.basename(f, ".js")}`
context.routes.push(rt) context.routes.push(rt)
return { url: rt, route: f }; return { url: rt, route: f };
}); });
context.models=glob.sync(`${pathtr}/ap*/models/*.js`).map(f=>{ context.models = glob.sync(`${pathtr}/ap*/models/*.js`).map(f => {
const modname=`${path.basename(f, ".js")}` const modname = `${path.basename(f, ".js")}`
return { return {
model: modname, model: modname,
tplstrings:glob.sync(`${pathtr}/objects/tplstrings/${modname}_*.json`).map(l=>path.basename(l,'.json').split("_")[1] ) tplstrings: glob.sync(`${pathtr}/objects/tplstrings/${modname}_*.json`).map(l => path.basename(l, '.json').split("_")[1])
} }
}) })
const conft=`../../itm/${t}.json` const conft = `../../itm/${t}.json`
const ctx=fs.readJsonSync(conft) const ctx = fs.readJsonSync(conft)
ctx.context=context ctx.context = context
fs.outputJSONSync(conft,ctx,{spaces:2}); fs.outputJSONSync(conft, ctx, { spaces: 2 });
routes=routes.concat(tribroutes); routes = routes.concat(tribroutes);
}); });
const app = express(); const app = express();
// load express parameter from conf // load express parameter from conf
@ -70,11 +78,11 @@ Object.keys(conf.api.appset).forEach((p) => {
// To set depending of data form or get size to send // To set depending of data form or get size to send
app.use(bodyParser.urlencoded(conf.api.bodyparse.urlencoded)); app.use(bodyParser.urlencoded(conf.api.bodyparse.urlencoded));
// To set depending of post put json data size to send // To set depending of post put json data size to send
app.use(express.json({limit:'10mb',type:'application/json',rawBody:true})); app.use(express.json({ limit: '10mb', type: 'application/json', rawBody: true }));
app.use(bodyParser.json(conf.api.bodyparse.json)); app.use(bodyParser.json(conf.api.bodyparse.json));
app.disable("x-powered-by"); // for security app.disable("x-powered-by"); // for security
app.locals.tribeids = tribeIds; app.locals.tribeids = tribeIds;
if (log){ if (log) {
console.log( console.log(
currentmod, currentmod,
" Allowed DOMs to access to this apxtri server:", " Allowed DOMs to access to this apxtri server:",
@ -82,7 +90,7 @@ if (log){
); );
console.log(currentmod, " app.locals.tribeids", app.locals.tribeids); console.log(currentmod, " app.locals.tribeids", app.locals.tribeids);
} }
// Cors management // Cors management
let originlst = "test"; let originlst = "test";
doms.forEach((d) => { doms.forEach((d) => {
originlst += `|${d.replace(/\./g, "\\.")}`; originlst += `|${d.replace(/\./g, "\\.")}`;
@ -102,7 +110,7 @@ app.use((req, res, next) => {
"request origin:", "request origin:",
req.headers.origin, req.headers.origin,
"testcors:", "testcors:",
cor, "headers allowed: [", conf.api.exposedHeaders.join(','),"] match with reg:", regtxt cor, "headers allowed: [", conf.api.exposedHeaders.join(','), "] match with reg:", regtxt
); );
cors({ cors({
origin: cor, origin: cor,

View File

@ -900,7 +900,7 @@ Odmdb.runidx = (objectPathname, schema) => {
}); });
Object.keys(ventil).forEach((n) => { Object.keys(ventil).forEach((n) => {
//if (log) console.log(currentmod,`${objectPathname}/idx/${ventil[n].name}.json`) //if (log) console.log(currentmod,`${objectPathname}/idx/${ventil[n].name}.json`)
fs.outputJSON( fs.outputJSONSync(
`${objectPathname}/idx/${ventil[n].name}.json`, `${objectPathname}/idx/${ventil[n].name}.json`,
ventil[n].data ventil[n].data
); );