Added debug mode
This commit is contained in:
parent
d601f4bfab
commit
5476a9bb06
6 changed files with 1274 additions and 43 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
node_modules
|
node_modules
|
||||||
local.sh
|
local.sh
|
||||||
currentSongId
|
currentSongId
|
||||||
|
dist
|
||||||
|
|
9
app.js
9
app.js
|
@ -4,7 +4,7 @@ import cookieParser from "cookie-parser";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import Pino from "express-pino-logger";
|
import Pino from "express-pino-logger";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import config from "./config";
|
import { port, trustProxy } from "./config";
|
||||||
import {
|
import {
|
||||||
formatResponse,
|
formatResponse,
|
||||||
formatResponseError,
|
formatResponseError,
|
||||||
|
@ -17,7 +17,7 @@ let currentSong = {};
|
||||||
const pino = new Pino({
|
const pino = new Pino({
|
||||||
prettyPrint: true,
|
prettyPrint: true,
|
||||||
});
|
});
|
||||||
pino.logger.info(`Server started on port ${config.port}`);
|
pino.logger.info(`Server started on port ${port}`);
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
|
||||||
app.set("trust proxy", config.trustProxy);
|
app.set("trust proxy", trustProxy);
|
||||||
|
|
||||||
// Get last saved song id on load
|
// Get last saved song id on load
|
||||||
fs.readFile("./currentSongId", "utf8", (err, data) => {
|
fs.readFile("./currentSongId", "utf8", (err, data) => {
|
||||||
|
@ -39,7 +39,6 @@ fs.readFile("./currentSongId", "utf8", (err, data) => {
|
||||||
|
|
||||||
app.post("/post", function (req, res, next) {
|
app.post("/post", function (req, res, next) {
|
||||||
if (req.body && req.body.now_playing && req.body.now_playing.song) {
|
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;
|
const newSongId = req.body.now_playing.song.id;
|
||||||
if (newSongId !== currentSongId) {
|
if (newSongId !== currentSongId) {
|
||||||
currentSongId = newSongId;
|
currentSongId = newSongId;
|
||||||
|
@ -49,7 +48,7 @@ app.post("/post", function (req, res, next) {
|
||||||
req.log.error(err);
|
req.log.error(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
publishMessage(req.body.now_playing, (err, response) => {
|
publishMessage(req.body.now_playing, req.log, (err, response) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
req.log.error(err);
|
req.log.error(err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
port: process.env.PORT || 3000,
|
port: process.env.PORT || 3000,
|
||||||
|
isDebug: process.env.DEBUG && process.env.DEBUG.toLowerCase() === "true",
|
||||||
trustProxy: process.env.TRUST_PROXY || "loopback",
|
trustProxy: process.env.TRUST_PROXY || "loopback",
|
||||||
mastodonToken: process.env.MASTODON_TOKEN,
|
mastodonToken: process.env.MASTODON_TOKEN,
|
||||||
mastondonApi: process.env.MASTODON_API_URL || "https://mamot.fr/api/v1/",
|
mastondonApi: process.env.MASTODON_API_URL || "https://mamot.fr/api/v1/",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import request from "request";
|
import request from "request";
|
||||||
import Masto from "mastodon";
|
import Masto from "mastodon";
|
||||||
import config from "../config";
|
import { mastodonToken, mastondonApi, isDebug } from "../config";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fonction permettant de formater une réponse API
|
* Fonction permettant de formater une réponse API
|
||||||
|
@ -96,9 +96,10 @@ const _formatMessage = (values) => {
|
||||||
/**
|
/**
|
||||||
* Fonction publiant un message (et média si attaché) sur Mastdon
|
* Fonction publiant un message (et média si attaché) sur Mastdon
|
||||||
* @param {Object} playing
|
* @param {Object} playing
|
||||||
|
* @param {Object} logger
|
||||||
* @param {Function} cb
|
* @param {Function} cb
|
||||||
*/
|
*/
|
||||||
const _publishMessage = (playing, cb) => {
|
const _publishMessage = (playing, logger, cb) => {
|
||||||
const callback = (err, res) => {
|
const callback = (err, res) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
cb(err);
|
cb(err);
|
||||||
|
@ -113,13 +114,15 @@ const _publishMessage = (playing, cb) => {
|
||||||
|
|
||||||
// Instanciation de Mastodon
|
// Instanciation de Mastodon
|
||||||
const M = new Masto({
|
const M = new Masto({
|
||||||
access_token: config.mastodonToken,
|
access_token: mastodonToken,
|
||||||
api_url: config.mastondonApi,
|
api_url: mastondonApi,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (cover) {
|
if (cover) {
|
||||||
_getMedia(cover, (err, dest) => {
|
_getMedia(cover, (err, dest) => {
|
||||||
if (err) {
|
if (isDebug) {
|
||||||
|
logger.info({ cover, status });
|
||||||
|
} else if (err) {
|
||||||
M.post("statuses", { status }, callback);
|
M.post("statuses", { status }, callback);
|
||||||
} else {
|
} else {
|
||||||
M.post("media", { file: fs.createReadStream(dest) })
|
M.post("media", { file: fs.createReadStream(dest) })
|
||||||
|
@ -132,6 +135,8 @@ const _publishMessage = (playing, cb) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else if (isDebug) {
|
||||||
|
logger.info(status);
|
||||||
} else {
|
} else {
|
||||||
M.post("statuses", { status }, callback);
|
M.post("statuses", { status }, callback);
|
||||||
}
|
}
|
||||||
|
|
23
package.json
23
package.json
|
@ -7,7 +7,20 @@
|
||||||
"author": "dbroqua <dbroqua@mousur.org>",
|
"author": "dbroqua <dbroqua@mousur.org>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": false,
|
"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": {
|
"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",
|
"body-parser": "^1.19.0",
|
||||||
"cookie-parser": "^1.4.5",
|
"cookie-parser": "^1.4.5",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
@ -17,18 +30,8 @@
|
||||||
"pino-pretty": "^4.0.0",
|
"pino-pretty": "^4.0.0",
|
||||||
"request": "^2.88.2"
|
"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/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": "^6.8.0",
|
||||||
"eslint-config-airbnb-base": "^14.1.0",
|
"eslint-config-airbnb-base": "^14.1.0",
|
||||||
|
|
Loading…
Reference in a new issue