From afe1d124edf9a3f1de9b22b6e518d46ace02da50 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Tue, 5 May 2015 11:56:35 +0200 Subject: [PATCH] Initial commit of nodejs --- CNAME | 1 - HermiteTest/index.html | 11 ----- README.md | 2 - _config.yml | 3 -- app.js | 42 ++++++++++++++++++ bouncing/index.md | 11 ----- controllers/bouncing/index.js | 11 +++++ controllers/bouncing/urls.js | 3 ++ controllers/index/index.js | 10 +++++ controllers/index/urls.js | 3 ++ controllers/multisphere/index.js | 10 +++++ controllers/multisphere/urls.js | 3 ++ controllers/prototype/index.js | 34 +++++++++++++++ controllers/prototype/urls.js | 5 +++ controllers/stream/index.js | 28 ++++++++++++ controllers/stream/urls.js | 3 ++ index.md | 31 ------------- index/index.js | 10 +++++ lib/boot.js | 37 ++++++++++++++++ multisphere/index.md | 12 ------ my_modules/filterInt.js | 6 +++ prototype/arrows/index.md | 7 --- prototype/index.md | 12 ------ prototype/viewports/index.md | 7 --- stream/index.md | 14 ------ urls.js | 7 +++ 404.html => views/404.html | 10 ++--- views/bouncing/index.html | 11 +++++ views/index.html | 43 +++++++++++++++++++ {_layouts => views}/main.html | 20 ++++----- views/multisphere/index.html | 13 ++++++ views/prototype/index.html | 14 ++++++ .../prototype/prototype.html | 13 +++--- views/stream/index.html | 16 +++++++ {_layouts => views}/withjs.html | 11 ++--- 35 files changed, 336 insertions(+), 138 deletions(-) delete mode 100644 CNAME delete mode 100644 HermiteTest/index.html delete mode 100644 README.md delete mode 100644 _config.yml create mode 100644 app.js delete mode 100644 bouncing/index.md create mode 100644 controllers/bouncing/index.js create mode 100644 controllers/bouncing/urls.js create mode 100644 controllers/index/index.js create mode 100644 controllers/index/urls.js create mode 100644 controllers/multisphere/index.js create mode 100644 controllers/multisphere/urls.js create mode 100644 controllers/prototype/index.js create mode 100644 controllers/prototype/urls.js create mode 100644 controllers/stream/index.js create mode 100644 controllers/stream/urls.js delete mode 100644 index.md create mode 100644 index/index.js create mode 100644 lib/boot.js delete mode 100644 multisphere/index.md create mode 100644 my_modules/filterInt.js delete mode 100644 prototype/arrows/index.md delete mode 100644 prototype/index.md delete mode 100644 prototype/viewports/index.md delete mode 100644 stream/index.md create mode 100644 urls.js rename 404.html => views/404.html (55%) create mode 100644 views/bouncing/index.html create mode 100644 views/index.html rename {_layouts => views}/main.html (77%) create mode 100644 views/multisphere/index.html create mode 100644 views/prototype/index.html rename _layouts/prototype.md => views/prototype/prototype.html (89%) create mode 100644 views/stream/index.html rename {_layouts => views}/withjs.html (92%) diff --git a/CNAME b/CNAME deleted file mode 100644 index 4d7d667..0000000 --- a/CNAME +++ /dev/null @@ -1 +0,0 @@ -3dinterface.no-ip.org diff --git a/HermiteTest/index.html b/HermiteTest/index.html deleted file mode 100644 index b24c4df..0000000 --- a/HermiteTest/index.html +++ /dev/null @@ -1,11 +0,0 @@ ---- -layout: withjs -title: Hermite Test -extrajs: ---- -
-
-        
-
-
- diff --git a/README.md b/README.md deleted file mode 100644 index 2cb3cb3..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# 3D Interface -A prototype for a user-friendly 3D interface allowing to browse 3D scenes / models diff --git a/_config.yml b/_config.yml deleted file mode 100644 index 49de929..0000000 --- a/_config.yml +++ /dev/null @@ -1,3 +0,0 @@ -gems: - - jekyll-redirect-from - - jemoji diff --git a/app.js b/app.js new file mode 100644 index 0000000..9abea75 --- /dev/null +++ b/app.js @@ -0,0 +1,42 @@ +var http = require('http'); +var express = require('express'); +var pejs = require('pejs'); +var module = require('./my_modules/filterInt'); + +var app = express(); +var views = pejs(); + +var urls = require('./urls'); + +app.set('view engine', 'pejs'); + +app.use(function(req, res, next) { + res.locals.title = "3DUI"; + res.locals.urls = urls; + next(); +}); + +// Load controllers +require('./lib/boot')(app, { verbose: !module.parent }); + +app.use('/static', express.static('static')); + +app.use(function(err, req, res, next) { + if (err.status === 404) { + res.setHeader('Content-Type', 'text/html'); + + views.render('404', res.locals, function(err, result) { + res.send(result); + }); + } +}); + +app.use(function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + views.render('404', res.locals, function(err, result) { + res.send(result); + }); +}); + +app.listen(4000); diff --git a/bouncing/index.md b/bouncing/index.md deleted file mode 100644 index f78a56b..0000000 --- a/bouncing/index.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -layout: withjs -title: Bouncing cube -extrajs: ---- -## Bouncing cube - -Click on the cube to make it jump ! - -
- diff --git a/controllers/bouncing/index.js b/controllers/bouncing/index.js new file mode 100644 index 0000000..7645fa6 --- /dev/null +++ b/controllers/bouncing/index.js @@ -0,0 +1,11 @@ +var pejs = require('pejs'); +views = pejs(); + +module.exports.index = function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + views.render('bouncing', res.locals, function(err, result) { + console.log(err); + res.send(result); + }); +} diff --git a/controllers/bouncing/urls.js b/controllers/bouncing/urls.js new file mode 100644 index 0000000..1288727 --- /dev/null +++ b/controllers/bouncing/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/bouncing' : 'index' +} diff --git a/controllers/index/index.js b/controllers/index/index.js new file mode 100644 index 0000000..8808a55 --- /dev/null +++ b/controllers/index/index.js @@ -0,0 +1,10 @@ +var pejs = require('pejs'); +views = pejs(); + +module.exports.index = function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + views.render('index', res.locals, function(err, result) { + res.send(result); + }); +} diff --git a/controllers/index/urls.js b/controllers/index/urls.js new file mode 100644 index 0000000..6ebdf66 --- /dev/null +++ b/controllers/index/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/': 'index' +} diff --git a/controllers/multisphere/index.js b/controllers/multisphere/index.js new file mode 100644 index 0000000..4e0332d --- /dev/null +++ b/controllers/multisphere/index.js @@ -0,0 +1,10 @@ +var pejs = require('pejs'); +views = pejs(); + +module.exports.index = function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + views.render('multisphere', res.locals, function(err, result) { + res.send(result); + }); +} diff --git a/controllers/multisphere/urls.js b/controllers/multisphere/urls.js new file mode 100644 index 0000000..e3206ed --- /dev/null +++ b/controllers/multisphere/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/multisphere': 'index' +} diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js new file mode 100644 index 0000000..a80d240 --- /dev/null +++ b/controllers/prototype/index.js @@ -0,0 +1,34 @@ +var express = require('express') +var pejs = require('pejs'); + +app = express(); +views = pejs(); + +module.exports.index = function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + views.render('prototype', res.locals, function(err, result) { + res.send(result); + }); +} + +module.exports.arrows = function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + res.locals.extrajs = ''; + + views.render('prototype/prototype', res.locals, function(err, result) { + console.log(err); + res.send(result); + }); +} + +module.exports.viewports = function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + res.locals.extrajs = ''; + + views.render('prototype/prototype', res.locals, function(err, result) { + res.send(result); + }); +} diff --git a/controllers/prototype/urls.js b/controllers/prototype/urls.js new file mode 100644 index 0000000..49b2c93 --- /dev/null +++ b/controllers/prototype/urls.js @@ -0,0 +1,5 @@ +module.exports = { + '/prototype': 'index', + '/prototype/arrows': 'arrows', + '/prototype/viewports': 'viewports' +} diff --git a/controllers/stream/index.js b/controllers/stream/index.js new file mode 100644 index 0000000..5d25965 --- /dev/null +++ b/controllers/stream/index.js @@ -0,0 +1,28 @@ +var pejs = require('pejs'); +var tools = require('../../my_modules/filterInt') + +views = pejs(); + +module.exports.index = function(req, res, next) { + + // Parse get argument res + res.locals.resolution = req.params.res; + + if (res.locals.resolution === undefined) { + res.locals.resolution = 5; + } else { + res.locals.resolution = tools.filterInt(res.locals.resolution); + } + + if (isNaN(res.locals.resolution) || res.locals.resolution < 1 || res.locals.resolution > 25) { + var error = new Error("Resolution was not set properly"); + error.status = 404; + next(error); + return; + } + + res.setHeader('Content-Type', 'text/html'); + views.render('stream/index.html', res.locals, function(err, result) { + res.send(result); + }); +} diff --git a/controllers/stream/urls.js b/controllers/stream/urls.js new file mode 100644 index 0000000..93c5723 --- /dev/null +++ b/controllers/stream/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/stream/:res?': 'index' +}; diff --git a/index.md b/index.md deleted file mode 100644 index 43c892a..0000000 --- a/index.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -layout: main -title: Index ---- - -## Index - - [A bouncing cube that jumps when you click on it](/bouncing/) - - Jumps and bounce when you click on it :smiley: - - - [Sphere with multi-resolution](/multisphere/) - - Lots of obj files loaded and displayed. When you click - somewhere, the current obj is hidden and the next one, with a - better resolution is shown. - - - [A proto of the real thing](/prototype/) - - You can move the camera with the arrow keys and move the angle of the camera - with 2, 4, 6 and 8 (the arrows of the numpad), or you can do a drag-and-drop - like (click on the mouse to grap the scene, and move the mouse to rotate the - camera). You can also select a camera by clicking on the red part of it, and - get back to the free camera by clicking again. You can also select a camera by - simply clicking on the object you want to see. The program will choose the - camera that you want, and move to it progressively. - - - [Streaming simulation](/stream/) - - A mesh of a sphere is fully loaded, - and displayed progressively. This test is here to prove that we can dynamically - add vertices and faces to a mesh. diff --git a/index/index.js b/index/index.js new file mode 100644 index 0000000..4fd522b --- /dev/null +++ b/index/index.js @@ -0,0 +1,10 @@ +module.exports = function(app, urls) { + app.get(urls.index, function(req, res) { + res.setHeader('Content-Type', 'text/html'); + + views.render('index', res.locals, function(err, result) { + console.log(err); + res.send(result); + }); + }); +} diff --git a/lib/boot.js b/lib/boot.js new file mode 100644 index 0000000..d9b0642 --- /dev/null +++ b/lib/boot.js @@ -0,0 +1,37 @@ +/** + * Module dependencies. + */ + +var express = require('express'); +var fs = require('fs'); + +module.exports = function(parent, options){ + var verbose = options.verbose; + fs.readdirSync(__dirname + '/../controllers').forEach(function(name){ + + // index.js in controller, with function as pages (views.py for django) + var obj = require('./../controllers/' + name + '/index'); + + // urls.js, just like django urls.py + var urls = require('./../controllers/' + name + '/urls'); + var name = obj.name || name; + var app = express(); + + // allow specifying the view engine + if (obj.engine) app.set('view engine', obj.engine); + app.set('views', __dirname + '/../controllers/' + name + '/views'); + + // generate routes based + // on the exported methods + + verbose && console.log('\t' + name + ':'); + for (var key in urls) { + app.get(key, obj[urls[key]]); + console.log('\t\t' + key + ' -> ' + name + '.' + urls[key]); + } + console.log(); + + // mount the app + parent.use(app); + }); +}; diff --git a/multisphere/index.md b/multisphere/index.md deleted file mode 100644 index 3b2e293..0000000 --- a/multisphere/index.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -layout: withjs -title: Multi-sphere -extrajs: ---- -## Multiresolution sphere - -This is the first test of multi-resolution. In fact, it's not really one -multi-resolution sphere but many spheres with different resolutions. You can -change resolution by clicking on the canvas. - -
diff --git a/my_modules/filterInt.js b/my_modules/filterInt.js new file mode 100644 index 0000000..c966cbb --- /dev/null +++ b/my_modules/filterInt.js @@ -0,0 +1,6 @@ +// Strict parseInt +module.exports.filterInt = function(value) { + if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) + return Number(value); + return NaN; +} diff --git a/prototype/arrows/index.md b/prototype/arrows/index.md deleted file mode 100644 index 4d2a1f3..0000000 --- a/prototype/arrows/index.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: Prototype with old cameras -layout: prototype -extrajs: -extrahead: ---- -{{ content }} diff --git a/prototype/index.md b/prototype/index.md deleted file mode 100644 index fb4efde..0000000 --- a/prototype/index.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -layout: main -title: Prototypes ---- - -## Index -There are two prototypes here : - - - [One with arrows](arrows/) - - - [One with viewports](viewports/) - diff --git a/prototype/viewports/index.md b/prototype/viewports/index.md deleted file mode 100644 index b52fba1..0000000 --- a/prototype/viewports/index.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: Prototype with old cameras -layout: prototype -extrajs: -extrahead: ---- -{{ content }} diff --git a/stream/index.md b/stream/index.md deleted file mode 100644 index e5132f8..0000000 --- a/stream/index.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -title: Streaming simulator -layout: withjs -extrajs: - ---- -## Streaming simulator - -In fact, it's not really streaming. The sphere is fully preloaded and then, a -mesh is created and vertices and faces are dynamically added to this mesh as -time goes by. - -
- diff --git a/urls.js b/urls.js new file mode 100644 index 0000000..ed177cb --- /dev/null +++ b/urls.js @@ -0,0 +1,7 @@ +module.exports.index = "/"; +module.exports.bouncing = '/bouncing/'; +module.exports.multisphere = '/multisphere/'; +module.exports.prototype = '/prototype/'; +module.exports.arrows = '/prototype/arrows/'; +module.exports.viewports = '/prototype/viewports/'; +module.exports.stream = '/stream/'; diff --git a/404.html b/views/404.html similarity index 55% rename from 404.html rename to views/404.html index 63f0c1b..21a06ad 100644 --- a/404.html +++ b/views/404.html @@ -1,10 +1,10 @@ ---- -layout: main -title: 404 ---- +<%{ 'main' }%> +<%{ title %>404<%} %> +<%{ content %>

Error 404

- There is nothing there... maybe you want to go back to the index + There is nothing there... maybe you want to go back to the index

+<%} %> diff --git a/views/bouncing/index.html b/views/bouncing/index.html new file mode 100644 index 0000000..66b06f8 --- /dev/null +++ b/views/bouncing/index.html @@ -0,0 +1,11 @@ +<%{ '../withjs' }%> +<%{ title %>Bouncing cube<%} %> +<%{ extrajs %><%} %> + +<%{ content %> +

Bouncing cube

+ +

Click on the cube to make it jump !

+ +
+<%} %> diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..9c675ea --- /dev/null +++ b/views/index.html @@ -0,0 +1,43 @@ +<%{ './main' }%> + +<%{ title %>Index<%} %> + +<%{ content %> +

Index

+ +<%} %> diff --git a/_layouts/main.html b/views/main.html similarity index 77% rename from _layouts/main.html rename to views/main.html index 272cac4..38ebf0c 100644 --- a/_layouts/main.html +++ b/views/main.html @@ -7,8 +7,8 @@ - {{ page.extrahead }} - 3DUI - {{ page.title }} + <%{{ extrahead }}%> + <%= title %> - <%{{ title }}%>
- {{ content }} + <%{{ content }}%>
- {{ page.js }} - {{ page.extrajs }} + <%{{ js }}%> + <%{{ extrajs }}%> diff --git a/views/multisphere/index.html b/views/multisphere/index.html new file mode 100644 index 0000000..dfa40e4 --- /dev/null +++ b/views/multisphere/index.html @@ -0,0 +1,13 @@ +<%{ '../withjs' }%> +<%{ title %>Multi-sphere<%} %> +<%{ extrajs %><%} %> + +<%{ content %> +

Multiresolution sphere

+ +

This is the first test of multi-resolution. In fact, it's not really one +multi-resolution sphere but many spheres with different resolutions. You can +change resolution by clicking on the canvas.

+ +
+<%} %> diff --git a/views/prototype/index.html b/views/prototype/index.html new file mode 100644 index 0000000..ffa562f --- /dev/null +++ b/views/prototype/index.html @@ -0,0 +1,14 @@ +<%{ '../main' }%> +<%{ title %>Prototypes<%} %> +<%{ content %> + +

Index

+There are two prototypes here : + + +<%} %> + diff --git a/_layouts/prototype.md b/views/prototype/prototype.html similarity index 89% rename from _layouts/prototype.md rename to views/prototype/prototype.html index d09b108..8354aa9 100644 --- a/_layouts/prototype.md +++ b/views/prototype/prototype.html @@ -1,8 +1,10 @@ ---- -title: Prototype with old cameras -layout: withjs -extrahead: ---- +<%{ '../withjs' }%> + +<%{ title %>Prototype with old cameras<%} %> +<%{ extrajs %><%- extrajs %><%} %> +<%{ extrahead %><%} %> + +<%{ content %>

3D Interface

@@ -43,3 +45,4 @@ button.
+<%} %> diff --git a/views/stream/index.html b/views/stream/index.html new file mode 100644 index 0000000..dd5e580 --- /dev/null +++ b/views/stream/index.html @@ -0,0 +1,16 @@ +<%{ '../withjs' }%> +<%{ title %>Streaming simulator<%} %> +<%{ extrajs %> + + +<%} %> + +<%{ content %> +

Streaming simulator

+ +

In fact, it's not really streaming. The sphere is fully preloaded and then, a +mesh is created and vertices and faces are dynamically added to this mesh as +time goes by.

+ +
+<%} %> diff --git a/_layouts/withjs.html b/views/withjs.html similarity index 92% rename from _layouts/withjs.html rename to views/withjs.html index 574d382..30d50b9 100644 --- a/_layouts/withjs.html +++ b/views/withjs.html @@ -1,6 +1,6 @@ ---- -layout: main -js: +<%{ 'main.html' }%> +<%{ js %> + @@ -19,7 +19,4 @@ js: ---- -{{ content }} - - +<%} %>