router.js
1 import { config } from './configParser.js' 2 import express from 'express' 3 import path from 'path' 4 import bodyParser from 'body-parser' 5 import state from './state.js' 6 import spec from './spec.js' 7 import fobtap from './fobtap.js' 8 import calculations from '../calculations.js' 9 import auth from './auth.js' 10 import openAo from './openAo.js' 11 import cors from 'cors' 12 13 const { serverAuth } = auth 14 15 import { fileURLToPath } from 'url' 16 const __dirname = path.dirname(fileURLToPath(import.meta.url)) 17 18 export default function applyRouter(app){ 19 app.use(cors()) 20 app.use(express.static(path.join(__dirname, '../../dist'))) 21 app.use(express.static(path.join(__dirname, '../../public'))) 22 if (config.open === true){ 23 app.use(openAo) 24 } 25 app.get('/*', (req, res) => { 26 res.sendFile(path.join(__dirname, '../../dist/index.html')) 27 }) 28 app.use(serverAuth) 29 app.use(bodyParser.json()) 30 app.use(bodyParser.urlencoded({ 31 extended: true 32 })) 33 app.use(spec) // event creation 34 app.use(fobtap) // rfid scan 35 app.post('/state', (req, res) => { 36 let xd = { 37 hashMap: state.pubState.hashMap, 38 ao: state.pubState.ao, 39 sessions: state.pubState.sessions, 40 members: state.pubState.members, 41 tasks: [], 42 resources: state.pubState.resources, 43 cash:state.pubState.cash 44 } 45 res.json(xd) 46 }) 47 app.post('/tasks/:taskId', (req, res) => { 48 res.json(state.serverState.tasks) 49 }) 50 app.post('/taskhash/:taskId', (req, res)=> { 51 // dos potential (it's behind auth but still bad?) 52 res.end( calculations.crawlerHash(state.serverState.tasks, req.params.taskId) ) 53 }) 54 }