Added debug mode

This commit is contained in:
dbroqua 2020-12-16 14:06:25 +01:00
parent d601f4bfab
commit 5476a9bb06
6 changed files with 1274 additions and 43 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules
local.sh
currentSongId
dist

9
app.js
View File

@ -4,7 +4,7 @@ import cookieParser from "cookie-parser";
import bodyParser from "body-parser";
import Pino from "express-pino-logger";
import cors from "cors";
import config from "./config";
import { port, trustProxy } from "./config";
import {
formatResponse,
formatResponseError,
@ -17,7 +17,7 @@ let currentSong = {};
const pino = new Pino({
prettyPrint: true,
});
pino.logger.info(`Server started on port ${config.port}`);
pino.logger.info(`Server started on port ${port}`);
const app = express();
@ -28,7 +28,7 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.set("trust proxy", config.trustProxy);
app.set("trust proxy", trustProxy);
// Get last saved song id on load
fs.readFile("./currentSongId", "utf8", (err, data) => {
@ -39,7 +39,6 @@ fs.readFile("./currentSongId", "utf8", (err, data) => {
app.post("/post", function (req, res, next) {
if (req.body && req.body.now_playing && req.body.now_playing.song) {
console.log("Received:", req.body.now_playing);
const newSongId = req.body.now_playing.song.id;
if (newSongId !== currentSongId) {
currentSongId = newSongId;
@ -49,7 +48,7 @@ app.post("/post", function (req, res, next) {
req.log.error(err);
}
});
publishMessage(req.body.now_playing, (err, response) => {
publishMessage(req.body.now_playing, req.log, (err, response) => {
if (err) {
req.log.error(err);
}

View File

@ -1,5 +1,6 @@
module.exports = {
port: process.env.PORT || 3000,
isDebug: process.env.DEBUG && process.env.DEBUG.toLowerCase() === "true",
trustProxy: process.env.TRUST_PROXY || "loopback",
mastodonToken: process.env.MASTODON_TOKEN,
mastondonApi: process.env.MASTODON_API_URL || "https://mamot.fr/api/v1/",

View File

@ -1,7 +1,7 @@
import fs from "fs";
import request from "request";
import Masto from "mastodon";
import config from "../config";
import { mastodonToken, mastondonApi, isDebug } from "../config";
/**
* Fonction permettant de formater une réponse API
@ -95,10 +95,11 @@ const _formatMessage = (values) => {
/**
* Fonction publiant un message (et média si attaché) sur Mastdon
* @param {Object} playing
* @param {Function} cb
* @param {Object} playing
* @param {Object} logger
* @param {Function} cb
*/
const _publishMessage = (playing, cb) => {
const _publishMessage = (playing, logger, cb) => {
const callback = (err, res) => {
if (err) {
cb(err);
@ -113,13 +114,15 @@ const _publishMessage = (playing, cb) => {
// Instanciation de Mastodon
const M = new Masto({
access_token: config.mastodonToken,
api_url: config.mastondonApi,
access_token: mastodonToken,
api_url: mastondonApi,
});
if (cover) {
_getMedia(cover, (err, dest) => {
if (err) {
if (isDebug) {
logger.info({ cover, status });
} else if (err) {
M.post("statuses", { status }, callback);
} else {
M.post("media", { file: fs.createReadStream(dest) })
@ -132,6 +135,8 @@ const _publishMessage = (playing, cb) => {
});
}
});
} else if (isDebug) {
logger.info(status);
} else {
M.post("statuses", { status }, callback);
}

View File

@ -7,7 +7,20 @@
"author": "dbroqua <dbroqua@mousur.org>",
"license": "MIT",
"private": false,
"scripts": {
"start": "node dist/start-server.js",
"start:local": "nodemon start-server.js",
"lint": "./node_modules/.bin/eslint . --fix",
"build": "babel . --ignore node_modules,dist -d dist --copy-files"
},
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/register": "^7.12.10",
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
@ -17,19 +30,9 @@
"pino-pretty": "^4.0.0",
"request": "^2.88.2"
},
"scripts": {
"start": "node start-server.js",
"start:local": "nodemon start-server.js",
"lint": "./node_modules/.bin/eslint . --fix"
},
"devDependencies": {
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/register": "^7.9.0",
"babel-eslint": "^10.1.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.11.0",

1260
yarn.lock

File diff suppressed because it is too large Load Diff