2024-06-09 13:07:49 +02:00
|
|
|
import cors from "cors";
|
2024-05-05 20:12:10 +02:00
|
|
|
import { BrowserWindow, dialog } from "electron";
|
|
|
|
import express from "express";
|
2024-05-20 15:23:26 +02:00
|
|
|
import swaggerUi from "swagger-ui-express";
|
2024-05-05 20:12:10 +02:00
|
|
|
import { settingsStore } from "../../scripts/settings";
|
2024-05-20 15:23:26 +02:00
|
|
|
import { settings } from "./../../constants/settings";
|
2024-05-05 20:12:10 +02:00
|
|
|
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";
|
2024-06-09 13:07:49 +02:00
|
|
|
import swaggerSpec from "./swagger.json";
|
2024-05-05 20:12:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to enable TIDAL Hi-Fi's express api
|
|
|
|
*/
|
|
|
|
export const startApi = (mainWindow: BrowserWindow) => {
|
2024-05-20 15:23:26 +02:00
|
|
|
const port = settingsStore.get<string, number>(settings.apiSettings.port);
|
2024-06-09 13:07:49 +02:00
|
|
|
const hostname = settingsStore.get<string, string>(settings.apiSettings.hostname) ?? "127.0.0.1";
|
2024-05-20 15:23:26 +02:00
|
|
|
|
2024-05-05 20:12:10 +02:00
|
|
|
const expressApp = express();
|
2024-05-14 12:53:39 +02:00
|
|
|
expressApp.use(cors());
|
2024-05-20 14:24:47 +02:00
|
|
|
expressApp.use(express.json());
|
2024-05-27 12:28:45 +02:00
|
|
|
expressApp.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
2024-05-05 20:12:10 +02:00
|
|
|
expressApp.get("/", (req, res) => res.send("Hello World!"));
|
2024-05-27 12:28:45 +02:00
|
|
|
expressApp.get("/swagger.json", (req, res) => res.json(swaggerSpec));
|
2024-05-05 20:12:10 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2024-06-09 13:07:49 +02:00
|
|
|
const expressInstance = expressApp.listen(port, hostname);
|
2024-05-05 20:12:10 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|