Improve checks

This commit is contained in:
François Michaud, Deepnox 2023-02-21 23:07:14 +01:00
parent 9acd329c3d
commit ac13aee9b4

View File

@ -1,10 +1,11 @@
const fs = require('fs-extra') const fs = require('fs')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const cors = require('cors') const cors = require('cors')
const express = require('express') const express = require('express')
const logger = require('./core/logger') const logger = require('./core/logger')
/******************************************* /*******************************************
SEE http://gitlab.ndda.fr/philc/apixtribe/-/wikis/HOWTOoverview SEE http://gitlab.ndda.fr/philc/apixtribe/-/wikis/HOWTOoverview
@ -12,22 +13,30 @@ const logger = require('./core/logger')
*********************************************/ *********************************************/
// check setup* // check setup*
/**
if( !fs.existsSync( '/etc/nginx/nginx.conf' ) ) { fs.stat('/etc/nginx/nginx.conf', (err, stats) => {
logger.info( '\x1b[31m Check documentation, nginx have to be installed on this server first, no /etc/nginx/nginx.conf available, install then rerun yarn command.' ); if (err === null) {
process.exit(); logger.debug(`File exists.`)
} } else if (err.code === 'ENOENT') {
*/ logger.error('Check documentation, nginx have to be installed on this server first, no /etc/nginx/nginx.conf available, install then rerun yarn command.')
if (!fs.existsSync('./tribes/townconf.js')) { process.exit(1)
logger.info('\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') } else {
process.exit() logger.error(`Exception: ` + err)
} process.exit(1)
}
})
// config.js exist in any case from Setup.checkinit(); // config.js exist in any case from Setup.checkinit();
const config = require('./tribes/townconf.js') try {
const config = require('./tribes/townconf.js')
} catch (err) {
logger.error('Welcome into apixtribe, you need to init your town by "yarn setup" the first time . Check README\'s project to learn more. more.')
process.exit(1)
}
// Tribes allow to get local apixtribe instance context // Tribes allow to get local apixtribe instance context
// dataclient .tribeids [] .DOMs [] .routes (plugins {url:name route:path}) .appname {tribeid:[website]} // dataclient .tribeids [] .DOMs [] .routes (plugins {url:name route:path}) .appname {tribeid:[website]}
const dataclient = require('./models/Tribes') const dataclient = require('./models/Tribes')
.init() .init()
logger.info('allowed DOMs to access to this apixtribe server: ', dataclient.DOMs) logger.info('allowed DOMs to access to this apixtribe server: ', dataclient.DOMs)
const app = express() const app = express()
app.set('trust proxy', true) app.set('trust proxy', true)
@ -40,47 +49,47 @@ app.locals.tribeids = dataclient.tribeids
logger.info('app.locals.tribeids', app.locals.tribeids) logger.info('app.locals.tribeids', app.locals.tribeids)
// User token authentification and user init user search // User token authentification and user init user search
const datauser = require('./models/Pagans') const datauser = require('./models/Pagans')
.init(dataclient.tribeids) .init(dataclient.tribeids)
app.locals.tokens = datauser.tokens app.locals.tokens = datauser.tokens
logger.info('app.locals.tokens key ', Object.keys(app.locals.tokens)) logger.info('app.locals.tokens key ', Object.keys(app.locals.tokens))
// Cors management // Cors management
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (origin === undefined) { if (origin === undefined) {
callback(null, true) callback(null, true)
} else if (origin.indexOf('chrome-extension') > -1) { } else if (origin.indexOf('chrome-extension') > -1) {
callback(null, true) callback(null, true)
} else { } else {
// logger.info( 'origin', origin ) // logger.info( 'origin', origin )
// marchais avant modif eslint const rematch = ( /^https?\:\/\/(.*)\:.*/g ).exec( origin ) // marchais avant modif eslint const rematch = ( /^https?\:\/\/(.*)\:.*/g ).exec( origin )
const rematch = (/^https?:\/\/(.*):.*/g) const rematch = (/^https?:\/\/(.*):.*/g)
.exec(origin) .exec(origin)
// logger.info( rematch ) // logger.info( rematch )
let tmp = origin.replace(/http.?:\/\//g, '') let tmp = origin.replace(/http.?:\/\//g, '')
.split('.') .split('.')
if (rematch && rematch.length > 1) tmp = rematch[1].split('.') if (rematch && rematch.length > 1) tmp = rematch[1].split('.')
// logger.info( 'tmp', tmp ) // logger.info( 'tmp', tmp )
let dom = tmp[tmp.length - 1] let dom = tmp[tmp.length - 1]
if (tmp.length > 1) { if (tmp.length > 1) {
dom = `${tmp[tmp.length - 2]}.${tmp[tmp.length - 1]}` dom = `${tmp[tmp.length - 2]}.${tmp[tmp.length - 1]}`
} }
logger.info(`origin: ${origin}, dom:${dom}, CORS allowed? : ${dataclient.DOMs.includes(dom)}`) logger.info(`origin: ${origin}, dom:${dom}, CORS allowed? : ${dataclient.DOMs.includes(dom)}`)
if (dataclient.DOMs.includes(dom)) { if (dataclient.DOMs.includes(dom)) {
callback(null, true) callback(null, true)
} else { } else {
logger.info('Origin is not allowed by CORS') logger.info('Origin is not allowed by CORS')
callback(new Error('Not allowed by CORS')) callback(new Error('Not allowed by CORS'))
} }
} }
}, },
exposedHeaders: Object.keys(config.exposedHeaders) exposedHeaders: Object.keys(config.exposedHeaders)
} }
// CORS // CORS
app.use(cors(corsOptions)) app.use(cors(corsOptions))
// Static Routes // Static Routes
app.use(express.static(`${__dirname}/tribes/${config.mayorId}/www/cdn/public`, { app.use(express.static(`${__dirname}/tribes/${config.mayorId}/www/cdn/public`, {
dotfiles: 'allow' dotfiles: 'allow'
})) }))
// Allow to public access a space dev delivered by apixtribe // Allow to public access a space dev delivered by apixtribe
@ -100,16 +109,16 @@ logger.info(dataclient.routes)
// prefix only use for dev purpose in production a proxy nginx redirect /app/ to node apixtribe // prefix only use for dev purpose in production a proxy nginx redirect /app/ to node apixtribe
dataclient.routes.forEach(r => { dataclient.routes.forEach(r => {
try { try {
app.use(r.url, require(r.route)) app.use(r.url, require(r.route))
} catch (err) { } catch (err) {
logger.error(`\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`) logger.error(`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.`)
logger.error('raise err-:', err) logger.error('raise err-:', err)
} }
}) })
// Listen web server from config profil (dev prod, other) // Listen web server from config profil (dev prod, other)
app.listen(config.porthttp, () => { app.listen(config.porthttp, () => {
logger.info(`check in your browser that api works http://${config.dnsapixtribe}:${config.porthttp}`) logger.info(`check in your browser that api works http://${config.dnsapixtribe}:${config.porthttp}`)
}) })
/* httpServer.setTimeout( config.settimeout ); /* httpServer.setTimeout( config.settimeout );
if( config.withssl === "YES" ) { if( config.withssl === "YES" ) {
@ -120,4 +129,4 @@ if( config.withssl === "YES" ) {
httpsServer.setTimeout( config.settimeout ); httpsServer.setTimeout( config.settimeout );
}; */ }; */
logger.info('\x1b[42m\x1b[37m', "Made with love for people's freedom, enjoy !!!", '\x1b[0m') logger.info("Made with love for people's freedom, enjoy !!!")