Skip to content
Snippets Groups Projects
Select Git revision
  • leo
  • dex
  • pendulum
  • master default protected
  • apfelstruder
  • littlerascal
6 results

cf.js

Blame
  • cf.js 4.84 KiB
    // new year new bootstrap
    
    const express = require('express')
    const app = express()
    // this include lets us read data out of put requests,
    const bodyparser = require('body-parser')
    // our fs tools,
    const fs = require('fs')
    const filesys = require('./filesys.js')
    // and we occasionally spawn local pipes (workers)
    const child_process = require('child_process')
    // will use these to figure where tf we are
    let ownIp = ''
    const os = require('os')
    let ifaces = os.networkInterfaces()
    
    // serve everything: https://expressjs.com/en/resources/middleware/serve-static.html
    app.use(express.static(__dirname))
    // accept post bodies as json,
    app.use(bodyparser.json())
    app.use(bodyparser.urlencoded({extended: true}))
    
    // if these don't exist, they get 'nexted' to any other 'middleware' we write
    app.get('/fileList', (req, res) => {
      try{
        // we would fs/ through our list, and serve that,
        filesys.getDirTree(req.query.path).then((list) => {
          // take the query out of the front of the path, and swap all \ for /
          // rm the first /
          for(i in list){
            if(list[i].charAt(0) == '/'){
              list[i] = list[i].substring(1, list[i].indexOf('.'))
            }
            // a final sweep, for double /
            if(list[i].charAt(0) == '/'){
              list[i] = list[i].substring(1)
            }
          }
          // console.log('sends', list)
          res.send(list)
          // ship up north,
        }).catch((err) => {
          res.send(err)
          // ship err back
        })
      } catch (err) {
        console.log(err)
        res.send('server-side error retrieving list')
      }
    })
    
    let unfjson = (program) => {
    
    }
    
    // we also handle file-saving this way,
    app.post('/save/contexts/:context/:file', (req, res) => {
      // this is probably fine for now, but I kind of want a websocket to do this kind of stuff ?
      let serialized = JSON.stringify(req.body, null, 2)
      // .. one-hack pls:
    
      fs.writeFile(`save/contexts/${req.params.context}/${req.params.file}.json`, serialized, (err) => {
        if (err) {
          console.log(`ERR while saving to save/contexts/${req.params.context}/${req.params.file}.json`)
          res.send({success: false})
        }
        console.log(`saved a context to to save/contexts/${req.params.context}/${req.params.file}.json`)
        res.send({success: true})
      })
    })