Browse Source

hapi - fortunes test

Valentin Agachi 12 years ago
parent
commit
11cecb078f
5 changed files with 59 additions and 2 deletions
  1. 5 0
      hapi/README.md
  2. 30 1
      hapi/app.js
  3. 1 0
      hapi/benchmark_config
  4. 2 1
      hapi/package.json
  5. 21 0
      hapi/views/fortunes.handlebars

+ 5 - 0
hapi/README.md

@@ -38,3 +38,8 @@ http://localhost:8080/mongoose/2
 
 
 MySQL:
 MySQL:
 http://localhost:8080/mysql-orm/2
 http://localhost:8080/mysql-orm/2
+
+### Fortune Test
+
+MySQL:
+http://localhost:8080/fortune

+ 30 - 1
hapi/app.js

@@ -36,7 +36,14 @@ if (cluster.isMaster) {
 		console.log('worker ' + worker.pid + ' died');
 		console.log('worker ' + worker.pid + ' died');
 	});
 	});
 } else {
 } else {
-	var server = module.exports = Hapi.createServer(null, 8080);
+	var server = module.exports = Hapi.createServer(null, 8080, {
+		views: {
+			engines: {
+				handlebars: 'handlebars'
+			},
+			path: __dirname + '/views'
+		}
+	});
 
 
 	server.route({
 	server.route({
 		method: 'GET',
 		method: 'GET',
@@ -90,5 +97,27 @@ if (cluster.isMaster) {
 		}
 		}
 	});
 	});
 
 
+	server.route({
+		method: 'GET',
+		path: '/fortune',
+		handler: function(req){
+			if (windows) return req.reply(Hapi.error.internal('Not supported on windows'));
+
+			Fortune.all(function(err, fortunes){
+				fortunes.push({
+					id: 0,
+					message: 'Additional fortune added at request time.'
+				});
+				fortunes.sort(function(a, b){
+					return (a.message < b.message) ? -1 : 1;
+				});
+
+				req.reply.view('fortunes.handlebars', {
+					fortunes: fortunes
+				}).header('Server', 'hapi');
+			});
+		}
+	});
+
 	server.start();
 	server.start();
 }
 }

+ 1 - 0
hapi/benchmark_config

@@ -18,6 +18,7 @@
       "setup_file": "setup",
       "setup_file": "setup",
       "db_url": "/mysql-orm/",
       "db_url": "/mysql-orm/",
       "query_url": "/mysql-orm/",
       "query_url": "/mysql-orm/",
+      "fortune_url": "/fortune",
       "port": 8080,
       "port": 8080,
       "sort": 120
       "sort": 120
     }
     }

+ 2 - 1
hapi/package.json

@@ -6,6 +6,7 @@
 		"hapi": "1.2.0",
 		"hapi": "1.2.0",
 		"mongoose": "3.5.5",
 		"mongoose": "3.5.5",
 		"async": "0.2.5",
 		"async": "0.2.5",
-		"mapper": "0.2.4-pre"
+		"mapper": "0.2.4-pre",
+		"handlebars": "1.0.11"
 	}
 	}
 }
 }

+ 21 - 0
hapi/views/fortunes.handlebars

@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<title>Fortunes</title>
+</head>
+
+<body>
+	<table>
+		<tr>
+			<th>id</th>
+			<th>message</th>
+		</tr>
+{{#fortunes}}
+		<tr>
+			<td>{{id}}</td>
+			<td>{{message}}</td>
+		</tr>
+{{/fortunes}}
+	</table>
+</body>
+</html>