Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const winston = require("winston")
const DailyRotateFile = require("winston-daily-rotate-file")

const filePath = process.argv[2] || "./logs"
const logLevel = process.env.LOG_LEVEL || "info"

const logger = winston.createLogger({
level: process.env.LOG_LEVEL || "info",
level: logLevel,
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.printf(({ timestamp, level, message }) => {
Expand Down
34 changes: 23 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { loadConfig } = require("./configBuilder")

// Load configuration
const config = process.env.NODE_ENV !== "test" ? loadConfig() : ""
const debug = logger.isLevelEnabled("debug")
const app = express()
app.use(express.json())

Expand All @@ -27,7 +28,7 @@ const isObject = (value) => typeof value === "object" && value !== null

const isObjectArray = (value) => Array.isArray(value) && value.some((item) => isObject(item))

const formatLogEntry = (entry) => {
const formatDebugLogEntry = (entry) => {
if (Array.isArray(entry)) {
return entry
.map((item) => (isObject(item) && item.name ? item.name : isObject(item) ? JSON.stringify(item) : item))
Expand All @@ -44,9 +45,9 @@ const formatLogEntry = (entry) => {
return entry
}

const buildLogMessage = (message, details = {}) => {
const buildDebugLogMessage = (message, details = {}) => {
const formattedDetails = Object.entries(details)
.map(([key, value]) => `${key}: ${formatLogEntry(value)}`)
.map(([key, value]) => `${key}: ${formatDebugLogEntry(value)}`)
.join("\n")

return `${message}\n${formattedDetails}`
Expand Down Expand Up @@ -99,9 +100,9 @@ const findMatchingInstances = (webhook, data, filters) => {
return false
}

if (logger.isLevelEnabled("debug")) {
if (debug) {
logger.debug(
buildLogMessage("Filter check:", {
buildDebugLogMessage("Filter check:", {
Field: key,
"Filter value": value,
"Request value": requestValue,
Expand Down Expand Up @@ -164,11 +165,8 @@ const sendToInstances = async (instances, requestId, data) => {
postData.serverId = instance.server_id
if (instance.quality_profile_id) postData.profileId = instance.quality_profile_id

logger.debug({
message: "Sending configuration to instance",
instance: item,
postData: postData,
})
if (debug)
logger.debug(buildDebugLogMessage("Sending configuration to instance:", { instance: item, postData }))

await applyConfig(requestId, postData)
logger.info(`Configuration applied for request ID ${requestId} on instance "${item}"`)
Expand Down Expand Up @@ -203,6 +201,20 @@ app.post("/webhook", async (req, res) => {
logger.info(
`Received request ID ${request.request_id} for ${media.media_type} "${data?.originalTitle || data?.originalName}"`
)
if (debug) {
const cleanMetadata = Object.fromEntries(
Object.entries(data).filter(
([key]) => !["credits", "relatedVideos", "networks", "watchProviders"].includes(key)
)
)
logger.debug(
buildDebugLogMessage("Request details:", {
webhook: JSON.stringify(req.body, null, 2),
metadata: JSON.stringify(cleanMetadata, null, 2),
})
)
}

const instances = findMatchingInstances(req.body, data, config.filters)
const postData = getPostData(req.body)
if (instances) await sendToInstances(instances, request.request_id, postData)
Expand All @@ -214,10 +226,10 @@ app.post("/webhook", async (req, res) => {
}
})

// Server initialization
const PORT = process.env.PORT || 8481
const server = app.listen(PORT, () => {
logger.info(`Server is running on port ${PORT}`)
if (debug) logger.debug("Debug logs enabled")
})

module.exports = { findMatchingInstances, server }
Loading