123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- import "dart:core";
- import "dart:io";
- import 'dart:async' show Future;
- import 'dart:math' show Random;
- import "package:redstone/server.dart" as app;
- import "package:redstone_mapper/mapper.dart";
- import "package:redstone_mapper/plugin.dart";
- import "package:redstone_mapper_mongo/manager.dart";
- import "package:redstone_mapper_pg/manager.dart";
- import "package:postgresql/postgresql.dart" as pg;
- import "package:di/di.dart";
- import "package:args/args.dart";
- import 'package:yaml/yaml.dart' as yaml;
- import 'package:mustache/mustache.dart' as mustache;
- const _WORLD_TABLE_SIZE = 10000;
- final _RANDOM = new Random();
- class Fortune implements Comparable<Fortune> {
-
- @Field()
- int id;
-
- @Field()
- String message;
-
- Fortune([this.id, this.message]);
- compareTo(Fortune other) => message.compareTo(other.message);
-
- }
- class World {
-
- @Field()
- int id;
- @Field(model: "randomnumber")
- int randomNumber;
- }
- class MongoFortune implements Comparable<Fortune> {
-
- @Field(model: "_id")
- int id;
-
- @Field()
- String message;
-
- MongoFortune([this.id, this.message]);
- compareTo(Fortune other) => message.compareTo(other.message);
-
- }
- class MongoWorld {
-
- @Field(model: "_id")
- int id;
- @Field()
- int randomNumber;
- }
- ///Handle PostgreSql connections
- @app.Interceptor(r'/pg/.+')
- pgSqlManager(PostgreSqlManager pgSql) {
- pgSql.getConnection().then((conn) {
- app.request.attributes["dbConn"] = conn;
- app.chain.next(() {
- pgSql.closeConnection(conn, error: app.chain.error);
- });
- });
- }
- ///Handle MongoDb connections
- @app.Interceptor(r'/mongo/.+')
- mongoDbManager(MongoDbManager mongoDb) {
- mongoDb.getConnection().then((conn) {
- app.request.attributes["dbConn"] = conn;
- app.chain.next(() {
- mongoDb.closeConnection(conn, error: app.chain.error);
- });
- });
- }
- ///JSON test
- @app.Route("/json")
- getJson() => {"message": "Hello, World!"};
- ///PlainText test
- @app.Route("/plaintext")
- getPlainText() => "Hello, World!";
- ///PostgreSql tests
- @app.Group("/pg")
- @Encode()
- class PgTests {
-
- static const worldQuery = 'SELECT id, randomnumber FROM world WHERE id = @id;';
- static const worldUpdt = 'UPDATE world SET randomnumber = @randomnumber WHERE id = @id;';
-
- static const fortuneQuery = 'SELECT id, message FROM fortune;';
-
- PostgreSql get pgSql => app.request.attributes["dbConn"];
-
- @app.Route("/db")
- Future<World> queryTest() {
- var params = { 'id': _RANDOM.nextInt(_WORLD_TABLE_SIZE) + 1 };
- return pgSql.query(worldQuery, World, params).then((list) => list[0]);
- }
- @app.Route("/queries")
- Future<List<World>> queriesTest() {
- var queries = _parseQueriesParam(app.request.queryParams.queries);
- return Future.wait(new List.generate(queries, (_) => queryTest()));
- }
-
- @app.Route("/updates")
- Future<List<World>> updateTest() {
- var queries = _parseQueriesParam(app.request.queryParams.queries);
- return Future.wait(new List.generate(queries, (_) => queryTest().then((world) {
- world.randomNumber = _RANDOM.nextInt(_WORLD_TABLE_SIZE) + 1;
- return pgSql.execute(worldUpdt, world).then((_) => world);
- })));
- }
-
- @app.Route("/fortunes", responseType: "text/html")
- Future<String> fortunesTest(@app.Inject() mustache.Template template) {
- return pgSql.query(fortuneQuery, Fortune).then((values) {
- values
- ..add(new Fortune(0, 'Additional fortune added at request time.'))
- ..sort();
-
- return template.renderString({
- "fortunes": encode(values)
- });
- });
- }
-
- }
- ///MongoDb tests
- @app.Group("/mongo")
- @Encode()
- class MongoTests {
-
- static const worldCollection = "World";
- static const fortuneCollection = "Fortune";
-
- MongoDb get mongoDb => app.request.attributes["dbConn"];
-
- @app.Route("/db")
- Future<MongoWorld> queryTest() {
- return mongoDb.findOne(worldCollection, MongoWorld, {
- "_id": _RANDOM.nextInt(_WORLD_TABLE_SIZE) + 1
- });
- }
-
- @app.Route("/queries")
- Future<List<MongoWorld>> queriesTest() {
- var queries = _parseQueriesParam(app.request.queryParams.queries);
- return Future.wait(new List.generate(queries, (_) => queryTest()));
- }
-
- @app.Route("/updates")
- Future<List<MongoWorld>> updateTest() {
- var queries = _parseQueriesParam(app.request.queryParams.queries);
- return Future.wait(new List.generate(queries, (_) => queryTest().then((world) {
- world.randomNumber = _RANDOM.nextInt(_WORLD_TABLE_SIZE) + 1;
- return mongoDb.update(worldCollection, { "_id": world.id }, world)
- .then((_) => world);
- })));
- }
-
- @app.Route("/fortunes", responseType: "text/html")
- Future<String> fortunesTest(@app.Inject() mustache.Template template) {
- return mongoDb.find(fortuneCollection, MongoFortune).then((values) {
- values
- ..add(new MongoFortune(0, 'Additional fortune added at request time.'))
- ..sort();
-
- return template.renderString({
- "fortunes": encode(values)
- });
- });
- }
-
- }
- main(List<String> args) {
- var parser = new ArgParser();
- parser.addOption('address', abbr: 'a', defaultsTo: '0.0.0.0');
- parser.addOption('port', abbr: 'p', defaultsTo: '8080');
- parser.addOption('dbconnections', abbr: 'd', defaultsTo: '256');
-
- var arguments = parser.parse(args);
- var dbConnections = int.parse(arguments["dbconnections"]);
-
- MongoDbManager mongoDbManager;
- PostgreSqlManager pgSqlManager;
- mustache.Template fortunesTemplate;
-
- Future.wait([
-
- //load PostgreSql configuration
- new File("postgresql.yaml").readAsString().then((config){
- pgSqlManager = new PostgreSqlManager(
- new pg.Settings.fromMap(yaml.loadYaml(config)).toUri(),
- min: dbConnections,
- max: dbConnections);
- }),
-
- //load MongoDb configuration
- new File("mongodb.yaml").readAsString().then((config) {
- var mongoConfig = yaml.loadYaml(config);
- mongoDbManager = new MongoDbManager(
- "mongodb://${mongoConfig["host"]}/${mongoConfig["database"]}",
- poolSize: dbConnections);
- }),
-
- //load fortunes mustache template
- new File('fortunes.mustache').readAsString().then((template) {
- fortunesTemplate = mustache.parse(template);
- })
-
- ]).then((_) {
-
- //app.setupConsoleLog();
-
- //install module for dependency injection
- app.addModule(new Module()
- ..bind(MongoDbManager, toValue: mongoDbManager)
- ..bind(PostgreSqlManager, toValue: pgSqlManager)
- ..bind(mustache.Template, toValue: fortunesTemplate));
-
- //initialize mapper plugin
- app.addPlugin(getMapperPlugin());
-
- //start the server
- app.start(address: arguments["address"], port: int.parse(arguments["port"]));
-
- });
- }
- _parseQueriesParam(param) {
- return param.isEmpty ? 1 : int.parse(param, radix: 10, onError: (_) => 1).clamp(1, 500);
- }
|