Skip to content

Commit

Permalink
shitty backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dennistimmermann committed Jun 15, 2016
1 parent f417d48 commit bae21e6
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 1 deletion.
11 changes: 11 additions & 0 deletions config.js
@@ -0,0 +1,11 @@
module.exports = {
express: {
port: 4000
},
rethinkdb: {
host: 'localhost',
port: 28015,
authKey: '',
db: 'szenograph'
},
};
12 changes: 12 additions & 0 deletions documentation/examples.js
@@ -0,0 +1,12 @@
[{
id: "270f74c7-0081-434d-9048-e6394af04129",
name: "Die Le von Malta",
img_url: "http://fjhdfjhdjhfjhdf.org.xxx",
description: "Le Ipsum",
input: [ids von iputs],
outputs: [ids von outputs],
tags: ["le", "van", "tran"]
},
{
...
}]
62 changes: 62 additions & 0 deletions documentation/schema.js
@@ -0,0 +1,62 @@
[{
id: '906bfc5a-5cbb-4486-b0a0-1abc3603721c',
name: 'Kamera',
type: 'sensor',
description: '',
content: '',
tags: ['video', 'auge', 'optik', 'camera', 'cam', 'vision'],
examples: [],
items: []
},
{
id: '66919f4f-c89d-4b50-ab25-7b23e3f03ae1',
name: 'Speedometer',
type: 'attribute',
description: '',
content: '',
tags: ['geschwindigkeit', 'speed', 'v', 'radar', 'schnell'],
examples: [],
items: []
},
{
id: 'd34776da-2b13-4a8b-b724-950c38890784',
name: 'Geschwindigkeit',
type: 'attribute',
description: 'Geschwindigkeit beschreibt, wie schnell ein Objekt oder ein Phänomen über die Zeit seine Position verändert.',
content: '',
tags: ['speed', 'v', 'velocity', 'schnell'],
examples: [],
items: [
'906bfc5a-5cbb-4486-b0a0-1abc3603721c', //Kamera
'66919f4f-c89d-4b50-ab25-7b23e3f03ae1' //Speedometer
]
},
{
id: 'ee3afb4a-2ad0-4850-b990-d0df6c2b1475',
name: 'Bewegung',
type: 'input',
description: 'Bewegung ist die positionelle Veränderung eines Objektes im Raum.',
content: '',
tags: [],
examples: [
"Springen",
"Stehen",
"Sitzen",
"Winken",
"Beugen der Arme",
"Beugen der Beine",
"Beugen des Oberkörpers",
"Beugen des Unterkörpers",
"Hüftbewegungen",
"Beugen und Strecken aller Fingerglieder",
"Synchronisation"
],
items: [
'd34776da-2b13-4a8b-b724-950c38890784', //Geschwindigkeit
'xxx', //Beschleunigung
'xxx' //Position im Raum
]
}]


// types: inpput, attribute, sensor
176 changes: 176 additions & 0 deletions index.js
@@ -0,0 +1,176 @@
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var fs = require('fs')
var mustache = require('mustache')
var r = require('rethinkdb')
var uuid = require('uuid')
var _ = require('lodash')
var config = require(__dirname + '/config.js') // TODO path this
// var template = mustache.compile('string of mustache');
// var result = template(locals);

console.log('welcome to the szenograph backend')
app.use(express.static(__dirname + '/public'))
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(bodyParser.json());
app.use(function(req, res, next) {
r.connect(config.rethinkdb, function(err, conn) {
if(err) return next(err)

req.app._rdbConn = conn
next()
})
})

// adding template engine
app.engine('mu', function(filePath, options, callback) {
fs.readFile(filePath, function(err, content) {
if(err) return callback(new Error(err))

var rendered = mustache.render(content.toString(), options.view, options.partials)
return callback(null, rendered)
})
})
app.set('views', './views')
app.set('view engine', 'mu')

app.get('/', function (req, res) {
res.send('Hello World!');
})

app.get('/lexikon', function (req, res) {
//
})

app.get('/examples', function (req, res) {
//
})

app.get('/configurator', function (req, res) {
//
})

app.get('/about', function (req, res) {
//
})

// app.get('/admin/input', function(req, res) {
//
// })
app.route('/admin/inputs')
.get(function(req, res, next) {
console.log('imma here')
r.table('inputs').run(req.app._rdbConn, function(err, cursor) {
if(err) return next(err)

cursor.toArray(function(err, result) {
if(err) return next(err)

// res.json(result)
res.render('admin_inputs', {
'view': {
list: result
}
})
})
})
})
var getById = function(id) {
r.table('inputs').get(id).run(req.app._rdbConn, function(err, result) {
if(err) return next(err)
res.json(result)
})
}
var getAll = function() {
r.table('inputs').run(req.app._rdbConn, function(err, cursor) {
if(err) return next(err)
res.json(result)
})
}
var showInputDetail = function(req, res, callback) {
r.table('inputs').run(req.app._rdbConn, function(err, cursor) {
if(err) return next(err)

cursor.toArray(function(err, result) {
if(err || !result) return next(err)

requested = _.find(result, { 'id': req.params.id})
if(requested == undefined) return next(new Error('id not found'))

requested.list = _.map(result, function(value, key) {
value.checked = _.includes(requested.items, value.id)
return value
})

requested.selected = []
requested.selected[requested.type] = true

// console.log(requested)

callback(requested)
})
})
}
app.route('/admin/inputs/add')
.get(function(req, res, next) {
var n = {id: uuid.v4()}
r.table('inputs').insert(n, {returnChanges: true}).run(req.app._rdbConn, function(err, result) {
if(err) return next(err)
res.redirect(302, '/admin/inputs/'+n.id)
// res.send('hi')
// showInputDetail(req, res, function(requested) {
// res.render('admin_inputs_detail', {
// 'view': {
// data: requested,
// debug: JSON.stringify(result.changes[0].new_val)
// }
// })
// })
})
})
app.route('/admin/inputs/:id')
.get(function(req, res, next) {
showInputDetail(req, res, function(requested) {
res.render('admin_inputs_detail', {
'view': {
data: requested
}
})
})
})
.post(function(req, res, next) {
console.log(req.body)
req.body.tags = _.trim(req.body.tags, ',').split(',')
req.body.examples = _.trim(req.body.examples, ',').split(',')
console.log(req.body)
r.table('inputs').get(req.params.id).update(req.body, {returnChanges: true}).run(req.app._rdbConn, function(err, result) {
if(err) return next(err)

// res.json(result.changes[0].new_val);
showInputDetail(req, res, function(requested) {
res.render('admin_inputs_detail', {
'view': {
data: requested,
debug: JSON.stringify(result)
}
})
})
});
// console.log(req.params)
// res.send(req.params.items)
})


// app.route('/admin/inputs/:id')
// .get()
// .put()
// .delete()

app.use(function(req, res, next) {
req.app._rdbConn.close()
})

app.listen(config.express.port, function () {
console.log('szenograph listening on port',config.express.port);
});
10 changes: 9 additions & 1 deletion package.json
Expand Up @@ -15,5 +15,13 @@
"bugs": {
"url": "https://github.com/dennistimmermann/mid-szenograph/issues"
},
"homepage": "https://github.com/dennistimmermann/mid-szenograph#readme"
"homepage": "https://github.com/dennistimmermann/mid-szenograph#readme",
"dependencies": {
"body-parser": "^1.15.1",
"express": "^4.13.4",
"lodash": "^4.13.1",
"mustache": "^2.2.1",
"nano": "^6.2.0",
"rethinkdb": "^2.3.2"
}
}
Binary file not shown.
25 changes: 25 additions & 0 deletions rethinkdb_data/log_file
@@ -0,0 +1,25 @@
2016-06-15T05:41:50.778695000 0.539663s notice: Recursively removing directory /Users/dennis/Repositories/mid-szenograph/rethinkdb_data/tmp
2016-06-15T05:41:50.782166000 0.543136s notice: Initializing directory /Users/dennis/Repositories/mid-szenograph/rethinkdb_data
2016-06-15T05:41:50.782300000 0.543265s info: Creating a default database for your convenience. (This is because you ran 'rethinkdb' without 'create', 'serve', or '--join', and the directory '/Users/dennis/Repositories/mid-szenograph/rethinkdb_data' did not already exist or is empty.)
2016-06-15T05:41:50.782335000 0.543299s notice: Running rethinkdb 2.3.4 (CLANG 7.3.0 (clang-703.0.31))...
2016-06-15T05:41:50.792473000 0.553439s notice: Running on Darwin 15.0.0 x86_64
2016-06-15T05:41:50.792543000 0.553507s notice: Loading data from directory /Users/dennis/Repositories/mid-szenograph/rethinkdb_data
2016-06-15T05:41:50.866400000 0.627366s info: Automatically using cache size of 100 MB
2016-06-15T05:41:50.866421000 0.627385s warn: Cache size does not leave much memory for server and query overhead (available memory: 704 MB).
2016-06-15T05:41:50.866429000 0.627394s warn: Cache size is very low and may impact performance.
2016-06-15T05:41:50.867415000 0.628379s notice: Listening for intracluster connections on port 29015
2016-06-15T05:41:50.869144000 0.630108s notice: Listening for client driver connections on port 28015
2016-06-15T05:41:50.869312000 0.630276s notice: Listening for administrative HTTP connections on port 8080
2016-06-15T05:41:50.869340000 0.630304s notice: Listening on cluster addresses: 127.0.0.1, ::1
2016-06-15T05:41:50.869349000 0.630313s notice: Listening on driver addresses: 127.0.0.1, ::1
2016-06-15T05:41:50.869352000 0.630316s notice: Listening on http addresses: 127.0.0.1, ::1
2016-06-15T05:41:50.869357000 0.630321s notice: To fully expose RethinkDB on the network, bind to all addresses by running rethinkdb with the `--bind all` command line option.
2016-06-15T05:41:50.869363000 0.630327s notice: Server ready, "macbook_5jo" dae26c80-bd28-4ed7-b67a-f1eb5370754d
2016-06-15T05:43:20.035508000 89.792638s info: Table 40f54b0e-230d-463e-8b51-1a0f1e71aa23: Starting a new Raft election for term 1.
2016-06-15T05:43:20.041504000 89.798634s info: Table 40f54b0e-230d-463e-8b51-1a0f1e71aa23: Added replica on this server.
2016-06-15T05:43:20.052683000 89.809813s info: Table 40f54b0e-230d-463e-8b51-1a0f1e71aa23: This server is Raft leader for term 1. Latest log index is 0.
2016-06-15T05:52:17.181030000 626.915562s notice: Server got SIGINT from pid 25465, uid 501; shutting down...
2016-06-15T05:52:17.181891000 626.916423s notice: Shutting down client connections...
2016-06-15T05:52:17.184883000 626.919415s notice: All client connections closed.
2016-06-15T05:52:17.184897000 626.919428s notice: Shutting down storage engine... (This may take a while if you had a lot of unflushed data in the writeback cache.)
2016-06-15T05:52:17.191834000 626.926366s notice: Storage engine shut down.
Binary file added rethinkdb_data/metadata
Binary file not shown.
28 changes: 28 additions & 0 deletions views/admin_inputs.mu
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Admin/inputs</title>
</head>
<body>
<table>
{{#list}}
<tr>
<td>
<a href="/admin/inputs/{{id}}">> {{name}}</a>
</td>
<td>
{{type}}
</td>
<td>
{{id}}
</td>
<td>
<a href="/admin/inputs/{{id}}/delete">[X]</a>
</td>
</tr>
{{/list}}
</table>
<br><br><a href="/admin/inputs/add">add new</a>
</body>
</html>
56 changes: 56 additions & 0 deletions views/admin_inputs_detail.mu
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Admin/inputs/detail</title>
</head>
<body>
{{debug}}
<form class="" action="/admin/inputs/{{data.id}}" method="post">
<table>
<tr>
<td>id</td>
<td>{{data.id}}</td>
</tr>
<tr>
<td>name</td>
<td><input type="text" name="name" value="{{data.name}}"></td>
</tr>
<tr>
<td>type</td>
<td><select name="type">
<option value="none" {{#data.selected.none}}selected{{/data.selected.none}}>none</option>
<option value="input" {{#data.selected.input}}selected{{/data.selected.input}}>input</option>
<option value="attribute" {{#data.selected.attribute}}selected{{/data.selected.attribute}}>attribute</option>
<option value="sensor" {{#data.selected.sensor}}selected{{/data.selected.sensor}}>sensor</option>
</select></td>
</tr>
<tr>
<td>description</td>
<td><textarea name="description">{{data.description}}</textarea></td>
</tr>
<tr>
<td>content</td>
<td><textarea name="content">{{data.content}}</textarea></td>
</tr>
<tr>
<td>tags</td>
<td><input type="text" name="tags" value="{{#data.tags}}{{.}},{{/data.tags}}"></td>
</tr>
<tr>
<td>examples</td>
<td><input type="text" name="examples" value="{{#data.examples}}{{.}},{{/data.examples}}"></td>
</tr>
<tr>
<td>children</td>
<td>
{{#data.list}}
<input type="checkbox" name="items[]" value="{{id}}" {{#checked}}checked{{/checked}}>{{type}} - {{name}} - {{id}}<br>
{{/data.list}}
</td>
</tr>
</table>
<input type="submit" value="save">
</form>
</body>
</html>
Empty file added views/datainput.htm
Empty file.

0 comments on commit bae21e6

Please sign in to comment.