<commit_before>import * as http from 'http';
import * as path from 'path';

import API from './lib/api';
import Logger from './lib/logger';
import { isLeaderServer } from './lib/utility';
import { Server as NodeStaticServer } from 'node-static';

const DEFAULT_PORT = 9000;
const SLOW_REQUEST_LIMIT = 2000;
const CONFIG_PATH = '../../config.json';
const CLIENT_PATH = '../../web';

const CSP_HEADER = `default-src 'none'; style-src 'self' 'nonce-123456789' 'nonce-987654321' https://fonts.googleapis.com; img-src 'self' www.google-analytics.com; media-src data: blob: https://*.amazonaws.com; script-src 'self' https://www.google-analytics.com/analytics.js; font-src 'self' https://fonts.gstatic.com; connect-src 'self'`;

const config = require(CONFIG_PATH);

export default class Server {
  server: http.Server;
  api: API;
  logger: Logger;
  staticServer: any;

  constructor() {
    this.staticServer = new NodeStaticServer(
      path.join(__dirname, CLIENT_PATH),
      {
        cache: false,
        headers: {
          'Content-Security-Policy': CSP_HEADER,
        },
      }
    );

    this.api = new API();

    // Make console.log output json.
    if (config.PROD) {
      this.logger = new Logger();
      this.logger.overrideConsole();
    }
  }

  /**
   * Log application level messages in a common format.
   */
  private print(...args: any[]) {
    args.unshift('APPLICATION --');
    console.log.apply(console, args);
  }

  /**
   * handleRequest
   *   Route requests to appropriate controller based on
   *   if the request deals with voice clips or web content.
   */
  private handleRequest(
    request: http.IncomingMessage,
    response: http.ServerResponse
  ) {
    let startTime = Date.now();

    if (this.api.isApiRequest(request)) {
      this.api.handleRequest(request, response);
      return;
    }

    // If we get here, feed request to static parser.
    request
      .addListener('end', () => {
        this.staticServer.serve(request, response, (err: any) => {
          if (err && err.status === 404) {
            console.error('page not found', request.url);

            // If file was not front, use main page and
            // let the front end handle url routing.
            this.staticServer.serveFile(
              'index.html',
              200,
              {},
              request,
              response<commit_msg>dont error log non-static requests<commit_after>import * as http from 'http';
import * as path from 'path';

import API from './lib/api';
import Logger from './lib/logger';
import { isLeaderServer } from './lib/utility';
import { Server as NodeStaticServer } from 'node-static';

const DEFAULT_PORT = 9000;
const SLOW_REQUEST_LIMIT = 2000;
const CONFIG_PATH = '../../config.json';
const CLIENT_PATH = '../../web';

const CSP_HEADER = `default-src 'none'; style-src 'self' 'nonce-123456789' 'nonce-987654321' https://fonts.googleapis.com; img-src 'self' www.google-analytics.com; media-src data: blob: https://*.amazonaws.com; script-src 'self' https://www.google-analytics.com/analytics.js; font-src 'self' https://fonts.gstatic.com; connect-src 'self'`;

const config = require(CONFIG_PATH);

export default class Server {
  server: http.Server;
  api: API;
  logger: Logger;
  staticServer: any;

  constructor() {
    this.staticServer = new NodeStaticServer(
      path.join(__dirname, CLIENT_PATH),
      {
        cache: false,
        headers: {
          'Content-Security-Policy': CSP_HEADER,
        },
      }
    );

    this.api = new API();

    // Make console.log output json.
    if (config.PROD) {
      this.logger = new Logger();
      this.logger.overrideConsole();
    }
  }

  /**
   * Log application level messages in a common format.
   */
  private print(...args: any[]) {
    args.unshift('APPLICATION --');
    console.log.apply(console, args);
  }

  /**
   * handleRequest
   *   Route requests to appropriate controller based on
   *   if the request deals with voice clips or web content.
   */
  private handleRequest(
    request: http.IncomingMessage,
    response: http.ServerResponse
  ) {
    let startTime = Date.now();

    if (this.api.isApiRequest(request)) {
      this.api.handleRequest(request, response);
      return;
    }

    // If we get here, feed request to static parser.
    request
      .addListener('end', () => {
        this.staticServer.serve(request, response, (err: any) => {
          if (err && err.status === 404) {
            console.log('non-static resource request', request.url);

            // If file was not front, use main page and
            // let the front end handle url routing.
            this.staticServer.serveFile(
              'index.html',
              200,
              {},
              request,
              response
{'fraction_non_alphanumeric': 0.13391739674593242, 'fraction_numerical': 0.013350020859407593, 'mean_word_length': 2.4620938628158844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, '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': '6160290', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1405, 'n_words': 389}