Skip to content
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
Prev Previous commit
logger functionality
  • Loading branch information
RohitKini committed Sep 18, 2024
commit 61d94859ac08b541759dba294f46c95ac2db03ff
1 change: 1 addition & 0 deletions api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type ConfigType = {
AZURE_EU?: string;
GCP_NA?: string;
};
LOG_FILE_PATH: string;
};

/**
Expand Down
99 changes: 61 additions & 38 deletions api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,86 +21,109 @@ import { Server } from "socket.io";
import http from "http";
import fs from "fs";

// Initialize file watcher for the log file
const watcher = chokidar.watch(config.LOG_FILE_PATH,{
usePolling: true, // Enables polling to detect changes in all environments
interval: 100, // Poll every 100ms (you can adjust this if needed)
awaitWriteFinish: { // Wait for file to finish being written before triggering
stabilityThreshold: 500, // Time to wait before considering the file stable
pollInterval: 100, // Interval at which to poll for file stability
},
persistent: true, // Keeps watching the file even after initial change
}); // Initialize with initial log path

let io: Server; // Socket.IO server instance

// Dynamically change the log file path and update the watcher
export async function setLogFilePath(path:string) {
console.info(`Setting new log file path: ${path}`);

// Stop watching the old log file
watcher.unwatch(config.LOG_FILE_PATH);

// Update the config and start watching the new log file
config.LOG_FILE_PATH = path;
watcher.add(path);
}

try {
const app = express();

// Set security-related HTTP headers
app.use(
helmet({
crossOriginOpenerPolicy: false,
crossOriginOpenerPolicy: false, // Disable to allow cross-origin resource sharing
})
);

// Enable CORS for all origins
app.use(cors({ origin: "*" }));

// Parsing request bodies
app.use(express.urlencoded({ extended: false, limit: "10mb" }));
app.use(express.json({ limit: "10mb" }));

// Custom middleware for logging and request headers
app.use(loggerMiddleware);
app.use(requestHeadersMiddleware);

// Routes
// Define routes
app.use("/v2/auth", authRoutes);
app.use("/v2/user", authenticateUser, userRoutes);
app.use("/v2/org/:orgId", authenticateUser, orgRoutes);
app.use("/v2/org/:orgId/project", authenticateUser, projectRoutes);
app.use("/v2/mapper", authenticateUser, contentMapperRoutes);
app.use("/v2/migration", authenticateUser, migrationRoutes);

//For unmatched route patterns
// Handle unmatched routes
app.use(unmatchedRoutesMiddleware);

// Error Middleware
// Handle errors
app.use(errorMiddleware);

// starting the server & DB connection.
// Start the server and establish DB connection
(async () => {
await connectToDatabase();
await connectToDatabase(); // Establish DB connection

const server = app.listen(config.PORT, () =>
logger.info(`Server listening at port ${config.PORT}`)
);
// Chokidar - Watch for log file changes
const logFilePath = config.LOG_FILE_PATH;
const watcher = chokidar.watch(logFilePath);
// Socket.IO - Send logs to client
/**
* The Socket.IO server instance.
*
* @remarks
* This server instance is responsible for handling real-time communication between the client and the server using the Socket.IO library.
*
* @type {Server}
*/
const io = new Server(
server,
(http,
{
cors: {
origin: "*", // This allows all origins. For production, specify exact origins for security.
methods: ["GET", "POST"], // Specify which HTTP methods are allowed.
allowedHeaders: ["my-custom-header"], // Specify which headers are allowed.
credentials: true, // If your client needs to send cookies or credentials with the requests.
},
})
);

// Initialize Socket.IO for real-time log updates
io = new Server(server, {
cors: {
origin: "*", // Allow all origins; adjust for production
methods: ["GET", "POST"],
allowedHeaders: ["my-custom-header"],
credentials: true,
},
});

// Emit initial log file content to connected clients

// File watcher for log file changes
watcher.on("change", (path) => {
// Read the updated log file
console.info(`File changed: ${path}`);

// Read the updated file content
fs.readFile(path, "utf8", (err, data) => {
if (err) {
logger.error(`Error reading log file: ${err}`);
return;
}
// Get just the updated data
// const updatedData = data.slice(data.lastIndexOf("\n") + 1);
console.info("updates", data);
// Emit the updated data to all connected clients

try {
const parsedData = data;
io.emit("logUpdate", parsedData);
// Emit the updated log content to connected clients
io.emit("logUpdate", data);
} catch (error) {
logger.error(`Error parsing data: ${error}`);
logger.error(`Error emitting log data: ${error}`);
}
});
});

})();
} catch (e) {
logger.error("Error while starting the server!");
logger.error(e);
}

8 changes: 6 additions & 2 deletions api/src/services/migration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import AuthenticationModel from "../models/authentication.js";
import { siteCoreService } from "./sitecore.service.js";
import { fileURLToPath } from 'url';
import { copyDirectory } from '../utils/index.js'

import { setLogFilePath } from "../server.js";



Expand Down Expand Up @@ -221,14 +221,18 @@ const runCli = async (rg: string, user_id: string, project: any) => {
const sourcePath = path.join(dirPath, 'sitecoreMigrationData', project?.destination_stack_id);
const backupPath = path.join(process.cwd(), 'migration-data', project?.destination_stack_id);
await copyDirectory(sourcePath, backupPath);

const loggerPath = path.join(backupPath, 'logs', 'import','success.log');
console.info('loggerPath', loggerPath);
await setLogFilePath(loggerPath);
shell.cd(path.join(process.cwd(), '..', 'cli', 'packages', 'contentstack'));
const pwd = shell.exec('pwd');
cliLogger(pwd);
const region = shell.exec(`node bin/run config:set:region ${regionPresent}`);
cliLogger(region);
const login = shell.exec(`node bin/run login -a ${userData?.authtoken} -e ${userData?.email}`);
cliLogger(login);
const exportData = shell.exec(`node bin/run cm:stacks:import -k ${project?.destination_stack_id} -d ${sourcePath} --backup-dir=${backupPath} --yes`);
const exportData = shell.exec(`node bin/run cm:stacks:import -k ${project?.destination_stack_id} -d ${sourcePath} --backup-dir=${backupPath} --yes`, { async: true });
cliLogger(exportData);
} else {
console.info('user not found.')
Expand Down