Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wrap Casbin editor into a desktop app via Electron #109

Merged
merged 7 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# production
/build
/dist

# misc
.DS_Store
Expand Down
60 changes: 60 additions & 0 deletions main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { app, BrowserWindow, protocol } = require('electron');
hsluoyz marked this conversation as resolved.
Show resolved Hide resolved
const path = require('path');

// Register the app protocol before app is ready
protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }]);

let appServe;
let isDev;

const loadModules = async () => {
isDev = (await import('electron-is-dev')).default;
if (!isDev) {
const serve = (await import('electron-serve')).default;
appServe = serve({
directory: path.join(__dirname, '../out'),
});
}
};

const createWindow = async () => {
const win = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});

if (!isDev && appServe) {
appServe(win)
.then(() => {
win.loadURL('app://-');
})
.catch((err) => {
console.error('Failed to serve app:', err);
});
} else {
win.loadURL('http://editor.casbin.org');
win.webContents.on('did-fail-load', () => {
win.loadURL('http://editor.casbin.org');
});
}
};

app.whenReady().then(async () => {
await loadModules();
createWindow();
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
10 changes: 10 additions & 0 deletions main/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
on: (channel, callback) => {
ipcRenderer.on(channel, callback);
},
send: (channel, args) => {
ipcRenderer.send(channel, args);
},
});
Loading