Simon Oberhammer 12 years ago
parent
commit
c622d047e1
2 changed files with 7 additions and 24 deletions
  1. 7 21
      ringojs-stick/views.js
  2. 0 3
      ringojs/ringo-main.js

+ 7 - 21
ringojs-stick/views.js

@@ -2,6 +2,7 @@ var sql = require('sql-ringojs-client');
 var {Application} = require("stick");
 var fs = require('fs');
 var {Template} = require('reinhardt/template');
+var response = require('ringo/jsgi/response');
 
 // DO NOT TOUCH THE FOLLOWING LINE.
 // THIS VARIABLE IS REGEX REPLACED BY setup.py
@@ -39,28 +40,17 @@ app.get('/db', function(request) {
       if (queryCount === null) {
          var randId = ((Math.random() * 10000) | 0) + 1
          var world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
-         // let's just hope it gets to this
-         connection.close();
-         return {
-            status: 200,
-            headers: {"Content-Type": "application/json; charset=UTF-8"},
-            body: [JSON.stringify(world)]
-         }
+         return response.json(world);
       } else {
          queryCount = parseInt(queryCount, 10);
-         var body = [];
+         var worlds = [];
          var randId, world;
          for (var i = 0; i < queryCount; i++) {
             randId = ((Math.random() * 10000) | 0) + 1;
             world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
-            body.push(world);
-         }
-         connection.close();
-         return {
-            status: 200,
-            headers: {"Content-Type": "application/json; charset=UTF-8"},
-            body: [JSON.stringify(body)]
+            worlds.push(world);
          }
+         return response.json(worlds);
       }
    } catch (e) {
       connection.close();
@@ -81,11 +71,7 @@ app.get('/fortune', function() {
          message: 'Additional fortune added at request time.'
       });
       fortunes.sort(sortFortunes);
-      return {
-         status: 200,
-         headers: {"Content-Type": "text/html; charset=UTF-8"},
-         body: [fortuneTemplate.render({fortunes: fortunes})]
-      }
+      return response.html(fortuneTemplate.render({fortunes: fortunes}));
    } catch (e) {
       connection.close();
       connection = null;
@@ -94,4 +80,4 @@ app.get('/fortune', function() {
          connection.close();
       }
    }
-});
+});

+ 0 - 3
ringojs/ringo-main.js

@@ -28,8 +28,6 @@ exports.app = function(req) {
          if (queryCount === null) {
             var randId = ((Math.random() * 10000) | 0) + 1
             var world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
-            // let's just hope it gets to this
-            connection.close();
             return {
                status: 200,
                headers: {"Content-Type": "application/json; charset=UTF-8"},
@@ -44,7 +42,6 @@ exports.app = function(req) {
                world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
                body.push(world);
             }
-            connection.close();
             return {
                status: 200,
                headers: {"Content-Type": "application/json; charset=UTF-8"},