Add:Env variable setting to allow CORS

This commit is contained in:
advplyr 2024-05-19 14:40:46 -05:00
parent ab2026ecea
commit 69833db819
3 changed files with 9 additions and 7 deletions

View File

@ -4,7 +4,6 @@ global.appRoot = __dirname
const isDev = process.env.NODE_ENV !== 'production'
if (isDev) {
const devEnv = require('./dev').config
process.env.NODE_ENV = 'development'
if (devEnv.Port) process.env.PORT = devEnv.Port
if (devEnv.ConfigPath) process.env.CONFIG_PATH = devEnv.ConfigPath
if (devEnv.MetadataPath) process.env.METADATA_PATH = devEnv.MetadataPath

View File

@ -7,6 +7,7 @@ class Logger {
this.logManager = null
this.isDev = process.env.NODE_ENV !== 'production'
this.logLevel = !this.isDev ? LogLevel.INFO : LogLevel.TRACE
this.socketListeners = []
}
@ -49,7 +50,7 @@ class Logger {
}
addSocketListener(socket, level) {
var index = this.socketListeners.findIndex(s => s.id === socket.id)
var index = this.socketListeners.findIndex((s) => s.id === socket.id)
if (index >= 0) {
this.socketListeners.splice(index, 1, {
id: socket.id,
@ -66,7 +67,7 @@ class Logger {
}
removeSocketListener(socketId) {
this.socketListeners = this.socketListeners.filter(s => s.id !== socketId)
this.socketListeners = this.socketListeners.filter((s) => s.id !== socketId)
}
/**
@ -135,8 +136,8 @@ class Logger {
/**
* Fatal errors are ones that exit the process
* Fatal logs are saved to crash_logs.txt
*
* @param {...any} args
*
* @param {...any} args
*/
fatal(...args) {
console.error(`[${this.timestamp}] FATAL:`, ...args, `(${this.source})`)
@ -148,4 +149,4 @@ class Logger {
this.handleLog(LogLevel.NOTE, args, this.source)
}
}
module.exports = new Logger()
module.exports = new Logger()

View File

@ -50,6 +50,7 @@ class Server {
global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH))
global.RouterBasePath = ROUTER_BASE_PATH
global.XAccel = process.env.USE_X_ACCEL
global.AllowCors = process.env.ALLOW_CORS === '1'
if (!fs.pathExistsSync(global.ConfigPath)) {
fs.mkdirSync(global.ConfigPath)
@ -182,11 +183,12 @@ class Server {
* @see https://ionicframework.com/docs/troubleshooting/cors
*
* Running in development allows cors to allow testing the mobile apps in the browser
* or env variable ALLOW_CORS = '1'
*/
app.use((req, res, next) => {
if (Logger.isDev || req.path.match(/\/api\/items\/([a-z0-9-]{36})\/(ebook|cover)(\/[0-9]+)?/)) {
const allowedOrigins = ['capacitor://localhost', 'http://localhost']
if (Logger.isDev || allowedOrigins.some((o) => o === req.get('origin'))) {
if (global.AllowCors || Logger.isDev || allowedOrigins.some((o) => o === req.get('origin'))) {
res.header('Access-Control-Allow-Origin', req.get('origin'))
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS')
res.header('Access-Control-Allow-Headers', '*')