<commit_before>const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const views = require('koa-views');
// const co = require('co');
const convert = require('koa-convert');
const json = require('koa-json')();
// const onerror = require('koa-onerror');
const bodyparser = require('koa-bodyparser')();
// const koabody = require('koa-body')();
const logger = require('koa-logger')();
const index = require('./routes/index');
// const users = require('./routes/users');
// const register = require('./routes/register');
const api = require('./routes/api');
const users = require('./routes/users');

const jwtKoa = require('koa-jwt');
// const util = require('util');
const secret = require('../secret-key').secret;

const mysqlConnection = require('./lib/db');

// middlewares
// app.use(convert(koabody));
app.use(convert(bodyparser));
app.use(convert(json));
app.use(convert(logger));

app.use(require('koa-static')(`${__dirname}/public`));
//
app.use(views(`${__dirname}/views`, {
    extension: 'jade'
}));

// Add mysqlConnection object into the context
app.context.db = mysqlConnection;

// logger
app.use((ctx, next) => {
    const start = new Date();
    return next().then(() => {
        const ms = new Date() - start;
        console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
    });
});

/**
 * 应用异常处理
 */
app.use((ctx, next) => {
    return next().catch((err) => {
        if (err.status === 401) {
            ctx.status = 401;
            ctx.body = `Protected resource, ${err}\n`;
        } else {
            throw err;
        }
    });
});

/**
 * jwt 认证
 */
app.use(jwtKoa({ 
    secret, 
    cookie: 'authorization'
}).unless({
    path: [/^\/users\/login/] // 数组中的路径不需要通过jwt验证
}));

// define router address
// router.use('/users', users.routes(), users.allowedMethods());
// router.use('/register', register.routes(), register.allowedMethods());

router.use('/users', users.routes(), users.allowedMethods());

router.use('/api', api.routes(), api.allowedMethods());
router.use('*', index.routes(), index.allowedMethods());

// register router
app.use(router.routes(), router.allowedMethods());
// response

app.on('error', (err, ctx) => {
    const context = ctx;
    console.log(err);
    context.body = err;
    logger.error('server error', err, ctx);
});


module.exports = app;
<commit_msg>🚗 移除数据库连接<commit_after>const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const views = require('koa-views');
// const co = require('co');
const convert = require('koa-convert');
const json = require('koa-json')();
// const onerror = require('koa-onerror');
const bodyparser = require('koa-bodyparser')();
// const koabody = require('koa-body')();
const logger = require('koa-logger')();
const index = require('./routes/index');
// const users = require('./routes/users');
// const register = require('./routes/register');
const api = require('./routes/api');
const users = require('./routes/users');

const jwtKoa = require('koa-jwt');
// const util = require('util');
const secret = require('../secret-key').secret;

// const mysqlConnection = require('./lib/db');

// middlewares
// app.use(convert(koabody));
app.use(convert(bodyparser));
app.use(convert(json));
app.use(convert(logger));

app.use(require('koa-static')(`${__dirname}/public`));
//
app.use(views(`${__dirname}/views`, {
    extension: 'jade'
}));

// Add mysqlConnection object into the context
// app.context.db = mysqlConnection;

// logger
app.use((ctx, next) => {
    const start = new Date();
    return next().then(() => {
        const ms = new Date() - start;
        console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
    });
});

/**
 * 应用异常处理
 */
app.use((ctx, next) => {
    return next().catch((err) => {
        if (err.status === 401) {
            ctx.status = 401;
            ctx.body = `Protected resource, ${err}\n`;
        } else {
            throw err;
        }
    });
});

/**
 * jwt 认证
 */
app.use(jwtKoa({ 
    secret, 
    cookie: 'authorization'
}).unless({
    path: [/^\/users\/login/] // 数组中的路径不需要通过jwt验证
}));

// define router address
// router.use('/users', users.routes(), users.allowedMethods());
// router.use('/register', register.routes(), register.allowedMethods());

router.use('/users', users.routes(), users.allowedMethods());

router.use('/api', api.routes(), api.allowedMethods());
router.use('*', index.routes(), index.allowedMethods());

// register router
app.use(router.routes(), router.allowedMethods());
// response

app.on('error', (err, ctx) => {
    const context = ctx;
    console.log(err);
    context.body = err;
    logger.error('server error', err, ctx);
});


module.exports = app;

{'fraction_non_alphanumeric': 0.22026338147833474, 'fraction_numerical': 0.002548853016142736, 'mean_word_length': 3.5453667953667956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2603616', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1564, 'n_words': 321}