Skip to content
This repository was archived by the owner on Mar 5, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/hub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const data: {

const ui: { projectsTreeView?: any } = {};
let socket: SocketIOClient.Socket;
const port = (window.location.port.length === 0) ? "80" : window.location.port;
const port = (window.location.port.length === 0) ? (window.location.protocol === "https:" ? "443" : "80") : window.location.port;

const languageNamesById: { [id: string]: string; } = {};
if (localStorage.getItem("superpowers-dev-mode") != null) languageNamesById["none"] = "None";
Expand Down
2 changes: 1 addition & 1 deletion client/src/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../window";

const port = (window.location.port.length === 0) ? "80" : window.location.port;
const port = (window.location.port.length === 0) ? (window.location.protocol === "https" ? "443" : "80") : window.location.port;

const connectingElt = document.querySelector(".connecting") as HTMLDivElement;
const formElt = document.querySelector(".login") as HTMLDivElement;
Expand Down
22 changes: 12 additions & 10 deletions server/loadSystems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function(mainApp: express.Express, buildApp: express.Express, cal
// Expose public stuff
try { fs.mkdirSync(`${systemPath}/public`); } catch (err) { /* Ignore */ }
mainApp.use(`/systems/${systemId}`, express.static(`${systemPath}/public`));
buildApp.use(`/systems/${systemId}`, express.static(`${systemPath}/public`));
if (buildApp != null) buildApp.use(`/systems/${systemId}`, express.static(`${systemPath}/public`));

// Write templates list
let templatesList: string[] = [];
Expand Down Expand Up @@ -149,16 +149,18 @@ function loadPlugins (systemId: string, pluginsPath: string, mainApp: express.Ex
});
});

for (const app of [mainApp, buildApp]) {
app.get(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}/bundles/*.js`, (req, res) => {
const bundleFile = req.path.split("/bundles/")[1];
const bundlePath = path.join(pluginPath, "public/bundles", bundleFile);
fs.exists(bundlePath, (exists) => {
if (exists) res.sendFile(bundlePath);
else res.send("");
for (const app of [ mainApp, buildApp ]) {
if (app != null) {
app.get(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}/bundles/*.js`, (req, res) => {
const bundleFile = req.path.split("/bundles/")[1];
const bundlePath = path.join(pluginPath, "public/bundles", bundleFile);
fs.exists(bundlePath, (exists) => {
if (exists) res.sendFile(bundlePath);
else res.send("");
});
});
});
app.use(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}`, express.static(`${pluginPath}/public`));
app.use(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}`, express.static(`${pluginPath}/public`));
}
}
});
});
Expand Down
44 changes: 31 additions & 13 deletions server/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ export default function start(serverDataPath: string) {

const { version, superpowers: { appApiVersion: appApiVersion } } = JSON.parse(fs.readFileSync(`${__dirname}/../package.json`, { encoding: "utf8" }));
SupCore.log(`Server v${version} starting...`);
fs.writeFileSync(`${__dirname}/../public/superpowers.json`, JSON.stringify({ version, appApiVersion, hasPassword: config.server.password.length !== 0 }, null, 2));

const serverPublicConfig = {
version, appApiVersion,
hasPassword: config.server.password.length !== 0,
useSSL: config.server.mainPort === 443
};
fs.writeFileSync(`${__dirname}/../public/superpowers.json`, JSON.stringify(serverPublicConfig, null, 2));

// SupCore
(global as any).SupCore = SupCore;
Expand Down Expand Up @@ -76,12 +82,13 @@ export default function start(serverDataPath: string) {
io = socketio(mainHttpServer, { transports: [ "websocket" ] });

// Build HTTP server
buildApp = express();
let hasSeparateBuildPort = config.server.mainPort !== config.server.buildPort;
buildApp = hasSeparateBuildPort ? express() : mainApp;

buildApp.get("/", redirectToHub);
if (hasSeparateBuildPort) buildApp.get("/", redirectToHub);
buildApp.get("/systems/:systemId/SupCore.js", serveSystemSupCore);

buildApp.use("/", express.static(`${__dirname}/../public`));
if (hasSeparateBuildPort) buildApp.use("/", express.static(`${__dirname}/../public`));

buildApp.get("/builds/:projectId/:buildId/*", (req, res) => {
const projectServer = hub.serversById[req.params.projectId];
Expand All @@ -91,10 +98,12 @@ export default function start(serverDataPath: string) {
res.sendFile(path.join(projectServer.buildsPath, buildId, req.params[0]));
});

buildHttpServer = http.createServer(buildApp);
buildHttpServer.on("error", onHttpServerError.bind(null, config.server.buildPort));
if (hasSeparateBuildPort) {
buildHttpServer = http.createServer(buildApp);
buildHttpServer.on("error", onHttpServerError.bind(null, config.server.buildPort));
}

loadSystems(mainApp, buildApp, onSystemsLoaded);
loadSystems(mainApp, hasSeparateBuildPort ? buildApp : null, onSystemsLoaded);

// Save on exit and handle crashes
process.on("SIGINT", onExit);
Expand All @@ -109,6 +118,7 @@ function loadConfig() {

for (const key in config.defaults) {
if (config.server[key] == null) config.server[key] = config.defaults[key];
else if (config.server[key] === "env.PORT") config.server[key] = process.env.PORT != null ? process.env.PORT : config.defaults[key];
}
} else {
fs.writeFileSync(serverConfigPath, JSON.stringify(config.defaults, null, 2) + "\n", { encoding: "utf8" });
Expand Down Expand Up @@ -195,15 +205,23 @@ function onSystemsLoaded() {
const hostname = (config.server.password.length === 0) ? "localhost" : "";

mainHttpServer.listen(config.server.mainPort, hostname, () => {
buildHttpServer.listen(config.server.buildPort, hostname, () => {
SupCore.log(`Main server started on port ${config.server.mainPort}, build server started on port ${config.server.buildPort}.`);
if (hostname === "localhost") SupCore.log("NOTE: Setup a password to allow other people to connect to your server.");
if (process != null && process.send != null) process.send({ type: "started" });
});
if (buildHttpServer != null) {
buildHttpServer.listen(config.server.buildPort, hostname, () => {
onServerStarted(hostname);
});
} else {
onServerStarted(hostname);
}
});
});
}

function onServerStarted(hostname: string) {
SupCore.log(`Main server started on port ${config.server.mainPort}, build server started on port ${config.server.buildPort}.`);
if (hostname === "localhost") SupCore.log("NOTE: Setup a password to allow other people to connect to your server.");
if (process != null && process.send != null) process.send({ type: "started" });
}

function handle404(err: any, req: express.Request, res: express.Response, next: Function) {
if (err.status === 404) { res.status(404).end("File not found"); return; }
next();
Expand All @@ -213,7 +231,7 @@ function onExit() {
if (isQuitting) return;
isQuitting = true;
mainHttpServer.close();
buildHttpServer.close();
if (buildHttpServer != null) buildHttpServer.close();

if (hub == null) {
process.exit(0);
Expand Down