ionian/app.ts
2021-02-22 23:09:35 +09:00

83 lines
2.4 KiB
TypeScript

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