<commit_before>// Copyright (c) 2016 <NAME>

import 'dart:async' show runZoned;
import 'dart:io' show Platform;

import 'package:bungie_client/bungie_client.dart';
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_route/shelf_route.dart';

import '../lib/grimoire.dart' as grimoire;
import '../lib/params.dart' as param;

/// Returns the value for [name] in the server configuration.
String _getConfigValue(String name) {
  final value = Platform.environment[name];
  if (value == null) {
    throw 'Missing configuration value for $name';
  }
  return value;
}

main() {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.loggerName}: ${rec.message}');
  });

  final log = new Logger('gDestinyServer');
  final portEnv = Platform.environment['PORT'];
  final port = portEnv == null ? 9999 : int.parse(portEnv);

  final context = {
    param.BUNGIE_CLIENT: new BungieClient(_getConfigValue('BUNGIE_API_KEY')),
    param.BUNGIE_CLAN_ID: _getConfigValue('BUNGIE_CLAN_ID'),
    param.DATABASE_URL: _getConfigValue('DATABASE_URL')
  };

  final commandRouter = router()..get('/grimoire', grimoire.handle);

  final handler = const shelf.Pipeline()
      .addMiddleware(
          shelf.logRequests(logger: (String message, _) => log.info(message)))
      .addMiddleware((shelf.Handler handler) {
    // Injects common parameters for request handlers to use.
    return (shelf.Request request) {
      return handler(request.change(context: context));
    };
  }).addHandler(commandRouter.handler);

  runZoned(() {
    log.info('Serving on port $port');
    printRoutes(commandRouter, printer: log.info);
    io.serve(handler, '0.0.0.0', port);
  }, onError: (e, stackTrace) => log.severe('Oh noes! $e $stackTrace'));
}
<commit_msg>Added access control header (#11)<commit_after>// Copyright (c) 2016 <NAME>

import 'dart:async' show runZoned;
import 'dart:io' show Platform;

import 'package:bungie_client/bungie_client.dart';
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_route/shelf_route.dart';

import '../lib/grimoire.dart' as grimoire;
import '../lib/params.dart' as param;

/// Returns the value for [name] in the server configuration.
String _getConfigValue(String name) {
  final value = Platform.environment[name];
  if (value == null) {
    throw 'Missing configuration value for $name';
  }
  return value;
}

main() {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.loggerName}: ${rec.message}');
  });

  final log = new Logger('gDestinyServer');
  final portEnv = Platform.environment['PORT'];
  final port = portEnv == null ? 9999 : int.parse(portEnv);

  final context = {
    param.BUNGIE_CLIENT: new BungieClient(_getConfigValue('BUNGIE_API_KEY')),
    param.BUNGIE_CLAN_ID: _getConfigValue('BUNGIE_CLAN_ID'),
    param.DATABASE_URL: _getConfigValue('DATABASE_URL')
  };

  final commandRouter = router()..get('/grimoire', grimoire.handle);

  final handler = const shelf.Pipeline()
      .addMiddleware(
          shelf.logRequests(logger: (String message, _) => log.info(message)))
      .addMiddleware((shelf.Handler handler) {
    // Injects common parameters for request handlers to use.
    return (shelf.Request request) {
      return handler(request.change(context: context));
    };
  }).addMiddleware((shelf.Handler handler) {
    // Add access control headers.
    return (shelf.Request request) async {
      final shelf.Response response = await handler(request);
      return response.change(headers: {'Access-Control-Allow-Origin': '*'});
    };
  }).addHandler(commandRouter.handler);

  runZoned(() {
    log.info('Serving on port $port');
    printRoutes(commandRouter, printer: log.info);
    io.serve(handler, '0.0.0.0', port);
  }, onError: (e, stackTrace) => log.severe('Oh noes! $e $stackTrace'));
}

{'fraction_non_alphanumeric': 0.15030450669914738, 'fraction_numerical': 0.00633373934226553, 'mean_word_length': 4.291237113402062, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1822759', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1354, 'n_words': 302}