<commit_before>import * as fs from 'fs'
import * as path from 'path'

import App, { AppMode } from './app'
import { ConfigType, IWebConfig, IConfigOptions } from './config'
import MateriaError from './error'

import * as express from 'express'
import * as session from 'express-session'

import * as morgan from 'morgan'
import * as methodOverride from 'method-override'
import * as bodyParser from 'body-parser'
import * as errorHandler from 'errorhandler'
import * as compression from 'compression'

import {Session} from './session'

var enableDestroy = require('server-destroy')

/**
 * @class Server
 * @classdesc
 * Represent the server
 */
export class Server {
	started: boolean = false

	expressApp: express.Application
	server: any

	session: Session

	disabled: boolean = false

	constructor(private app: App) {
		this.session = new Session(app)
	}

	load() {
		this.expressApp = express()
		this.expressApp.use(bodyParser.urlencoded({ extended: false }))
		this.expressApp.use(bodyParser.json())
		this.expressApp.use(methodOverride())
		this.expressApp.use(compression())

		this.expressApp.use(express.static(path.join(this.app.client.config.build)));
		if ((this.app.mode == AppMode.DEVELOPMENT || this.app.options.logRequests) && this.app.options.logRequests != false) {
			this.expressApp.use(morgan('dev'))
		}

		//TODO: Option to enable / disable CORS API call
		this.expressApp.use(function(req, res, next) {
			res.header("Access-Control-Allow-Origin", "*");
			res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
			next();
		});

		this.expressApp.use(errorHandler())

		this.server = require('http').createServer(this.expressApp)
		enableDestroy(this.server)
	}

	/**
	Get the base url for endpoints
	@returns {string}
	*/
	getBaseUrl(path?:string, mode?:AppMode, options?:IConfigOptions) {
		path = path || '/api'
		let conf = this.app.config.get<IWebConfig>(mode, ConfigType.WEB, options)
		if ( ! conf) {
			return ""
		}
		let url = `${conf.ssl ? 'https' : 'http'}://${conf.host}`;
		if (conf.port != 80) {
			url += ':' + conf.port
		}
		url += path
		return url
	}

	/**
	Return true if the server is started
	@returns {boolean}
	*/
	isStarted():boolean { return this.started }

	/**
	Return true if the server has a static page
	@returns {boolean}
	*/
	hasStatic() {
		return fs.existsSync(path.join(this.app.client.config.build, 'index.html'))
	}

	/**
	Starts the server and listen on its endpoints.
	@returns {Promise<void>}
	*/
	start():Promise<void> {
		return new Promise<void>((resolve, reject) => {
			this.stop().then(() => {
				this.app.api.registerEndpoints()
				this.expressApp.all('/api/*', (req, res) => {
					res.status(404).send({
						error: true,
						message: 'API endpoint not found'
					})
				})

				this.expressApp.all('/*', (req, res) => {
					if (fs.existsSync(path.join(this.app.client.config.build, '404.html'))) {
						res.sendFile(path.join(this.app.client.config.build, '404.html'))
					}
					else if (this.hasStatic()) {
						res.sendFile(path.join(this.app.client.config.build, 'index.html'))
					}<commit_msg>(Server) delay the express static declaration<commit_after>import * as fs from 'fs'
import * as path from 'path'

import App, { AppMode } from './app'
import { ConfigType, IWebConfig, IConfigOptions } from './config'
import MateriaError from './error'

import * as express from 'express'
import * as session from 'express-session'

import * as morgan from 'morgan'
import * as methodOverride from 'method-override'
import * as bodyParser from 'body-parser'
import * as errorHandler from 'errorhandler'
import * as compression from 'compression'

import {Session} from './session'

var enableDestroy = require('server-destroy')

/**
 * @class Server
 * @classdesc
 * Represent the server
 */
export class Server {
	started: boolean = false

	expressApp: express.Application
	server: any

	session: Session

	disabled: boolean = false

	constructor(private app: App) {
		this.session = new Session(app)
	}

	load() {
		this.expressApp = express()
		this.expressApp.use(bodyParser.urlencoded({ extended: false }))
		this.expressApp.use(bodyParser.json())
		this.expressApp.use(methodOverride())
		this.expressApp.use(compression())
	}

	/**
	Get the base url for endpoints
	@returns {string}
	*/
	getBaseUrl(path?:string, mode?:AppMode, options?:IConfigOptions) {
		path = path || '/api'
		let conf = this.app.config.get<IWebConfig>(mode, ConfigType.WEB, options)
		if ( ! conf) {
			return ""
		}
		let url = `${conf.ssl ? 'https' : 'http'}://${conf.host}`;
		if (conf.port != 80) {
			url += ':' + conf.port
		}
		url += path
		return url
	}

	/**
	Return true if the server is started
	@returns {boolean}
	*/
	isStarted():boolean { return this.started }

	/**
	Return true if the server has a static page
	@returns {boolean}
	*/
	hasStatic() {
		return fs.existsSync(path.join(this.app.client.config.build, 'index.html'))
	}

	/**
	Starts the server and listen on its endpoints.
	@returns {Promise<void>}
	*/
	start():Promise<void> {
		this.expressApp.use(express.static(path.join(this.app.client.config.build)));
		if ((this.app.mode == AppMode.DEVELOPMENT || this.app.options.logRequests) && this.app.options.logRequests != false) {
			this.expressApp.use(morgan('dev'))
		}

		//TODO: Option to enable / disable CORS API call
		this.expressApp.use(function(req, res, next) {
			res.header("Access-Control-Allow-Origin", "*");
			res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
			next();
		});

		this.expressApp.use(errorHandler())

		this.server = require('http').createServer(this.expressApp)
		enableDestroy(this.server)

		return new Promise<void>((resolve, reject) => {
			this.stop().then(() => {
				this.app.api.registerEndpoints()
				this.expressApp.all('/api/*', (req, res) => {
					res.status(404).send({
						error: true,
						message: 'API endpoint not found'
					})
				})

				this.expressApp.all('/*', (req, res) => {
					if (fs.existsSync(path.join(this.app.client.config.build, '404.html'))) {
						res.sendFile(path.join(this.app.client.config.build, '404.html'))
					}
					else if (this.hasStatic()) {
						res.sendFile(path.join(this.app.client.config.build, 'index.html'))
					}
{'fraction_non_alphanumeric': 0.1599364069952305, 'fraction_numerical': 0.0034976152623211448, 'mean_word_length': 3.794969512195122, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '64829', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2057, 'n_words': 479}