apxtrib/apxtrib.js

146 lines
6.6 KiB
JavaScript
Raw Normal View History

2023-01-22 09:53:09 +00:00
const fs = require( 'fs-extra' );
const bodyParser = require( 'body-parser' );
const cors = require( 'cors' );
const express = require( 'express' );
/*******************************************
2023-03-27 05:52:21 +00:00
SEE https://gitea.ndda.fr/apxtrib/apxtrib/wiki/Devrules
To have a quick understanding and convention before doing deeply in source code
2023-01-22 09:53:09 +00:00
*********************************************/
2023-03-27 05:52:21 +00:00
// to make absolute path with `${__base}relativepath`
global.__base = __dirname +'/';
2023-01-22 09:53:09 +00:00
// check setup
if( !fs.existsSync( '/etc/nginx/nginx.conf' ) ) {
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.' );
process.exit();
}
2023-04-13 05:46:35 +00:00
if( !fs.existsSync( './nationchains/tribes/index/conf.json' ) ) {
// this is a first installation of a dev or prod machine
const readline = require( 'readline' );
const rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} );
const townconf = fs.readJsonSync( './nationchains/www/setup/townconf.json') ;
townconf.sudoerUser=process.env.USER;
townconf.dirname=__dirname;
townconf.nginx.include.push(`${__dirname}/nationchains/**/nginx_*.conf`);
townconf.nginx.logs=`${__dirname}/nationchains/logs/nginx`;
townconf.nginx.website='setup';
townconf.nginx.fswww='nationchains/';//for a local tribe nationchains/tribes/tribeid
townconf.nginx.tribeid="town"
console.log(townconf)
rl.question( 'This is the first install from ./nationchains/www/setup/townconf.json used, this will change your nginx config (/etc/nginx.conf will be saved as /etc/nginxconf.saved) (Yes/no)?', function ( rep1 ) {
let quest = `This is a production install, please check that ${townconf.townName}.${townconf.nationName}.${townconf.dns} IP is well redirect to tour server`;
if( rep1 !== "Yes" ) process.exit( 0 );
//console.log(process.env)
// saved and change nginx conf
const mustache=require('Mustache');
fs.moveSync("/etc/nginx/nginx.conf","/etc/nginx/nginxconf.saved");
const tplnginxconf=fs.readFileSync("./nationchains/www/setup/nginx/nginx.conf.mustache","utf8");
fs.outputFileSync("/etc/nginx/nginx.conftest",mustache.render(tplnginxconf, townconf),"utf8")
const tplnginxwww=fs.readFileSync("./nationchains/www/setup/nginx/modelwebsiteconf.mustache","utf8");
fs.outputFileSync(`./${townconf.nginx.fswww}www/nginx_${townconf.nginx.website}.conf`,mustache.render(tplnginxwww, townconf),"utf8")
//restart nginx
//fs.outputJsonSync('./nationchains/tribes/index/conf.json',setupconf);
console.log( `\x1b[42m#########################################################################\x1b[0m\n\x1b[42mWellcome into apxtrib, init your town and first tribe by 'yarn setup'. \x1b[0m \n\x1b[42mThen 'yarn dev' or 'yarn startpm2' or 'yarn unittest'. Check README's project to learn more.\x1b[0m\n\x1b[42m#########################################################################\x1b[0m` );
2023-01-22 09:53:09 +00:00
process.exit();
}
2023-04-13 05:46:35 +00:00
const config = require( './nationchains/tribes/index/conf.json' );
2023-03-27 05:52:21 +00:00
// Tribes allow to get local apxtrib instance context
2023-01-22 09:53:09 +00:00
// dataclient .tribeids [] .DOMs [] .routes (plugins {url:name route:path}) .appname {tribeid:[website]}
2023-04-13 05:46:35 +00:00
const dataclient = require( './app/models/Tribes' )
2023-01-22 09:53:09 +00:00
.init();
2023-03-27 05:52:21 +00:00
console.log( 'allowed DOMs to access to this apxtrib server: ', dataclient.DOMs )
2023-01-22 09:53:09 +00:00
const app = express();
2023-04-13 05:46:35 +00:00
2023-03-27 05:52:21 +00:00
Object.keys(config.appset).forEach(p=>{
app.set(p,config.appset[p])
})
2023-01-22 09:53:09 +00:00
app.set( 'trust proxy', true );
// To set depending of data form or get size to send
app.use( bodyParser.urlencoded( config.bodyparse.urlencoded ) );
// To set depending of post put json data size to send
app.use( express.json() )
app.use( bodyParser.json( config.bodyparse.json ) );
app.locals.tribeids = dataclient.tribeids;
console.log( 'app.locals.tribeids', app.locals.tribeids );
// User token authentification and user init user search
const datauser = require( './models/Pagans' )
.init( dataclient.tribeids );
app.locals.tokens = datauser.tokens;
console.log( 'app.locals.tokens key ', Object.keys( app.locals.tokens ) )
// Cors management
const corsOptions = {
origin: ( origin, callback ) => {
if( origin === undefined ) {
callback( null, true );
} else if( origin.indexOf( 'chrome-extension' ) > -1 ) {
callback( null, true );
} else {
//console.log( 'origin', origin )
//marchais avant modif eslint const rematch = ( /^https?\:\/\/(.*)\:.*/g ).exec( origin )
const rematch = ( /^https?:\/\/(.*):.*/g )
.exec( origin )
//console.log( rematch )
let tmp = origin.replace( /http.?:\/\//g, '' )
.split( '.' )
if( rematch && rematch.length > 1 ) tmp = rematch[ 1 ].split( '.' );
//console.log( 'tmp', tmp )
let dom = tmp[ tmp.length - 1 ];
if( tmp.length > 1 ) {
dom = `${tmp[tmp.length-2]}.${tmp[tmp.length-1]}`
}
console.log( `origin: ${origin}, dom:${dom}, CORS allowed? : ${dataclient.DOMs.includes( dom )}` );
if( dataclient.DOMs.includes( dom ) ) {
callback( null, true )
} else {
console.log( `Origin is not allowed by CORS` );
callback( new Error( 'Not allowed by CORS' ) );
}
}
},
exposedHeaders: Object.keys( config.exposedHeaders )
};
// CORS
app.use( cors( corsOptions ) );
// Static Routes
app.use( express.static( `${__dirname}/tribes/${config.mayorId}/www/cdn/public`, {
dotfiles: 'allow'
} ) );
2023-03-27 05:52:21 +00:00
//Allow to public access a space dev delivered by apxtrib
2023-01-22 09:53:09 +00:00
// this is just a static open route for dev purpose,
// for production, we'll use a nginx static set to /www/app/appname
2023-03-27 05:52:21 +00:00
/*console.log( `${config.dnsapxtrib}/space/tribeid/website`, dataclient.appname );
2023-01-22 09:53:09 +00:00
Object.keys( dataclient.appname )
.forEach( cid => {
dataclient.appname[ cid ].forEach( website => {
app.use( `/space/${cid}/${website}`, express.static( `${config.tribes}/${cid}/spacedev/${website}` ) );
} )
} );
*/
// Routers add any routes from /routes and /plugins
2023-03-27 05:52:21 +00:00
console.log( 'Routes available on this apxtrib instance' );
2023-01-22 09:53:09 +00:00
console.log( dataclient.routes );
2023-03-27 05:52:21 +00:00
// prefix only use for dev purpose in production a proxy nginx redirect /app/ to node apxtrib
2023-01-22 09:53:09 +00:00
dataclient.routes.forEach( r => {
try {
app.use( r.url, require( r.route ) );
} catch ( err ) {
console.log( `\x1b[31m!!! WARNING issue with route ${r.route} from ${r.url} check err if route is key then solve err, if not just be aware that this route won't work on your server. If you are not the maintainer and no turn around please contact the email maintainer.\x1b[0m` )
console.log( 'raise err-:', err );
}
} )
app.listen( config.porthttp, () => {
2023-03-27 05:52:21 +00:00
console.log( `check in your browser that api works http://${config.dnsapxtrib}:${config.porthttp}` );
2023-01-22 09:53:09 +00:00
} );
console.log( "\x1b[42m\x1b[37m", "Made with love for people's freedom, enjoy !!!", "\x1b[0m" );