|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:math'; |
| 4 | +import 'package:angel_configuration/angel_configuration.dart'; |
| 5 | +import 'package:angel_framework/angel_framework.dart'; |
| 6 | +import 'package:args/args.dart'; |
| 7 | +import 'package:file/local.dart'; |
| 8 | +import 'package:http_parser/http_parser.dart'; |
| 9 | +import 'package:mongo_dart/mongo_dart.dart'; |
| 10 | +import 'package:mustache4dart/mustache4dart.dart' as mustache; |
| 11 | +import 'package:postgres/postgres.dart'; |
| 12 | +import 'src/models/models.dart'; |
| 13 | +import 'src/query/query.dart'; |
| 14 | + |
| 15 | +AngelConfigurer configureServer(ArgResults argResults) { |
| 16 | + var rnd = new Random(); |
| 17 | + var minQueryCount = 1; |
| 18 | + var maxQueryCount = 500; |
| 19 | + var worldTableSize = 10000; |
| 20 | + var fs = const LocalFileSystem(); |
| 21 | + |
| 22 | + return (Angel app) async { |
| 23 | + // Load configuration. |
| 24 | + await app.configure(configuration(fs)); |
| 25 | + |
| 26 | + // Set up the view engine. |
| 27 | + var fortunesTemplate = |
| 28 | + await fs.file('views/fortunes.mustache').readAsString(); |
| 29 | + |
| 30 | + app.viewGenerator = |
| 31 | + (name, [data]) => mustache.render(fortunesTemplate, data); |
| 32 | + |
| 33 | + // Select a querier, either MongoDB or PostgreSQL. |
| 34 | + // |
| 35 | + // Either way, the container *must* contain a `Querier`. |
| 36 | + if (argResults['type'] == 'mongo') { |
| 37 | + var db = Db(app.configuration['mongo_db']); |
| 38 | + app.container |
| 39 | + .registerSingleton<Querier>(MongoQuerier(db, rnd, worldTableSize)); |
| 40 | + await db.open(); |
| 41 | + app.shutdownHooks.add((_) => db.close()); |
| 42 | + } else if (argResults['type'] == 'postgres') { |
| 43 | + var postgresConfig = app.configuration['postgres'] as Map; |
| 44 | + var connection = PostgreSQLConnection( |
| 45 | + postgresConfig['host'], |
| 46 | + postgresConfig['port'], |
| 47 | + postgresConfig['databaseName'], |
| 48 | + username: postgresConfig['username'], |
| 49 | + password: postgresConfig['password'], |
| 50 | + ); |
| 51 | + app.container.registerSingleton<Querier>(PostgresQuerier(connection)); |
| 52 | + await connection.open(); |
| 53 | + app.shutdownHooks.add((_) => connection.close()); |
| 54 | + } else { |
| 55 | + throw UnsupportedError('Unsupported DB ${argResults['type']}'); |
| 56 | + } |
| 57 | + |
| 58 | + // Always add a Date header. |
| 59 | + app.fallback((req, res) { |
| 60 | + res.headers['date'] = HttpDate.format(DateTime.now()); |
| 61 | + }); |
| 62 | + |
| 63 | + // JSON response. |
| 64 | + app.get('/json', (req, res) { |
| 65 | + res.serialize({'message': 'Hello, World!'}); |
| 66 | + }); |
| 67 | + |
| 68 | + // Plaintext response. |
| 69 | + app.get('/plaintext', (req, res) { |
| 70 | + res |
| 71 | + ..write('Hello, World!') |
| 72 | + ..close(); |
| 73 | + }); |
| 74 | + |
| 75 | + // Fetch random world object. |
| 76 | + app.get('/db', (req, res) async { |
| 77 | + var querier = req.container.make<Querier>(); |
| 78 | + res.serialize(await querier.getRandomWorld()); |
| 79 | + }); |
| 80 | + |
| 81 | + // DB queries |
| 82 | + app.get('/queries', (req, res) async { |
| 83 | + // Get the querier and query count. |
| 84 | + var querier = req.container.make<Querier>(); |
| 85 | + var queryCount = |
| 86 | + int.tryParse(req.uri.queryParameters['queryCount'].toString()) ?? |
| 87 | + minQueryCount; |
| 88 | + queryCount = queryCount.clamp(minQueryCount, maxQueryCount); |
| 89 | + |
| 90 | + // Fetch the objects. |
| 91 | + var worlds = await Future.wait<World>( |
| 92 | + List.generate(queryCount, (_) => querier.getRandomWorld())); |
| 93 | + res.serialize(worlds); |
| 94 | + }); |
| 95 | + |
| 96 | + // DB updates |
| 97 | + app.get('/updates', (req, res) async { |
| 98 | + // Get the querier and query count. |
| 99 | + var querier = req.container.make<Querier>(); |
| 100 | + var queryCount = |
| 101 | + int.tryParse(req.uri.queryParameters['queryCount'].toString()) ?? |
| 102 | + minQueryCount; |
| 103 | + queryCount = queryCount.clamp(minQueryCount, maxQueryCount); |
| 104 | + |
| 105 | + // Fetch the objects. |
| 106 | + var worlds = |
| 107 | + await Future.wait<World>(List.generate(queryCount, (_) async { |
| 108 | + var world = await querier.getRandomWorld(); |
| 109 | + world = world.copyWith(randomNumber: rnd.nextInt(worldTableSize) + 1); |
| 110 | + await querier.updateWorld(world.id, world); |
| 111 | + return world; |
| 112 | + })); |
| 113 | + res.serialize(worlds); |
| 114 | + }); |
| 115 | + |
| 116 | + // Templating |
| 117 | + app.get('/fortunes', (req, res) async { |
| 118 | + var querier = req.container.make<Querier>(); |
| 119 | + var fortunes = await querier.getFortunes(); |
| 120 | + |
| 121 | + // Insert an additional fortune. |
| 122 | + fortunes.add( |
| 123 | + Fortune( |
| 124 | + id: 0, |
| 125 | + message: 'Additional fortune added at request time.', |
| 126 | + ), |
| 127 | + ); |
| 128 | + |
| 129 | + // Sort the fortunes. |
| 130 | + fortunes.sort((a, b) => a.message.compareTo(b.message)); |
| 131 | + |
| 132 | + // Render the template. |
| 133 | + res.contentType = new MediaType('text', 'html', {'charset': 'utf-8'}); |
| 134 | + await res.render('fortunes', |
| 135 | + {'fortunes': fortunes.map((f) => f.copyWith(id: f.id.toInt()))}); |
| 136 | + }); |
| 137 | + }; |
| 138 | +} |
0 commit comments