<commit_before>"use strict";

import express from "express";
import * as path from "path";
import { isInProduction } from "../environment/environment";
import profile from "../profile/profile";
import { createNewCharacter } from "../character/character";

/**
 * Sole purpose of this file is to provide the routes for different "*-view" components in the front.
 * For example for path="/" we provide index.html, for path="/foobar" it provides index.html and so on..
 * Provide index.html for the front routes so the single page app frontend can then sort itself.
 */

const pathToIndexHtml = path.join(__dirname, "../../components/index.html");

// Note: we could also boldly just define that all routes not matching SOMETHING will return index.html
//       but it makes debugging different issue cases extremely difficult when you are only get index.html
//   	 for missing js files and such... Think how hard it would have been to debug the missing component .js
//   	 file cases with receiving index.html for all the js files :7

/**
 * Adds all the required frontend routes for Single Page App usage scenario.
 * @param {express.Application} app Application object from express().
 */
export function addViewIndexRoutesForSpa(app: express.Application) {
	// Defining the routes we want to hook explicitly to index.html
	[
		"/",
		"/front",
		"/login"
	]
	.forEach(route => app.get(route, (request: express.Request, response: express.Response) =>
		response.sendFile(pathToIndexHtml)));

	app.get("/profile", (request: express.Request, response: express.Response) => {
		if (request.isAuthenticated ? request.isAuthenticated() : false) {
			// TODO proper error sending to fron in .catch
			profile.get(request.user._json.id)
				.then(profile => response.json(profile))
				.catch(() => response.json({}));
		} else {
			response.status(401);
		}
	});

	app.get("/character", (request: express.Request, response: express.Response) => {<commit_msg>Add minor comments on the route functionality.<commit_after>"use strict";

import express from "express";
import * as path from "path";
import { isInProduction } from "../environment/environment";
import profile from "../profile/profile";
import { createNewCharacter } from "../character/character";

/**
 * Sole purpose of this file is to provide the routes for different "*-view" components in the front.
 * For example for path="/" we provide index.html, for path="/foobar" it provides index.html and so on..
 * Provide index.html for the front routes so the single page app frontend can then sort itself.
 */

const pathToIndexHtml = path.join(__dirname, "../../components/index.html");

// Note: we could also boldly just define that all routes not matching SOMETHING will return index.html
//       but it makes debugging different issue cases extremely difficult when you are only get index.html
//   	 for missing js files and such... Think how hard it would have been to debug the missing component .js
//   	 file cases with receiving index.html for all the js files :7

/**
 * Adds all the required frontend routes for Single Page App usage scenario.
 * @param {express.Application} app Application object from express().
 */
export function addViewIndexRoutesForSpa(app: express.Application) {
	// Defining the routes we want to hook explicitly to index.html
	[
		"/",
		"/front",
		"/login"
	]
	.forEach(route => app.get(route, (request: express.Request, response: express.Response) =>
		response.sendFile(pathToIndexHtml)));

	app.get("/profile", (request: express.Request, response: express.Response) => {
		if (request.isAuthenticated ? request.isAuthenticated() : false) {
			// TODO proper error sending to fron in .catch
			profile.get(request.user._json.id)
				.then(profile => response.json(profile))
				.catch(() => response.json({}));
		} else {
			response.status(401);
		}
	});

	// User can create characters
	app.get("/character", (request: express.Request, response: express.Response) => {
{'fraction_non_alphanumeric': 0.12053346753900353, 'fraction_numerical': 0.0020130850528434826, 'mean_word_length': 3.883292383292383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6174602', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1063, 'n_words': 424}