diff --git a/src/routes/datasets.js b/src/routes/datasets.js new file mode 100644 index 0000000..1c2b753 --- /dev/null +++ b/src/routes/datasets.js @@ -0,0 +1,34 @@ +import { Router } from 'express'; +import { join } from 'path'; +import { getProjectPath } from '../utils/paths.js'; + +const router = Router(); + +// Environment variables for paths (all relative to project root) +const DATA_DIR = process.env.DATA_DIR ? getProjectPath(process.env.DATA_DIR) : getProjectPath('data'); + +const DATASETS = [ + "comment_generation", + "code_refinement" +]; + +router.get('/download/:dataset', async (req, res) => { + const { dataset } = req.params; + const withContext = req.query.withContext ? JSON.parse(req.query.withContext) : false; + + if (!DATASETS.includes(dataset)) { + return res.status(400).json({ error: 'Invalid dataset name' }); + } + + const fileName = `${dataset}_${withContext ? 'with_context' : 'no_context'}.zip`; + const filePath = join(DATA_DIR, fileName); + + try { + res.download(filePath); + } catch (error) { + console.error('Error serving file:', error); + res.status(500).json({ error: 'Error serving file' }); + } +}); + +export default router; diff --git a/src/routes/index.js b/src/routes/index.js index 1aeab9c..507f994 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,4 +1,6 @@ import { Router } from 'express'; +import datasetRoutes from './datasets.js'; + const router = Router(); // Routes @@ -11,4 +13,7 @@ router.get('/api/hello', (_req, res) => { res.json({ message: 'Hello from the backend!' }); }); +// Dataset routes +router.use('/datasets', datasetRoutes); + export default router; diff --git a/src/utils/paths.js b/src/utils/paths.js new file mode 100644 index 0000000..ed828aa --- /dev/null +++ b/src/utils/paths.js @@ -0,0 +1,11 @@ +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// Get the project root directory (2 levels up from src/utils) +export const PROJECT_ROOT = join(__dirname, '../..'); + +// Helper function to create paths relative to project root +export const getProjectPath = (relativePath) => join(PROJECT_ROOT, relativePath); \ No newline at end of file