<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

/*
 * lib/server/sapi.js: SAPI server initialization and shutdown
 */

var assert = require('assert-plus');
var async = require('async');
var http = require('http');
var https = require('https');
var restify = require('restify');

var endpoints = require('./endpoints');
var Model = require('./model');

function SAPI(config) {
    assert.object(config.log, 'config.log');

    this.config = config;
    this.log = config.log;

    /* Default to listening on port 80 */
    if (!this.config.port)
        this.config.port = 80;

    this.model = new Model(this.config);
}

SAPI.prototype.start = function start(cb) {
    var self = this;
    var log = self.log;

    http.globalAgent.maxSockets = self.config.maxHttpSockets || 100;
    https.globalAgent.maxSockets = self.config.maxHttpSockets || 100;

    async.waterfall([
        function (subcb) {
            self.model.initClients(subcb);
        },
        function (subcb) {
            var server_opts = {};
            server_opts.model = self.model;
            server_opts.log = self.log;

            var server = self.server = createServer(server_opts);

            server.listen(self.config.port, function () {
                log.info('%s listening at %s',
                    server.name, server.url);
                subcb();
            });
        }
    ], cb);
};

SAPI.prototype.shutdown = function shutdown(cb) {
    var self = this;
    var log = self.log;

    log.info('starting shutdown');

    async.parallel([
        function (subcb) {
            self.model.close(subcb);
        },
        function (subcb) {
            self.server.close(subcb);
        }
    ], function () {
        log.info('shutdown completed');
        cb();
    });
};

function createServer(options) {
    var server = restify.createServer({
        name: 'Services API',
        log: options.log,
        version: ['1.0.0', '2.0.0']
    });

    server.pre(function (req, res, next) {
        /**
         * If the client does not set Accept-Version, then we default
         * the header to "~1".
         *
         * *Could* do:
         *    req.headers['accept-version'] = '~1'
         * but that lies in the audit log. Would like a
         * `req.setVersion()` in restify instead of hacking private
         * `req._version`.
         */
        if (req.headers['accept-version'] === undefined) {
            req._version = '~1';<commit_msg>SAPI-254: hang in stage-test-sapi the last two nightly runs (logging to help narrow down hang)<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

/*
 * lib/server/sapi.js: SAPI server initialization and shutdown
 */

var assert = require('assert-plus');
var async = require('async');
var http = require('http');
var https = require('https');
var restify = require('restify');

var endpoints = require('./endpoints');
var Model = require('./model');

function SAPI(config) {
    assert.object(config.log, 'config.log');

    this.config = config;
    this.log = config.log;

    /* Default to listening on port 80 */
    if (!this.config.port)
        this.config.port = 80;

    this.model = new Model(this.config);
}

SAPI.prototype.start = function start(cb) {
    var self = this;
    var log = self.log;

    http.globalAgent.maxSockets = self.config.maxHttpSockets || 100;
    https.globalAgent.maxSockets = self.config.maxHttpSockets || 100;

    async.waterfall([
        function (subcb) {
            self.model.initClients(subcb);
        },
        function (subcb) {
            var server_opts = {};
            server_opts.model = self.model;
            server_opts.log = self.log;

            var server = self.server = createServer(server_opts);

            server.listen(self.config.port, function () {
                log.info('%s listening at %s',
                    server.name, server.url);
                subcb();
            });
        }
    ], cb);
};

SAPI.prototype.shutdown = function shutdown(cb) {
    var self = this;
    var log = self.log;

    log.info('shutdown: start');
    async.parallel([
        function (subcb) {
            log.info('shutdown: closing model');
            self.model.close(subcb);
        },
        function (subcb) {
            log.info('shutdown: closing server');
            self.server.close(subcb);
        }
    ], function () {
        log.info('shutdown: complete');
        cb();
    });
};

function createServer(options) {
    var server = restify.createServer({
        name: 'Services API',
        log: options.log,
        version: ['1.0.0', '2.0.0']
    });

    server.pre(function (req, res, next) {
        /**
         * If the client does not set Accept-Version, then we default
         * the header to "~1".
         *
         * *Could* do:
         *    req.headers['accept-version'] = '~1'
         * but that lies in the audit log. Would like a
         * `req.setVersion()` in restify instead of hacking private
         * `req._version`.
         */
        if (req.headers['accept-version'] === undefined) {
            req._version = '~1';
{'fraction_non_alphanumeric': 0.1403001265593925, 'fraction_numerical': 0.01030555053335744, 'mean_word_length': 2.0716268739589117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6373062', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1628, 'n_words': 437}