apxtrib/apxtrib.js
2023-01-22 10:53:09 +01:00

120 lines
5.0 KiB
JavaScript
Executable File

const fs = require( 'fs-extra' );
const bodyParser = require( 'body-parser' );
const cors = require( 'cors' );
const express = require( 'express' );
/*******************************************
SEE http://gitlab.ndda.fr/philc/apixtribe/-/wikis/HOWTOoverview
To have a quick understanding before doing deeply in source code
*********************************************/
// 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();
}
if( !fs.existsSync( './tribes/townconf.js' ) ) {
console.log( `\x1b[42m####################################\nWellcome into apixtribe, you need to init your town by "yarn setup" the first time . \nCheck README's project to learn more. more.\n #####################################\x1b[0m` );
process.exit();
}
// config.js exist in any case from Setup.checkinit();
const config = require( './tribes/townconf.js' );
// Tribes allow to get local apixtribe instance context
// dataclient .tribeids [] .DOMs [] .routes (plugins {url:name route:path}) .appname {tribeid:[website]}
const dataclient = require( './models/Tribes' )
.init();
console.log( 'allowed DOMs to access to this apixtribe server: ', dataclient.DOMs )
const app = express();
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'
} ) );
//Allow to public access a space dev delivered by apixtribe
// this is just a static open route for dev purpose,
// for production, we'll use a nginx static set to /www/app/appname
/*console.log( `${config.dnsapixtribe}/space/tribeid/website`, dataclient.appname );
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
console.log( 'Routes available on this apixtribe instance' );
console.log( dataclient.routes );
// prefix only use for dev purpose in production a proxy nginx redirect /app/ to node apixtribe
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 );
}
} )
// Listen web server from config profil (dev prod, other)
app.listen( config.porthttp, () => {
console.log( `check in your browser that api works http://${config.dnsapixtribe}:${config.porthttp}` );
} );
/*httpServer.setTimeout( config.settimeout );
if( config.withssl == "YES" ) {
const httpsServer = https.createServer( config.SSLCredentials, app );
httpsServer.listen( config.port.https, () => {
console.log( `check in your browser that api works https://${config.dnsapixtribe}:${config.port.https}` );
} );
httpsServer.setTimeout( config.settimeout );
};*/
console.log( "\x1b[42m\x1b[37m", "Made with love for people's freedom, enjoy !!!", "\x1b[0m" );