<commit_before>var restify = require('restify');
var restifyValidation = require('node-restify-validation');
var i18n = require('i18next');

var config = require('./config');
var middleware = require('./middleware');

var feedback = require('./lib/feedback');

var dummyTest = require('./api/dummy');
var account = require('./api/account');
var document = require('./api/document');

function route(mode, callback) {

    var app = config({mode: mode}).app;

    // init for localize
    i18n.init({
        lng: app.lang,
        useCookie: false,
        debug: false,
        sendMissingTo: 'fallback'
    });

    // init restify server
    var options = {
        name: app.name
    };

    var server = restify.createServer(options);

    // globalMiddleware
    server.use(restify.throttle({
        burst: 100,
        rate: 50,
        ip: true,
        overrides: {
            '192.168.1.1': {
                rate: 0,        // unlimited
                burst: 0
            }
        }
    }));

    // api counter for ip district
    server.use(middleware.callCounterForIPs);
    server.use(middleware.callCounterForToken);

    // haroo cloud api document page
    server.get(/^\/(?!api).*$/, restify.serveStatic({
        directory: 'static',
        default: 'index.html'
    }));

    // dummy testing
    server.get('/api', dummyTest.testVersion1);
    server.get('/api/testing', dummyTest.testSimple);
    server.get('/api/testing/:name', dummyTest.testSimpleWithParam);
    server.get('/api/i18n', dummyTest.testI18N);
    server.get('/api/i18n-en', dummyTest.testEnForce);

    // version specified api for only feature test
    server.get({ path: '/api/version', version: '1.0.1'}, dummyTest.testVersion1);
    server.get({ path: '/api/version', version: '1.2.3'}, dummyTest.testVersion1_2_3);
    server.get({ path: '/api/version', version: '2.0.1'}, dummyTest.testVersion2);

    // commonMiddleware
    // set host name to res.locals for all client
    server.use(middleware.accessClient);
    server.use(restify.queryParser());
    server.use(restify.bodyParser());
    server.use(restifyValidation.validationPlugin({
        //errorHandler: middleware.validationError
        // shit, can't set custom res.status by standard features, need a hack. let's do it later.
        // todo: hack restifyValidation errorhandler
    }));
    /*
     patch this and update this middleware
     var handle = function (errors, req, res, options, next) {
        if (options.errorHandler) {
            return options.errorHandler(errors, res);   // for custom res.status
        } else {
            return res.send(400, {
                status: 'validation failed',
                errors: errors
            });
        }
     }
     */

    // header parameter test
    server.get('/api/test-no-header-locals', dummyTest.testCustomParams);
    server.get('/api/test-with-header-locals', dummyTest.testCustomParams);

    // for account
    server.post({ path: '/api/account/create', validation: {<commit_msg>`add` cors<commit_after>var restify = require('restify');
var restifyValidation = require('node-restify-validation');
var i18n = require('i18next');

var config = require('./config');
var middleware = require('./middleware');

var feedback = require('./lib/feedback');

var dummyTest = require('./api/dummy');
var account = require('./api/account');
var document = require('./api/document');

function route(mode, callback) {

    var app = config({mode: mode}).app;

    // init for localize
    i18n.init({
        lng: app.lang,
        useCookie: false,
        debug: false,
        sendMissingTo: 'fallback'
    });

    // init restify server
    var options = {
        name: app.name
    };

    var server = restify.createServer(options);

    // globalMiddleware
    server.use(restify.throttle({
        burst: 100,
        rate: 50,
        ip: true,
        overrides: {
            '192.168.1.1': {
                rate: 0,        // unlimited
                burst: 0
            }
        }
    }));

    // api counter for ip district
    server.use(middleware.callCounterForIPs);
    server.use(middleware.callCounterForToken);

    // haroo cloud api document page
    server.get(/^\/(?!api).*$/, restify.serveStatic({
        directory: 'static',
        default: 'index.html'
    }));

    // dummy testing
    server.get('/api', dummyTest.testVersion1);
    server.get('/api/testing', dummyTest.testSimple);
    server.get('/api/testing/:name', dummyTest.testSimpleWithParam);
    server.get('/api/i18n', dummyTest.testI18N);
    server.get('/api/i18n-en', dummyTest.testEnForce);

    // version specified api for only feature test
    server.get({ path: '/api/version', version: '1.0.1'}, dummyTest.testVersion1);
    server.get({ path: '/api/version', version: '1.2.3'}, dummyTest.testVersion1_2_3);
    server.get({ path: '/api/version', version: '2.0.1'}, dummyTest.testVersion2);

    // commonMiddleware
    // set host name to res.locals for all client
    server.use(middleware.accessClient);
    server.use(restify.CORS());
    server.use(restify.queryParser());
    server.use(restify.bodyParser());
    server.use(restifyValidation.validationPlugin({
        //errorHandler: middleware.validationError
        // shit, can't set custom res.status by standard features, need a hack. let's do it later.
        // todo: hack restifyValidation errorhandler
    }));
    /*
     patch this and update this middleware
     var handle = function (errors, req, res, options, next) {
        if (options.errorHandler) {
            return options.errorHandler(errors, res);   // for custom res.status
        } else {
            return res.send(400, {
                status: 'validation failed',
                errors: errors
            });
        }
     }
     */

    // header parameter test
    server.get('/api/test-no-header-locals', dummyTest.testCustomParams);
    server.get('/api/test-with-header-locals', dummyTest.testCustomParams);

    // for account
    server.post({ path: '/api/account/create', validation: {
{'fraction_non_alphanumeric': 0.1457682826622843, 'fraction_numerical': 0.014790468364831553, 'mean_word_length': 2.4678062678062678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1144354', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1769, 'n_words': 408}