ionian/app.ts

106 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-01-10 14:45:34 +09:00
import { app, BrowserWindow, session } from "electron";
2021-01-10 03:04:30 +09:00
import { get_setting } from "./src/setting";
import { create_server, start_server } from "./src/server";
2021-01-10 18:02:26 +09:00
import { getAdminCookieValue, loginTokenName } from "./src/login";
2021-01-10 03:04:30 +09:00
2021-01-10 03:12:51 +09:00
const get_loading_html = (content?:string)=> `<!DOCTYPE html>
2021-01-10 03:04:30 +09:00
<html lang="ko"><head>
<meta charset="UTF-8">
<title>react-sample</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
body { margin-top: 100px; background-color: #3f51b5; color: #fff; text-align:center; }
h1 {
font: 2em 'Roboto', sans-serif;
margin-bottom: 40px;
}
#loading {
display: inline-block;
width: 50px;
height: 50px;
border: 3px solid rgba(255,255,255,.3);
border-radius: 50%;
border-top-color: #fff;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg);}
}
</style>
<body>
2021-01-10 03:12:51 +09:00
<h1>${content || "Loading..."}</h1>
${content === undefined ? '<div id="loading"></div>' : ""}
2021-01-10 03:04:30 +09:00
</body>
</html>
`;
const setting = get_setting();
if (!setting.cli) {
let window: BrowserWindow | null = null;
const createWindow = async () => {
window = new BrowserWindow({
width: 800,
height: 600,
center: true,
useContentSize: true,
});
2021-01-10 03:12:51 +09:00
await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html()).toString('base64'));
2021-01-10 14:45:34 +09:00
await session.defaultSession.cookies.set({
url:`http://localhost:${setting.port}`,
2021-01-10 18:02:26 +09:00
name:loginTokenName,
2021-01-10 14:45:34 +09:00
value:getAdminCookieValue(),
httpOnly: true,
secure: false,
sameSite:"strict"
});
2021-01-10 03:12:51 +09:00
try{
const server = await create_server();
start_server(server);
await window.loadURL(`http://localhost:${setting.port}`);
}
catch(e){
if(e instanceof Error){
await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html("Error : "+e.message)).toString('base64'));
}
else{
await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html("Error : "+e)).toString('base64'));
}
}
2021-01-10 03:04:30 +09:00
window.on("closed", () => {
window = null;
});
};
const isPrimary = app.requestSingleInstanceLock();
if (!isPrimary) {
app.quit(); //exit window
app.exit();
}
app.on("second-instance", () => {
if (window != null) {
if (window.isMinimized()) {
window.restore();
}
window.focus();
}
});
app.on("ready", (event, info) => {
createWindow();
});
app.on("window-all-closed", () => { // quit when all windows are closed
if (process.platform != "darwin") app.quit(); // (except leave MacOS app active until Cmd+Q)
});
app.on("activate", () => { // re-recreate window when dock icon is clicked and no other windows open
if (window == null) createWindow();
});
} else {
(async () => {
const server = await create_server();
start_server(server);
})();
}