forked from sergeychernyshev/streamerjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·150 lines (119 loc) · 3.84 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env node
import os from "os";
import fs from "fs";
import url from "url";
import express from "express";
import serveIndex from "serve-index";
import ejs from "ejs";
import PouchDB from "pouchdb";
import ExpressPutchDBFactory from "express-pouchdb";
let config;
// Read the content of package.json
const packageJsonPath = url.fileURLToPath(
import.meta.resolve("./package.json")
);
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
// Access the version
const cliVersion = packageJson.version;
// to override the default config, create a config.json file
// in the root folder of your project
const default_config = {
port: 2525,
dbpath: "db",
};
try {
// Read the contents of config.json synchronously
const data = fs.readFileSync("config.json", "utf8");
try {
// Parse the JSON config data
config = { ...default_config, ...JSON.parse(data) };
} catch (jsonError) {
console.error("Error parsing config.json:", jsonError);
process.exit(1);
}
} catch (fileError) {
// use default config when config.json is not found
config = default_config;
}
const insecurePort = config.port || process.env.PORT;
const app = express();
// Scenes in user's project
app.use(
"/scenes/",
express.static("scenes"),
serveIndex("scenes", { icons: true })
);
// Assets in user's project
app.use("/assets/", express.static("assets"));
// streamer resources
const templates = url.fileURLToPath(import.meta.resolve("./templates/"));
app.set("view engine", "ejs").set("views", templates);
let enableControlPanel = false;
// check if control folder exists to indicate that the user wants to create the control panel(s)
if (fs.existsSync("control")) {
enableControlPanel = true;
// Check if the db folder exists, create it if it doesn't
if (!fs.existsSync(config.dbpath)) {
try {
fs.mkdirSync(config.dbpath, { recursive: true });
console.log(`PouchDB database folder created at: ${config.dbpath}`);
} catch (error) {
console.error(`Error creating PouchDB database folder: ${error.message}`);
}
}
const StreamerPouchDB = PouchDB.defaults({ prefix: config.dbpath + "/" });
new StreamerPouchDB("streamer");
// Control panel resources in user's project
app.use("/control/", express.static("control"));
/**
* Server paths
*/
const pouchDBLibPath = url.fileURLToPath(
import.meta.resolve("Pouchdb/dist/")
);
// PouchDB client library
app.use("/_resources/pouchdb/", express.static(pouchDBLibPath));
const pouchApp = ExpressPutchDBFactory(StreamerPouchDB, {
logPath: config.dbpath + "/log.txt",
configPath: config.dbpath + "/config.json",
});
// PouchDB server
app.use("/_db", pouchApp);
}
// Get network interfaces
const networkInterfaces = os.networkInterfaces();
const ips = [];
// Iterate over each network interface
Object.keys(networkInterfaces).forEach((interfaceName) => {
const interfaces = networkInterfaces[interfaceName];
// Iterate over each interface
interfaces.forEach((interfaceInfo) => {
// Check if the address is an IPv4
if (interfaceInfo.family === "IPv4") {
ips.push(interfaceInfo.address);
}
});
});
// Server index linking to other parts of the server
app.get("/", (req, res) => {
app.engine("ejs", ejs.renderFile);
res.render("index", { control: enableControlPanel, ips, port: config.port });
});
// Assets in user's project
app.use(
"/_resources/",
express.static(url.fileURLToPath(import.meta.resolve("./resources/")))
);
// HTTP server
app.listen(insecurePort);
console.log(`StreamerJS (v${cliVersion}) server listening on:`);
ips.forEach((ip) => {
console.log(`http://${ip}:${insecurePort}`);
});
if (enableControlPanel) {
console.log("\nControl your stream by opening:");
ips.forEach((ip) => {
console.log(`http://${ip}:${insecurePort}/control/`);
});
}