gmail-to-exchange365
Version:
Complete Gmail to Exchange 365 migration tool with UI - Migrate emails, attachments, and folders seamlessly
127 lines • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const googleAuth_1 = require("./googleAuth");
const msAuth_1 = require("./msAuth");
const migrator_1 = require("./migrator");
const router = (0, express_1.Router)();
// Home page
router.get("/", (req, res) => {
res.sendFile("index.html", { root: __dirname + "/../ui" });
});
// Google OAuth
router.get("/google/auth", (req, res) => {
const authUrl = (0, googleAuth_1.generateGoogleAuthUrl)();
res.redirect(authUrl);
});
router.get("/google/callback", async (req, res) => {
try {
const code = req.query.code;
if (!code) {
return res.redirect("/?error=no_code");
}
const tokens = await (0, googleAuth_1.exchangeGoogleCode)(code);
req.session.google = tokens;
req.session.save(() => {
res.redirect("/?google=connected");
});
}
catch (error) {
console.error("Google OAuth error:", error);
res.redirect("/?error=google_auth_failed");
}
});
// Microsoft OAuth
router.get("/ms/auth", (req, res) => {
const authUrl = (0, msAuth_1.getMSAuthUrl)();
res.redirect(authUrl);
});
router.get("/ms/callback", async (req, res) => {
try {
const code = req.query.code;
if (!code) {
return res.redirect("/?error=no_code");
}
const tokens = await (0, msAuth_1.exchangeMSCode)(code);
req.session.ms = tokens;
req.session.save(() => {
res.redirect("/?ms=connected");
});
}
catch (error) {
console.error("Microsoft OAuth error:", error);
res.redirect("/?error=ms_auth_failed");
}
});
// Check auth status
router.get("/api/status", (req, res) => {
res.json({
google: !!req.session.google,
ms: !!req.session.ms
});
});
// Start migration
router.post("/api/migrate", async (req, res) => {
try {
if (!req.session.google || !req.session.ms) {
return res.status(400).json({
error: "Both Google and Microsoft accounts must be connected"
});
}
// Set up Server-Sent Events for progress updates
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
const options = {
batchSize: req.body.batchSize || 10,
delayBetweenBatches: req.body.delayBetweenBatches || 1000,
retryAttempts: req.body.retryAttempts || 3,
retryDelay: req.body.retryDelay || 5000
};
await (0, migrator_1.migrateUser)(req.session.google, req.session.ms, (current, total, message) => {
const progress = {
current,
total,
percentage: total > 0 ? Math.round((current / total) * 100) : 0,
message: message || `Migrated ${current}/${total} emails`
};
res.write(`data: ${JSON.stringify(progress)}\n\n`);
}, options);
res.write(`data: ${JSON.stringify({ completed: true })}\n\n`);
res.end();
}
catch (error) {
console.error("Migration error:", error);
res.write(`data: ${JSON.stringify({ error: error.message })}\n\n`);
res.end();
}
});
// Pause migration (would need to store migrator instance in production)
router.post("/api/migrate/pause", (req, res) => {
// In a real implementation, you'd store the migrator instance
// and call pause() on it
res.json({ message: "Migration paused" });
});
// Resume migration
router.post("/api/migrate/resume", (req, res) => {
// In a real implementation, you'd store the migrator instance
// and call resume() on it
res.json({ message: "Migration resumed" });
});
// Stop migration
router.post("/api/migrate/stop", (req, res) => {
// In a real implementation, you'd store the migrator instance
// and call stop() on it
res.json({ message: "Migration stopped" });
});
// Logout
router.post("/api/logout", (req, res) => {
req.session.destroy((err) => {
if (err) {
return res.status(500).json({ error: "Failed to logout" });
}
res.json({ message: "Logged out successfully" });
});
});
exports.default = router;
//# sourceMappingURL=routes.js.map