ionian/app.ts

83 lines
2.4 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-11 03:14:29 +09:00
import { getAdminAccessTokenValue,getAdminRefreshTokenValue, accessTokenName, refreshTokenName } from "./src/login";
2021-01-10 03:04:30 +09:00
const setting = get_setting();
if (!setting.cli) {
2021-01-12 03:35:29 +09:00
let wnd: BrowserWindow | null = null;
2021-01-10 03:04:30 +09:00
const createWindow = async () => {
2021-01-12 03:35:29 +09:00
wnd = new BrowserWindow({
2021-01-10 03:04:30 +09:00
width: 800,
height: 600,
center: true,
useContentSize: true,
});
2021-01-12 03:35:29 +09:00
//await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html()).toString('base64'));
await wnd.loadFile('../loading.html');
2021-01-10 14:45:34 +09:00
await session.defaultSession.cookies.set({
url:`http://localhost:${setting.port}`,
2021-01-11 03:14:29 +09:00
name:accessTokenName,
value:getAdminAccessTokenValue(),
httpOnly: true,
secure: false,
sameSite:"strict"
});
await session.defaultSession.cookies.set({
url:`http://localhost:${setting.port}`,
name:refreshTokenName,
value:getAdminRefreshTokenValue(),
2021-01-10 14:45:34 +09:00
httpOnly: true,
secure: false,
sameSite:"strict"
});
2021-01-10 03:12:51 +09:00
try{
const server = await create_server();
start_server(server);
2021-01-12 03:35:29 +09:00
await wnd.loadURL(`http://localhost:${setting.port}`);
2021-01-10 03:12:51 +09:00
}
catch(e){
if(e instanceof Error){
2021-01-12 03:35:29 +09:00
await wnd.webContents.executeJavaScript(`document.querySelector("body").innerHTML="<h1>${e.message}</h1>"`)
2021-01-10 03:12:51 +09:00
}
else{
2021-01-12 03:35:29 +09:00
await wnd.webContents.executeJavaScript(`document.querySelector("body").innerHTML="<h1>${e}</h1>"`)
2021-01-10 03:12:51 +09:00
}
}
2021-01-12 03:35:29 +09:00
wnd.on("closed", () => {
wnd = null;
2021-01-10 03:04:30 +09:00
});
};
const isPrimary = app.requestSingleInstanceLock();
if (!isPrimary) {
app.quit(); //exit window
app.exit();
}
app.on("second-instance", () => {
2021-01-12 03:35:29 +09:00
if (wnd != null) {
if (wnd.isMinimized()) {
wnd.restore();
2021-01-10 03:04:30 +09:00
}
2021-01-12 03:35:29 +09:00
wnd.focus();
2021-01-10 03:04:30 +09:00
}
});
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
2021-01-12 03:35:29 +09:00
if (wnd == null) createWindow();
2021-01-10 03:04:30 +09:00
});
} else {
(async () => {
const server = await create_server();
start_server(server);
})();
}