Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use routing and deliver HTML.

  • Loading branch information...
commit b681d8da3b5ef40dc654c8b66fa526034b9d5aec 1 parent 33af0ae
Nikhil Marathe authored
Showing with 41 additions and 16 deletions.
  1. +41 −16 server.js
57 server.js
View
@@ -117,22 +117,47 @@ function handleFile(request, response) {
});
};
-http.createServer( function(request, response) {
- if( request.method != 'POST' && request.method != 'PUT' ) {
- getOut(405, 'PUT or POST Only', response);
- return;
- }
+var pageTemplate = _.template(fs.readFileSync('view.html', 'utf8'));
- var path = url.parse(request.url).pathname;
- if( path === '/faces/file' ) {
- handleFile(request, response);
- return;
- }
+function viewInBrowser(request, response, id) {
+ var r = redis.createClient();
+ r.on('connected', function() {
+ r.get('mugshot:image:' + id, function(err, path) {
+ if( err ) {
+ response.writeHead(404, { "content-type": "text/html" });
+ response.end("No such image.");
+ }
+ else {
+ response.writeHead(200, { "content-type": "text/html" });
+ response.write(pageTemplate({ image_url: 'http://localhost:5000/image/' + require('path').basename(path.toString()), faces: JSON.stringify(face.faces(path.toString())) }));
+ response.end();
+ }
+ });
+ });
+}
- if( path === '/faces/url' ) {
- handleUrl(request, response);
- return;
- }
+var images = new servant.Servant('/tmp/data');
+var static = new servant.Servant('.');
+var router = choreo.router();
+
+router.get('/view/*', viewInBrowser);
+
+router.post('/file', handleFile);
+
+router.post('/url', handleUrl);
+
+router.get('/image/*', function(request, response) {
+ request.url = request.url.replace(/^\/image/, '');
+ images.serve(request, response);
+});
+
+router.notFound(function(request, response) {
+ static.serve(request, response, function(err, obj) {
+ if(err) {
+ response.writeHead(404);
+ response.end("Page Not Found.");
+ }
+ });
+});
- getOut(404, response);
-}).listen(process.argv[2] || 80);
+http.createServer(router).listen(process.argv[2]||5000);
Please sign in to comment.
Something went wrong with that request. Please try again.