aiom_pack
Version:
Framework for interdependent (mcmc-like) behavioral experiments
64 lines (58 loc) • 2.79 kB
JavaScript
// routes/api.js
const express = require('express');
const router = express.Router();
const path = require('path');
const fs = require('fs');
const dotenv = require('dotenv');
class api_router {
constructor(app, config, experimentPath) {
this.app = app;
this.config = config;
this.experiment = config.get('experiment');
this.experimentPath = experimentPath;
this.setupRoutes();
this.setupAddonRoutes();
}
setupRoutes() {
const { Controller } = require(`../controllers/base/${this.experiment}`);
this.taskController = new Controller(this.experimentPath, this.config);
this.initialize_experiment(this.taskController.initialize());
this.app.post("/api/set_table", this.taskController.set_table.bind(this.taskController));
this.app.get('/api/get_choices', this.taskController.get_choices.bind(this.taskController));
this.app.post('/api/register_choices', this.taskController.register_choices.bind(this.taskController));
}
setupAddonRoutes() {
if (this.config.getBoolean('production')) {
const uploadController = require('../controllers/addon/dataUpload');
this.app.post('/api/upload_files', uploadController.upload_files);
}
if (this.config.getBoolean('attention_check')) {
const acController = require('../controllers/addon/attention_check');
this.app.post('/api/register_attentioncheck', acController.register_attentioncheck);
}
if (this.config.getBoolean('categorization')) {
const { CategorizationController } = require('../controllers/addon/categorization');
this.catController = new CategorizationController(this.experimentPath);
this.initialize_experiment(this.catController.setupCategorization());
this.app.get('/api/categorization_stimuli', this.catController.get_stimuli.bind(this.catController));
this.app.post('/api/register_categorization', this.catController.register_choices.bind(this.catController));
}
if (this.config.getBoolean('prolific')) {
const prolificController = require('../controllers/addon/prolific_Controller');
this.app.get('/api/complete_redirect', prolificController.complete_redirect);
} else {
this.app.get('/api/complete_redirect', (req, res) => {
res.status(200).json({ success: true });
});
}
}
async initialize_experiment(func) {
try {
await func;
} catch (error) {
console.error('Failed to initialize:', error);
throw error;
}
};
}
module.exports = { api_router };