mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-03 00:06:46 +01:00
Add:Env variable setting to allow CORS
This commit is contained in:
parent
ab2026ecea
commit
69833db819
1
index.js
1
index.js
@ -4,7 +4,6 @@ global.appRoot = __dirname
|
|||||||
const isDev = process.env.NODE_ENV !== 'production'
|
const isDev = process.env.NODE_ENV !== 'production'
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
const devEnv = require('./dev').config
|
const devEnv = require('./dev').config
|
||||||
process.env.NODE_ENV = 'development'
|
|
||||||
if (devEnv.Port) process.env.PORT = devEnv.Port
|
if (devEnv.Port) process.env.PORT = devEnv.Port
|
||||||
if (devEnv.ConfigPath) process.env.CONFIG_PATH = devEnv.ConfigPath
|
if (devEnv.ConfigPath) process.env.CONFIG_PATH = devEnv.ConfigPath
|
||||||
if (devEnv.MetadataPath) process.env.METADATA_PATH = devEnv.MetadataPath
|
if (devEnv.MetadataPath) process.env.METADATA_PATH = devEnv.MetadataPath
|
||||||
|
@ -7,6 +7,7 @@ class Logger {
|
|||||||
this.logManager = null
|
this.logManager = null
|
||||||
|
|
||||||
this.isDev = process.env.NODE_ENV !== 'production'
|
this.isDev = process.env.NODE_ENV !== 'production'
|
||||||
|
|
||||||
this.logLevel = !this.isDev ? LogLevel.INFO : LogLevel.TRACE
|
this.logLevel = !this.isDev ? LogLevel.INFO : LogLevel.TRACE
|
||||||
this.socketListeners = []
|
this.socketListeners = []
|
||||||
}
|
}
|
||||||
@ -49,7 +50,7 @@ class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addSocketListener(socket, level) {
|
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) {
|
if (index >= 0) {
|
||||||
this.socketListeners.splice(index, 1, {
|
this.socketListeners.splice(index, 1, {
|
||||||
id: socket.id,
|
id: socket.id,
|
||||||
@ -66,7 +67,7 @@ class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
removeSocketListener(socketId) {
|
removeSocketListener(socketId) {
|
||||||
this.socketListeners = this.socketListeners.filter(s => s.id !== socketId)
|
this.socketListeners = this.socketListeners.filter((s) => s.id !== socketId)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,6 +50,7 @@ class Server {
|
|||||||
global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH))
|
global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH))
|
||||||
global.RouterBasePath = ROUTER_BASE_PATH
|
global.RouterBasePath = ROUTER_BASE_PATH
|
||||||
global.XAccel = process.env.USE_X_ACCEL
|
global.XAccel = process.env.USE_X_ACCEL
|
||||||
|
global.AllowCors = process.env.ALLOW_CORS === '1'
|
||||||
|
|
||||||
if (!fs.pathExistsSync(global.ConfigPath)) {
|
if (!fs.pathExistsSync(global.ConfigPath)) {
|
||||||
fs.mkdirSync(global.ConfigPath)
|
fs.mkdirSync(global.ConfigPath)
|
||||||
@ -182,11 +183,12 @@ class Server {
|
|||||||
* @see https://ionicframework.com/docs/troubleshooting/cors
|
* @see https://ionicframework.com/docs/troubleshooting/cors
|
||||||
*
|
*
|
||||||
* Running in development allows cors to allow testing the mobile apps in the browser
|
* 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) => {
|
app.use((req, res, next) => {
|
||||||
if (Logger.isDev || req.path.match(/\/api\/items\/([a-z0-9-]{36})\/(ebook|cover)(\/[0-9]+)?/)) {
|
if (Logger.isDev || req.path.match(/\/api\/items\/([a-z0-9-]{36})\/(ebook|cover)(\/[0-9]+)?/)) {
|
||||||
const allowedOrigins = ['capacitor://localhost', 'http://localhost']
|
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-Origin', req.get('origin'))
|
||||||
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS')
|
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS')
|
||||||
res.header('Access-Control-Allow-Headers', '*')
|
res.header('Access-Control-Allow-Headers', '*')
|
||||||
|
Loading…
Reference in New Issue
Block a user