<commit_before>"use strict";



const express = require( "express" );
const expressApp = express();
const compression = require( "compression" );
const cookieParser = require( "cookie-parser" );
const lager = require( "properjs-lager" );
const listeners = {};
const core = {
    watch: require( "./watch" ),
    query: require( "./query" ),
    config: require( "../../clutch.config" ),
    content: require( "./content" ),
    template: require( "./template" )
};



/**
 *
 * Configure Express Middleware.
 *
 */
expressApp.use( cookieParser() );
expressApp.use( compression( core.config.compression ) );
expressApp.use( express.static( core.config.template.staticDir, {
    maxAge: core.config.static.maxAge
}));



/**
 *
 * Configure Express Routes.
 *
 */
const getKey = function ( type ) {
    const key = type;

    return key || core.config.homepage;
}
const getApi = function ( req, res ) {
    const key = getKey( req.params.type );

    core.query.getApi( req, res, listeners[ key ] ).then(( result ) => {
        if ( req.query.format === "html" ) {
            res.status( 200 ).send( result );

        } else {
            res.status( 200 ).json( result );
        }
    });
};
const getPage = function ( req, res ) {
    const key = getKey( req.params.type );

    core.content.getPage( req, res, listeners[ key ] ).then(( callback ) => {
        // Handshake callback :-P
        callback(( status, html ) => {
            res.status( status ).send( html );
        });
    });
};
const getPreview = function ( req, res ) {
    core.query.getPreview( req, res ).then(( url ) => {
        res.redirect( url );
    });
};



// SYSTEM
expressApp.get( "/preview", getPreview );

// API => JSON
expressApp.get( "/api/:type", getApi );
expressApp.get( "/api/:type/:uid", getApi );

// URI => HTML
expressApp.get( "/", getPage );
expressApp.get( "/:type", getPage );
expressApp.get( "/:type/:uid", getPage );



/**
 *
 * Router API.
 *
 */
module.exports = {
    /**
     *
     * Handle router subscribe.<commit_msg>Set req.params.type to 'home' in router middleware for root '/'<commit_after>"use strict";



const express = require( "express" );
const expressApp = express();
const compression = require( "compression" );
const cookieParser = require( "cookie-parser" );
const lager = require( "properjs-lager" );
const listeners = {};
const core = {
    watch: require( "./watch" ),
    query: require( "./query" ),
    config: require( "../../clutch.config" ),
    content: require( "./content" ),
    template: require( "./template" )
};



/**
 *
 * Configure Express Middleware.
 *
 */
expressApp.use( cookieParser() );
expressApp.use( compression( core.config.compression ) );
expressApp.use( express.static( core.config.template.staticDir, {
    maxAge: core.config.static.maxAge
}));



/**
 *
 * Configure Express Routes.
 *
 */
const getKey = function ( type ) {
    const key = type;

    return key || core.config.homepage;
}
const getApi = function ( req, res ) {
    const key = getKey( req.params.type );

    core.query.getApi( req, res, listeners[ key ] ).then(( result ) => {
        if ( req.query.format === "html" ) {
            res.status( 200 ).send( result );

        } else {
            res.status( 200 ).json( result );
        }
    });
};
const getPage = function ( req, res ) {
    const key = getKey( req.params.type );

    core.content.getPage( req, res, listeners[ key ] ).then(( callback ) => {
        // Handshake callback :-P
        callback(( status, html ) => {
            res.status( status ).send( html );
        });
    });
};
const getPreview = function ( req, res ) {
    core.query.getPreview( req, res ).then(( url ) => {
        res.redirect( url );
    });
};
const setReq = function ( req, res, next ) {
    req.params.type = req.params.type || core.config.homepage;

    next();
};



// SYSTEM
expressApp.get( "/preview", getPreview );

// API => JSON
expressApp.get( "/api/:type", setReq, getApi );
expressApp.get( "/api/:type/:uid", setReq, getApi );

// URI => HTML
expressApp.get( "/", setReq, getPage );
expressApp.get( "/:type", setReq, getPage );
expressApp.get( "/:type/:uid", setReq, getPage );



/**
 *
 * Router API.
 *
 */
module.exports = {
    /**
     *
     * Handle router subscribe.
{'fraction_non_alphanumeric': 0.18132896583996255, 'fraction_numerical': 0.0028076743097800653, 'mean_word_length': 2.629032258064516, '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': '5964587', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1332, 'n_words': 343}