<commit_before>
'use strict';

const koa = require('koa');
const bodyParser = require('koa-bodyparser');
const router = require('koa-router')();
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/articles');
const app = koa();

import { User } from './models';

app.use(bodyParser());

router.get('/', function *(next) {
  this.body = 'ohcE';
});

router.get('/me', function *(next) {
  const me = yield User.findOne({ 'alias': this.request.headers.alias }).exec();
  this.body = me;
});

router.post('/me', function *(next) {
  const reqAlias = this.request.body.alias;
  if (!reqAlias || /^\s*$/.exec(reqAlias)) {
    this.throw(`need a valid alias: "${reqAlias}"`, 400);
  }
  let me = yield User.findOne({ 'alias': reqAlias }).exec();
  if (!me) {
    me = new User({
      alias: reqAlias,
    });
    yield me.save();
    this.status = 201;
  }
  this.body = me;
});

app.use(router.routes());

const server = app.listen(8000, () => {
  const host = server.address().address;
  const port = server.address().port;
<commit_msg>middleware for user fetching<commit_after>
'use strict';

const koa = require('koa');
const bodyParser = require('koa-bodyparser');
const router = require('koa-router')();
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/articles');
const app = koa();

import { User } from './models';

app.use(bodyParser());

router.get('/', function *(next) {
  this.body = 'ohcE';
});

router.use(function *(next) {
  if (!this.request.headers.alias) {
    yield next;
    return;
  }
  const me = yield User.findOne({ 'alias': this.request.headers.alias }).exec();
  if (me) {
    this.user = me;
  }
  yield next;
});

router.get('/me', function *(next) {
  this.body = this.user;
});

router.post('/me', function *(next) {
  const reqAlias = this.request.headers.alias;
  if (!reqAlias || /^\s*$/.exec(reqAlias)) {
    this.throw(`need a valid alias: "${reqAlias}"`, 400);
  }
  if (!this.user) {
    this.user = new User({
      alias: reqAlias,
    });
    yield this.user.save();
    this.status = 201;
  }
  this.body = this.user;
});

app.use(router.routes());

const server = app.listen(8000, () => {
  const host = server.address().address;
  const port = server.address().port;

{'fraction_non_alphanumeric': 0.20468611847922194, 'fraction_numerical': 0.008841732979664015, 'mean_word_length': 3.665979381443299, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1833463', 'n_tokens_mistral': 792, 'n_tokens_neox': 777, 'n_words': 181}