summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2017-06-30 20:17:24 +0200
committerJules Laplace <julescarbon@gmail.com>2017-06-30 20:17:24 +0200
commit7c5ca9fdcafcec0e3781ed856eba42cd608f0e15 (patch)
treea4425e8ce03538c1741e6c10b0a2459bcb03aea2 /lib
parent82cf40b516b5ab11c34b3642a01603ec1b590c9f (diff)
es6 stuff
Diffstat (limited to 'lib')
-rw-r--r--lib/bridge/index.js25
-rw-r--r--lib/db/index.js23
-rw-r--r--lib/server/index.js116
3 files changed, 141 insertions, 23 deletions
diff --git a/lib/bridge/index.js b/lib/bridge/index.js
new file mode 100644
index 0000000..e0ab441
--- /dev/null
+++ b/lib/bridge/index.js
@@ -0,0 +1,25 @@
+import { execFile } from 'child_process'
+
+export default class Bridge {
+ constructor() {
+ this.cpus = []
+ this.getCPUs()
+ }
+ getCPUs() {
+ this.run(['python/devices.py']).then( (stdout, stderr) => {
+ this.cpus = JSON.parse(stdout)
+ console.log(this.cpus)
+ }).catch( (err) => {
+ console.error('error fetching cpus:', err)
+ })
+ }
+ run(args) {
+ return new Promise( (resolve, reject) => {
+ console.log('>', args.join(' '))
+ execFile(process.env.PYTHON_BINARY, args, (err, stdout, stderr) => {
+ if (err) return reject(err)
+ return resolve(stdout, stderr)
+ })
+ })
+ }
+} \ No newline at end of file
diff --git a/lib/db/index.js b/lib/db/index.js
index 468d9be..a74ba89 100644
--- a/lib/db/index.js
+++ b/lib/db/index.js
@@ -54,26 +54,3 @@ db.crud = function(type, model) {
},
}
}
-db.getFolders = function(id) {
- var model = new Folder({'id': id})
- return model.fetch()
-}
-db.getFolderFiles = function(id) {
- return File.query(function(qb){
- qb.where('folder_id', '=', id).orderBy("id", "desc")
- }).fetch()
-}
-db.getLatest = function () {
- return Image.query(function(qb){
- qb.orderBy("id", "desc").limit(1)
- }).fetch()
-}
-db.getRandom = function () {
- return Image.query(function(qb){
- qb.orderBy(knex.raw('RAND()')).limit(1)
- }).fetch()
-}
-
-db.createImage = function(url){
- return new Image({ url: url, shorturl: "" }).save()
-} \ No newline at end of file
diff --git a/lib/server/index.js b/lib/server/index.js
new file mode 100644
index 0000000..d062131
--- /dev/null
+++ b/lib/server/index.js
@@ -0,0 +1,116 @@
+var fs = require('fs')
+var app, express = require('express')
+var http = require('http')
+var bodyParser = require('body-parser')
+var path = require('path')
+
+var multer = require('multer')
+var upload = multer({ dest: 'uploads/' })
+
+var app, server
+
+const db = require('../db')
+
+var site = module.exports = {}
+
+site.init = function(bridge){
+ app = express()
+ app.use(express.static(path.join(__dirname, './public')))
+ app.use(bodyParser.json())
+ app.use(bodyParser.urlencoded({ extended: false }))
+
+ app.use(express.query())
+
+ server = http.createServer(app).listen(process.env.PORT, function () {
+ console.log('Cortex listening at http://localhost:%s', server.address().port)
+ })
+
+ const api_folders = crud(app, 'folder', db.Folder, {
+ afterCreate: (folder) => {
+ fs.mkdir('public/data/' + folder.id + '/', function(){
+ console.log('created folder', folder.id, folder.name)
+ })
+ }
+ })
+ const api_files = crud(app, 'file', db.File)
+ const api_jobs = crud(app, 'job', db.Job)
+ const api_tasks = crud(app, 'task', db.Task)
+
+ app.post('/folders/:id', upload.array('file'), function(req, res){
+ if ( ! req.files ) return;
+ let loaded = {};
+ ( req.files || [] ).forEach( (file) => {
+ loaded[file.filename] = false
+ const fn = file.originalname
+ fs.rename(file.path, 'public/data/' + req.params.id + '/' + fn, function(err){
+ api_files.create({
+ // table.string('username')
+ 'folder_id': req.params.id,
+ 'name': fn,
+ 'size': file.size,
+ 'generated': false,
+ 'processed': false,
+ }).then( (file) => {
+ loaded[file.filename] = file.toJSON()
+ if (Object.keys(loaded).some( el => !! el )) {
+ res.json( Object.keys(loaded).map(k=>loaded[k]).sort((a,b) => { b.id - a.id }) )
+ }
+ }).catch( (err) => {
+ console.warn(err)
+ res.sendStatus(500)
+ })
+ })
+ })
+ })
+
+ function crud(app, type_s, model, callbacks){
+ callbacks = callbacks || {}
+ const type = '/' + type_s + 's/'
+ const type_id = type + ':id'
+
+ const crud = db.crud(type_s, model)
+
+ // index
+ app.get(type, (req, res) => {
+ console.log('index', type)
+ crud.index(req.query).then( (data) => {
+ res.json(data ? data.toJSON() : [])
+ }) // }).catch( () => res.sendStatus(500) )
+ })
+
+ // show
+ app.get(type_id, (req, res) => {
+ console.log('show', type, req.params.id)
+ crud.show(req.params.id).then( (data) => {
+ res.json(data.toJSON())
+ }) // .catch( (err) => res.sendStatus(500) )
+ })
+
+ // create
+ app.post(type, (req, res) => {
+ console.log('create', type)
+ crud.create(req.body).then( (data) => {
+ res.json(data.toJSON())
+ callbacks.afterCreate && callbacks.afterCreate(data.toJSON())
+ })// .catch( () => res.sendStatus(500) )
+ })
+
+ // update
+ app.put(type_id, (req, res) => {
+ console.log('update', type, req.params.id)
+ crud.update(req.body.id, req.body).then( (data) => {
+ res.json(data.toJSON())
+ })// .catch( () => res.sendStatus(500) )
+ })
+
+ // destroy
+ app.delete(type_id, (req, res) => {
+ console.log('destroy', type, req.params.id)
+ crud.destroy(req.params.id).then( (data) => {
+ res.json(data.toJSON())
+ })// .catch( () => res.sendStatus(500) )
+ })
+
+ return crud
+ }
+}