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 swaggerjsdoc from "swagger-jsdoc";
|
|
|
|
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-05-14 12:53:39 +02:00
|
|
|
import cors from "cors";
|
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);
|
|
|
|
const specs = swaggerjsdoc({
|
|
|
|
definition: {
|
|
|
|
openapi: "3.1.0",
|
|
|
|
info: {
|
|
|
|
title: "TIDAL Hi-Fi API",
|
2024-05-20 15:24:15 +02:00
|
|
|
version: "5.13.0",
|
2024-05-20 15:23:26 +02:00
|
|
|
description: "",
|
|
|
|
license: {
|
|
|
|
name: "MIT",
|
|
|
|
url: "https://github.com/Mastermindzh/tidal-hifi/blob/master/LICENSE",
|
|
|
|
},
|
|
|
|
contact: {
|
|
|
|
name: "Rick <mastermindzh> van Lieshout",
|
|
|
|
url: "https://www.rickvanlieshout.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
servers: [
|
|
|
|
{
|
|
|
|
url: `http://localhost:${port}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
externalDocs: {
|
|
|
|
description: "swagger.json",
|
|
|
|
url: "swagger.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
apis: ["**/*.ts"],
|
|
|
|
});
|
|
|
|
|
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-20 15:23:26 +02:00
|
|
|
expressApp.use("/docs", swaggerUi.serve, swaggerUi.setup(specs));
|
2024-05-05 20:12:10 +02:00
|
|
|
expressApp.get("/", (req, res) => res.send("Hello World!"));
|
2024-05-20 15:23:26 +02:00
|
|
|
expressApp.get("/swagger.json", (req, res) => res.json(specs));
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|