Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library NuclearSummer;
- import 'package:sqljocky/sqljocky.dart';
- import 'dart:async';
- import 'dart:io';
- import 'dart:mirrors';
- part 'model/player.dart';
- part 'model/user.dart';
- part 'model/training_stats.dart';
- part 'model/util.dart';
- part 'model/ranks.dart';
- part 'model/events.dart';
- part 'view/template_filler.dart';
- part 'controller/controller.dart';
- ConnectionPool db;
- // TODO: Change this to actually fetch from a file.
- dynamic config(String key) {
- Map<String,String> config = {
- "DATABASE_NAME": "NuclearSummer",
- "DATABASE_USER": "root",
- "DATABASE_PASSWORD": "",
- "DATABASE_PORT": 3306,
- "DATABASE_HOST": "127.0.0.1",
- "SERVER_PORT": 81,
- "SERVER_HOST": "0.0.0.0",
- "ROOT_DIR": r"C:\Users\Thomas\dart\NuclearSummer\bin\view\"
- };
- return config[key];
- }
- bool classExists(String className) {
- try {
- return currentMirrorSystem().isolate.rootLibrary.classes.containsKey(new Symbol(className));
- }
- catch (e) {
- return false;
- }
- }
- bool classHasMethod(Object o, String methodName) {
- try {
- return reflect(o).type.methods.containsKey(new Symbol (methodName));
- }
- catch (e) {
- return false;
- }
- }
- void send503 (HttpRequest req) {
- req.response.statusCode = HttpStatus.FORBIDDEN;
- req.response.write("The file you have requested is not available at this time (503)");
- req.response.close();
- }
- void send404 (HttpRequest req) {
- req.response.statusCode = HttpStatus.NOT_FOUND;
- req.response.write("The file you have requested is not available at this time (404)");
- req.response.close();
- }
- void main() {
- Language.init();
- EventHandler.init();
- db = new ConnectionPool(host: config("DATABASE_HOST"), port: config("DATABASE_PORT"), user: config("DATABASE_USER"), db: "", max: 5);
- db.query("CREATE DATABASE IF NOT EXISTS ${config("DATABASE_NAME")}").then((t) {
- return db.query("USE ${config("DATABASE_NAME")}");
- });
- // Whitelisting files until we get around the whole dl any file thing
- PathWhitelist.addPath("/");
- PathWhitelist.addPath("/favicon.ico");
- // First time install shit to go below.
- String bsepth = r"..\web\";
- HttpServer.bind(config("SERVER_HOST"), config("SERVER_PORT")).then((HttpServer server) {
- print('Starting server on port: ${config("SERVER_PORT")}');
- server.listen((HttpRequest req) {
- // Retreive file and send back
- String path = req.uri.path;
- if (path.substring(0,1) != "/") {
- path = "/${path}";
- }
- File f = new File("${config("ROOT_DIR")}$path");
- f.exists().then((bool ex) {
- if (ex) {
- if (PathWhitelist.canRequest(path)) {
- f.openRead()
- .pipe(req.response)
- .catchError((e) { print(e); });
- }
- else {
- send503(req);
- }
- }
- else {
- print(path);
- List<String> splitPth = path.split('/');
- if (classExists(splitPth[1])) {
- ClassMirror m = currentMirrorSystem().isolate.rootLibrary.classes[new Symbol(splitPth[1])];
- if (m.superclass.qualifiedName == new Symbol("NuclearSummer.Controller")) {
- InstanceMirror instance = m.newInstance(new Symbol("create"), []);
- if (splitPth.length > 2) {
- if (classHasMethod(instance.reflectee, splitPth[2])) {
- instance.invoke(new Symbol (splitPth[2]), [req]);
- }
- }
- else {
- instance.reflectee.defaultPage(req);
- }
- }
- else {
- send404(req);
- }
- }
- else {
- send404(req);
- }
- }
- });
- }, onError: (e) { print("ERROR: $e"); });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement