UNPKG

aiom

Version:

A Framework for interdependent (mcmc-like) behavioral experiments

57 lines (51 loc) 2.45 kB
// 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.task_order = config.getArray('task_order'); 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.task_order.includes('production')) { const productionController = require('../controllers/addon/production'); this.app.post('/api/production', productionController.production); } if (this.task_order.includes('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('attention_check')) { const acController = require('../controllers/addon/attention_check'); this.app.post('/api/register_attentioncheck', acController.register_attentioncheck); } } async initialize_experiment(func) { try { await func; } catch (error) { console.error('Failed to initialize:', error); throw error; } }; } module.exports = { api_router };