Skip to content

Commit

Permalink
Add:Env variable setting to allow CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed May 19, 2024
1 parent ab2026e commit 69833db
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions server/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

/**
Expand Down Expand Up @@ -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})`)
Expand All @@ -148,4 +149,4 @@ class Logger {
this.handleLog(LogLevel.NOTE, args, this.source)
}
}
module.exports = new Logger()
module.exports = new Logger()
4 changes: 3 additions & 1 deletion server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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', '*')
Expand Down

0 comments on commit 69833db

Please sign in to comment.