Browse Source

ringojs as a web framework: ringojs-convinient

stick (JSGI routing), ringo-sqlstore and reinhardt (templates) are
my recommended setup for a convinient framework experience with ringo

makes ringojs-stick obsolete
Simon Oberhammer 12 years ago
parent
commit
a81d871d71

+ 0 - 0
ringojs-stick/README.md → ringojs-convinient/README.md


+ 0 - 0
ringojs-stick/__init__.py → ringojs-convinient/__init__.py


+ 43 - 0
ringojs-convinient/app/models.js

@@ -0,0 +1,43 @@
+var {Store, ConnectionPool, Cache} = require('ringo-sqlstore');
+
+// DO NOT TOUCH THE FOLLOWING LINE.
+// THIS VARIABLE IS REGEX REPLACED BY setup.py
+var dbHost = 'localhost';
+
+// create and configure store
+var connectionPool = module.singleton("connectionPool", function() {
+    return new ConnectionPool({
+        "url": "jdbc:mysql://" + dbHost + "/hello_world",
+        "driver": "com.mysql.jdbc.Driver",
+        "username": "benchmarkdbuser",
+        "password": "benchmarkdbpass"
+    });
+});
+var store = exports.store = new Store(connectionPool);
+var entityCache = module.singleton("entityCache", function() {
+    return new Cache(10000);
+});
+var queryCache = module.singleton("queryCache", function() {
+    return new Cache(10000);
+});
+store.setEntityCache(entityCache);
+store.setQueryCache(queryCache);
+
+// define entities in DB
+exports.World = store.defineEntity('World', {
+	table: 'World',
+	properties: {
+		randomNumber: 'integer'
+	}
+});
+
+var Fortune = exports.Fortune = store.defineEntity('Fortune', {
+	table: 'Fortune',
+	properties: {
+		message: 'string'
+	}
+});
+
+Fortune.sort = function(a, b) {
+ return (a.message < b.message) ? -1 : (a.message > b.message) ? 1 : 0;
+};

+ 39 - 0
ringojs-convinient/app/views.js

@@ -0,0 +1,39 @@
+var {Application} = require("stick");
+var fs = require('fs');
+var response = require('ringo/jsgi/response');
+var models = require('./models');
+
+var {Template} = require('reinhardt/template');
+var fortuneTemplate = module.singleton('fortuneTemplate', function() {
+   return new Template(fs.read(module.resolve('../templates/fortunes.reinhardt')));
+});
+
+var app = exports.app = Application();
+app.configure("params", "route");
+
+app.get('/json', function() {
+   var helloObject = {message: "Hello, world"};
+   return response.json(helloObject);
+});
+
+app.get('/db/:queries?', function(request, queries) {
+   queries = parseInt(queries, 10) || 1;
+   var worlds = [];
+   var randId, world;
+   for (var i = 0; i < queries; i++) {
+      randId = ((Math.random() * 10000) | 0) + 1;
+      world = models.store.query('select World.* from World where World.id = :id', {id: randId})[0];
+      worlds.push(world.toJSON());
+   }
+   return response.json(worlds);
+});
+
+app.get('/fortune', function() {
+   var fortunes = models.Fortune.all();
+   fortunes.push({
+      _id: 0,
+      message: 'Additional fortune added at request time.'
+   });
+   fortunes.sort(models.Fortune.sort);
+   return response.html(fortuneTemplate.render({fortunes: fortunes}));
+});

+ 3 - 8
ringojs-stick/benchmark_config → ringojs-convinient/benchmark_config

@@ -1,19 +1,14 @@
 {
 {
-  "framework": "ringojs-stick",
+  "framework": "ringojs-convinient",
   "tests": [{
   "tests": [{
     "default": {
     "default": {
       "setup_file": "setup",
       "setup_file": "setup",
       "json_url": "/json",
       "json_url": "/json",
-      "port": 8080,
-      "sort": 76
-    },
-    "raw": {
-      "setup_file": "setup",
       "db_url": "/db",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/db/",
       "fortune_url": "/fortune",
       "fortune_url": "/fortune",
       "port": 8080,
       "port": 8080,
-      "sort": 77
+      "sort": 78
     }
     }
   }]
   }]
 }
 }

+ 1 - 1
ringojs-stick/ringo-main.js → ringojs-convinient/ringo-main.js

@@ -2,7 +2,7 @@ var {Application} = require("stick");
 
 
 var app = exports.app = Application();
 var app = exports.app = Application();
 app.configure("mount");
 app.configure("mount");
-app.mount("/", require("./views"));
+app.mount("/", require("./app/views"));
 
 
 if (require.main == module) {
 if (require.main == module) {
     require("ringo/httpserver").main(module.id);
     require("ringo/httpserver").main(module.id);

+ 3 - 2
ringojs-stick/setup.py → ringojs-convinient/setup.py

@@ -5,10 +5,11 @@ import setup_util
 import os
 import os
 
 
 def start(args):
 def start(args):
-  setup_util.replace_text("ringojs-stick/views.js", "dbHost = '.*';", "dbHost = '" + args.database_host + "';")
+  setup_util.replace_text("ringojs-convinient/app/models.js", "dbHost = '.*';", "dbHost = '" + args.database_host + "';")
 
 
   try:
   try:
-    subprocess.Popen("ringo --production ringo-main.js", shell=True, cwd="ringojs-stick")
+    subprocess.check_call("sudo cp /usr/share/ringojs//packages/sql-ringojs-client/jars/mysql.jar /usr/share/ringojs/packages/ringo-sqlstore/jars/", shell=True)
+    subprocess.Popen("ringo --production ringo-main.js", shell=True, cwd="ringojs-convinient")
     return 0
     return 0
   except subprocess.CalledProcessError:
   except subprocess.CalledProcessError:
     return 1
     return 1

+ 1 - 1
ringojs-stick/templates/fortunes.reinhardt → ringojs-convinient/templates/fortunes.reinhardt

@@ -11,7 +11,7 @@
 </tr>
 </tr>
 {% for fortune in fortunes %}
 {% for fortune in fortunes %}
 <tr>
 <tr>
-<td>{{fortune.id}}</td>
+<td>{{fortune._id}}</td>
 <td>{{fortune.message}}</td>
 <td>{{fortune.message}}</td>
 </tr>
 </tr>
 {% endfor %}
 {% endfor %}

+ 0 - 83
ringojs-stick/views.js

@@ -1,83 +0,0 @@
-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
-var dbHost = 'localhost';
-
-var sortFortunes = function(a, b) {
- return (a.message < b.message) ? -1 : (a.message > b.message) ? 1 : 0;
-};
-
-var fortuneTemplate = module.singleton('fortuneTemplate', function() {
-   return new Template(fs.read(module.resolve('./templates/fortunes.reinhardt')));
-})
-
-var datasource = module.singleton('pooling-datasource', function() {
-  return sql.connect("jdbc:mysql://" + dbHost + "/hello_world", 'benchmarkdbuser', 'benchmarkdbpass');
-});
-
-var app = exports.app = Application();
-app.configure("params", "route");
-
-app.get('/json', function() {
-	var helloObject = {message: "Hello, world"};
-	// JSON Response Test
-	return {
-      status: 200,
-      headers: {"Content-Type": "application/json; charset=UTF-8"},
-      body: [JSON.stringify(helloObject)]
-	}
-});
-
-app.get('/db', function(request) {
-   var queryCount = request.params.queries;
-   try {
-      var connection = datasource.getConnection();
-      if (queryCount === null) {
-         var randId = ((Math.random() * 10000) | 0) + 1
-         var world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
-         return response.json(world);
-      } else {
-         queryCount = parseInt(queryCount, 10);
-         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];
-            worlds.push(world);
-         }
-         return response.json(worlds);
-      }
-   } catch (e) {
-      connection.close();
-      connection = null;
-   } finally {
-      if (connection !== null) {
-         connection.close();
-      }
-   }
-});
-
-app.get('/fortune', function() {
-   try {
-      var connection = datasource.getConnection();
-      var fortunes = sql.query(connection, 'select * from Fortune');
-      fortunes.push({
-         id: 0,
-         message: 'Additional fortune added at request time.'
-      });
-      fortunes.sort(sortFortunes);
-      return response.html(fortuneTemplate.render({fortunes: fortunes}));
-   } catch (e) {
-      connection.close();
-      connection = null;
-   } finally {
-      if (connection !== null) {
-         connection.close();
-      }
-   }
-});