forked from Loccy/NoobLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.mongo.js
More file actions
219 lines (193 loc) · 6.05 KB
/
main.mongo.js
File metadata and controls
219 lines (193 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const { spawn } = require("child_process");
require("dotenv").config(); // Load environment variables
const mongoose = require("mongoose");
let mainWindow;
let shellProcess;
// --- Database Setup ---
// Connect to MongoDB (assuming local instance for now, or use a cloud URI)
const MONGO_URI =
process.env.MONGO_URI || "mongodb://localhost:27017/nooblab_desktop";
console.log(
"Connecting to MongoDB at:",
MONGO_URI.replace(/:([^:@]+)@/, ":****@")
); // Log URI with masked password
mongoose
.connect(MONGO_URI)
.then(() => {
console.log("MongoDB Connected");
seedDatabase();
})
.catch((err) =>
console.log(
"MongoDB Connection Error (Expected if no DB running):",
err
)
);
// Define a simple User Schema
const UserSchema = new mongoose.Schema({
username: String,
password: String, // In production, hash this!
});
const User = mongoose.model("User", UserSchema);
const CourseSchema = new mongoose.Schema({
id: String,
title: String,
description: String,
});
const Course = mongoose.model("Course", CourseSchema);
async function seedDatabase() {
try {
const userCount = await User.countDocuments();
if (userCount === 0) {
console.log("Seeding Users...");
await User.create({ username: "student", password: "password" });
await User.create({ username: "admin", password: "admin" });
console.log("Users Seeded");
}
const courseCount = await Course.countDocuments();
if (courseCount === 0) {
console.log("Seeding Courses...");
await Course.create([
{
id: "java101",
title: "Java Programming 101",
description:
"Introduction to Java, variables, loops, and objects.",
},
{
id: "python_basics",
title: "Python Basics",
description:
"Learn Python syntax and basic data structures.",
},
{
id: "web_dev",
title: "Web Development",
description: "HTML, CSS, and JavaScript fundamentals.",
},
]);
console.log("Courses Seeded");
}
} catch (err) {
console.error("Seeding Error:", err);
}
}
// ----------------------
// Terminal IPC
ipcMain.on("terminal-init", (event) => {
const shell = process.platform === "win32" ? "powershell.exe" : "bash";
shellProcess = spawn(shell, [], {
cwd: process.cwd(),
env: process.env,
});
shellProcess.stdout.on("data", (data) => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send("terminal-incoming", data.toString());
}
});
shellProcess.stderr.on("data", (data) => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send("terminal-incoming", data.toString());
}
});
shellProcess.on("exit", (code) => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send(
"terminal-incoming",
`\r\nProcess exited with code ${code}\r\n`
);
}
});
});
ipcMain.on("terminal-input", (event, data) => {
if (shellProcess && !shellProcess.killed) {
shellProcess.stdin.write(data);
}
});
// --- Auth IPC ---
ipcMain.on("login-attempt", async (event, { username, password }) => {
console.log(`Login attempt for: ${username}`);
try {
// Check the DB
const user = await User.findOne({ username, password });
if (user) {
console.log("Login successful");
// Navigate the webview to the dashboard
if (mainWindow) {
const dashboardPath = path.join(
__dirname,
"local-server",
"public",
"dashboard.html"
);
const dashboardUrl = `file://${dashboardPath}`;
mainWindow.webContents.send("server-ready", dashboardUrl);
}
} else {
console.log("Login failed");
// Send failure message back (we can use event.reply if using ipcRenderer.send)
event.reply("login-failed", "Invalid credentials");
}
} catch (e) {
console.error("Login Error:", e);
event.reply("login-failed", "Server error");
}
});
ipcMain.handle("get-dashboard-data", async (event) => {
try {
const courses = await Course.find({});
return { courses };
} catch (e) {
console.error("Fetch Error:", e);
return { courses: [] };
}
});
// ----------------
function createWindow() {
mainWindow = new BrowserWindow({
width: 1280,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
webviewTag: true, // Enable <webview> tag
},
});
const localIndex = path.join(__dirname, "index.html");
mainWindow.loadFile(localIndex);
// Point to our new local login page
const loginPath = path.join(
__dirname,
"local-server",
"public",
"login.html"
);
const loginUrl = `file://${loginPath}`;
mainWindow.webContents.on("did-finish-load", () => {
mainWindow.webContents.send("server-ready", loginUrl);
});
mainWindow.on("closed", function () {
mainWindow = null;
});
}
app.on("ready", () => {
createWindow();
});
app.on("window-all-closed", function () {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", function () {
if (mainWindow === null) {
createWindow();
}
});
app.on("will-quit", () => {
if (javaProcess) {
console.log("Killing Java server...");
javaProcess.kill();
}
});