<commit_before>import * as Koa from 'koa'
import * as send from 'koa-send'
import * as Router from 'koa-router'
import * as compress from 'koa-compress'
import * as path from 'path'
import docs from './documentation'

const staticRoot = path.resolve(__dirname, '..', 'static')
const serve = (ctx, path) => send(ctx, path, { root: staticRoot, index: 'index.html' })

const api = new Router({ prefix: '/api' })
  .get('/:module/:article', async (ctx, next) => {
    ctx.body = {
      index: await docs.index(),
      markdown: await docs.markdown(ctx.params.module, ctx.params.article)
    }
    return next()
  })

const app = new Koa()
  .use(api.routes())
  .use((ctx, next) =>{
    if (ctx.method != 'HEAD' && ctx.method != 'GET') return next();
    if (/api/i.test(ctx.path)) return next();
    return serve(ctx, ctx.path).catch(() => serve(ctx, '/')).then(next)
  })
  .use(compress())
  .listen(process.env.PORT || 3000)<commit_msg>Redirect to full route from partials<commit_after>import * as Koa from 'koa'
import * as send from 'koa-send'
import * as Router from 'koa-router'
import * as compress from 'koa-compress'
import * as path from 'path'
import * as _ from 'lodash'
import docs from './documentation'

const staticRoot = path.resolve(__dirname, '..', 'static')
const serve = (ctx, path) => send(ctx, path, { root: staticRoot, index: 'index.html' })

const router = new Router()
  .get('/api/:module/:article', async (ctx, next) => {
    const markdown = await docs.markdown(ctx.params.module, ctx.params.article)
    const index = await docs.index()
    ctx.body = { index, markdown }
    return next()
  })
  .get('/docs/:module?/:article?/:chapter?', async (ctx, next) => {
    let { module, article } = ctx.params
    if (module && article) await serve(ctx, '/')
    else {
      const index = await docs.index()
      if (!module) module = index[0].module
      if (!article) article = index.find(i => i.module === module).articles[0].id
      ctx.redirect(`/docs/${module}/${article}`)
    }
    return next()
  })

const app = new Koa()
  .use(router.routes())
  .use((ctx, next) => {
    if (ctx.method != 'HEAD' && ctx.method != 'GET') return next();
    if (/(api|docs)/i.test(ctx.path)) return next();
    return serve(ctx, ctx.path).then(next)
  })
  .use(compress())
  .listen(process.env.PORT || 3000)
{'fraction_non_alphanumeric': 0.18532818532818532, 'fraction_numerical': 0.00429000429000429, 'mean_word_length': 3.450381679389313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '4359833', 'n_tokens_mistral': 794, 'n_tokens_neox': 778, 'n_words': 217}