2024-05-05 20:12:10 +02:00
|
|
|
import { BrowserWindow, dialog } from "electron";
|
|
|
|
import express from "express";
|
|
|
|
import { settings } from "../../constants/settings";
|
|
|
|
import { settingsStore } from "../../scripts/settings";
|
|
|
|
import { addCurrentInfo } from "./features/current";
|
|
|
|
import { addPlaybackControl } from "./features/player";
|
2024-05-20 14:24:47 +02:00
|
|
|
import { addSettingsAPI } from "./features/settings/settings";
|
2024-05-05 20:12:10 +02:00
|
|
|
import { addLegacyApi } from "./legacy";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to enable TIDAL Hi-Fi's express api
|
|
|
|
*/
|
|
|
|
export const startApi = (mainWindow: BrowserWindow) => {
|
|
|
|
const expressApp = express();
|
2024-05-20 14:24:47 +02:00
|
|
|
expressApp.use(express.json());
|
2024-05-05 20:12:10 +02:00
|
|
|
expressApp.get("/", (req, res) => res.send("Hello World!"));
|
|
|
|
|
|
|
|
// add features
|
|
|
|
addLegacyApi(expressApp, mainWindow);
|
|
|
|
addPlaybackControl(expressApp, mainWindow);
|
|
|
|
addCurrentInfo(expressApp);
|
2024-05-20 14:24:47 +02:00
|
|
|
addSettingsAPI(expressApp, mainWindow);
|
2024-05-05 20:12:10 +02:00
|
|
|
|
|
|
|
const port = settingsStore.get<string, number>(settings.apiSettings.port);
|
|
|
|
const expressInstance = expressApp.listen(port, "127.0.0.1");
|
|
|
|
expressInstance.on("error", function (e: { code: string }) {
|
|
|
|
let message = e.code;
|
|
|
|
if (e.code === "EADDRINUSE") {
|
|
|
|
message = `Port ${port} in use.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.showErrorBox("Api failed to start.", message);
|
|
|
|
});
|
|
|
|
};
|