Advertisement
Guest User

Untitled

a guest
Jun 30th, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. library NuclearSummer;
  2.  
  3. import 'package:sqljocky/sqljocky.dart';
  4. import 'dart:async';
  5. import 'dart:io';
  6. import 'dart:mirrors';
  7.  
  8. part 'model/player.dart';
  9. part 'model/user.dart';
  10. part 'model/training_stats.dart';
  11. part 'model/util.dart';
  12. part 'model/ranks.dart';
  13. part 'model/events.dart';
  14.  
  15. part 'view/template_filler.dart';
  16.  
  17. part 'controller/controller.dart';
  18.  
  19. ConnectionPool db;
  20.  
  21. // TODO: Change this to actually fetch from a file.
  22. dynamic config(String key) {
  23. Map<String,String> config = {
  24. "DATABASE_NAME": "NuclearSummer",
  25. "DATABASE_USER": "root",
  26. "DATABASE_PASSWORD": "",
  27. "DATABASE_PORT": 3306,
  28. "DATABASE_HOST": "127.0.0.1",
  29. "SERVER_PORT": 81,
  30. "SERVER_HOST": "0.0.0.0",
  31. "ROOT_DIR": r"C:\Users\Thomas\dart\NuclearSummer\bin\view\"
  32. };
  33. return config[key];
  34. }
  35.  
  36. bool classExists(String className) {
  37. try {
  38. return currentMirrorSystem().isolate.rootLibrary.classes.containsKey(new Symbol(className));
  39. }
  40. catch (e) {
  41. return false;
  42. }
  43. }
  44. bool classHasMethod(Object o, String methodName) {
  45. try {
  46. return reflect(o).type.methods.containsKey(new Symbol (methodName));
  47. }
  48. catch (e) {
  49. return false;
  50. }
  51. }
  52.  
  53. void send503 (HttpRequest req) {
  54. req.response.statusCode = HttpStatus.FORBIDDEN;
  55. req.response.write("The file you have requested is not available at this time (503)");
  56. req.response.close();
  57. }
  58. void send404 (HttpRequest req) {
  59. req.response.statusCode = HttpStatus.NOT_FOUND;
  60. req.response.write("The file you have requested is not available at this time (404)");
  61. req.response.close();
  62. }
  63.  
  64. void main() {
  65. Language.init();
  66. EventHandler.init();
  67. db = new ConnectionPool(host: config("DATABASE_HOST"), port: config("DATABASE_PORT"), user: config("DATABASE_USER"), db: "", max: 5);
  68. db.query("CREATE DATABASE IF NOT EXISTS ${config("DATABASE_NAME")}").then((t) {
  69. return db.query("USE ${config("DATABASE_NAME")}");
  70. });
  71.  
  72. // Whitelisting files until we get around the whole dl any file thing
  73.  
  74. PathWhitelist.addPath("/");
  75. PathWhitelist.addPath("/favicon.ico");
  76.  
  77. // First time install shit to go below.
  78. String bsepth = r"..\web\";
  79. HttpServer.bind(config("SERVER_HOST"), config("SERVER_PORT")).then((HttpServer server) {
  80. print('Starting server on port: ${config("SERVER_PORT")}');
  81. server.listen((HttpRequest req) {
  82. // Retreive file and send back
  83. String path = req.uri.path;
  84. if (path.substring(0,1) != "/") {
  85. path = "/${path}";
  86. }
  87.  
  88. File f = new File("${config("ROOT_DIR")}$path");
  89. f.exists().then((bool ex) {
  90. if (ex) {
  91. if (PathWhitelist.canRequest(path)) {
  92. f.openRead()
  93. .pipe(req.response)
  94. .catchError((e) { print(e); });
  95. }
  96. else {
  97. send503(req);
  98. }
  99. }
  100. else {
  101. print(path);
  102. List<String> splitPth = path.split('/');
  103. if (classExists(splitPth[1])) {
  104. ClassMirror m = currentMirrorSystem().isolate.rootLibrary.classes[new Symbol(splitPth[1])];
  105. if (m.superclass.qualifiedName == new Symbol("NuclearSummer.Controller")) {
  106. InstanceMirror instance = m.newInstance(new Symbol("create"), []);
  107. if (splitPth.length > 2) {
  108. if (classHasMethod(instance.reflectee, splitPth[2])) {
  109. instance.invoke(new Symbol (splitPth[2]), [req]);
  110. }
  111. }
  112. else {
  113. instance.reflectee.defaultPage(req);
  114. }
  115. }
  116. else {
  117. send404(req);
  118. }
  119. }
  120. else {
  121. send404(req);
  122. }
  123. }
  124. });
  125.  
  126. }, onError: (e) { print("ERROR: $e"); });
  127. });
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement