<commit_before>#!/usr/bin/env node

import koa from 'koa'
import program from 'commander'
import chalk from 'chalk'
import { execSync } from 'child_process'
import enableDestroy from 'server-destroy'
import http from 'http'

import serve from 'koa-static'
import mount from 'koa-mount'
import compress from 'koa-compress'
import handlebars from 'koa-handlebars'
import koaRouter from 'koa-router'
import sendfile from 'koa-sendfile'
import bodyParser from 'koa-body'
import jwt from 'koa-jwt'

import { rope } from '../lib/db'
import helpers from '../lib/helpers'
import { log, isSite, exists } from '../lib/utils'
import controller from '../lib/controller'

const router = koaRouter()
const app = koa()

program
  .option('-w, --watch', 'Rebuild site if files change')
  .option('-p, --port <port>', 'The port on which your site will be available', parseInt)
  .parse(process.argv)

if (!isSite()) {
  log(chalk.red('No site in here!'))
  process.exit(1)
}

// Build before serving if "dist" directory doesn't exist
if (!exists(process.cwd() + '/dist')) {
  try {
    execSync('muffin build', {stdio: [0, 1]})
  } catch (err) {
    log(err)
    process.exit(1)
  }
}

app.use(compress())

router.use('/api', jwt({
  secret: process.env.SESSION_SECRET
}).unless({
  path: [/token-auth/, /token-refresh/, /reset-password/]
}))

router.use(bodyParser({
  multipart: true
}))

function getRoutes (path) {
  // Retrieve routes from passed path
  return require('../lib/routes/' + path).routes()
}

const APIroutes = [
  'content',
  'reset-password',
  'token',
  'upload'
]

// Register media routes and API
router.use('/uploads*', getRoutes('uploads'))

for (let route of APIroutes) {
  router.use('/api', getRoutes('api/' + route))
}

// Serve assets of admin area...
app.use(mount('/admin', serve(__dirname + '/../dist')))

// ...and the Ember app
router.get('/admin*', function *() {
  yield* sendfile.call(this, __dirname + '/../dist/index.html')
  if (!this.status) this.throw(404)
})

// Serve frontend assets
app.use(mount('/assets', serve(process.cwd() + '/dist')))

router.get('/login', function *(next) {
  yield next
  this.redirect('/admin/login')
})

// Log HTTP requests to console
app.use(function *(next){
  let start = new Date
  yield next
  let ms = new Date - start

  if (this.url.split('/')[1] == 'api') {
    return
  }

  console.log(chalk.blue('[muffin]') + ' %s %s - %sms', this.method, this.url, ms)
})

// Load front routes
import frontRouter from '../lib/routes/front'
<commit_msg>Load ember app from correct path<commit_after>#!/usr/bin/env node

import koa from 'koa'
import program from 'commander'
import chalk from 'chalk'
import { execSync } from 'child_process'
import enableDestroy from 'server-destroy'
import http from 'http'

import serve from 'koa-static'
import mount from 'koa-mount'
import compress from 'koa-compress'
import handlebars from 'koa-handlebars'
import koaRouter from 'koa-router'
import sendfile from 'koa-sendfile'
import bodyParser from 'koa-body'
import jwt from 'koa-jwt'

import { rope } from '../lib/db'
import helpers from '../lib/helpers'
import { log, isSite, exists } from '../lib/utils'
import controller from '../lib/controller'

const router = koaRouter()
const app = koa()

program
  .option('-w, --watch', 'Rebuild site if files change')
  .option('-p, --port <port>', 'The port on which your site will be available', parseInt)
  .parse(process.argv)

if (!isSite()) {
  log(chalk.red('No site in here!'))
  process.exit(1)
}

// Build before serving if "dist" directory doesn't exist
if (!exists(process.cwd() + '/dist')) {
  try {
    execSync('muffin build', {stdio: [0, 1]})
  } catch (err) {
    log(err)
    process.exit(1)
  }
}

app.use(compress())

router.use('/api', jwt({
  secret: process.env.SESSION_SECRET
}).unless({
  path: [/token-auth/, /token-refresh/, /reset-password/]
}))

router.use(bodyParser({
  multipart: true
}))

function getRoutes (path) {
  // Retrieve routes from passed path
  return require('../lib/routes/' + path).routes()
}

const APIroutes = [
  'content',
  'reset-password',
  'token',
  'upload'
]

// Register media routes and API
router.use('/uploads*', getRoutes('uploads'))

for (let route of APIroutes) {
  router.use('/api', getRoutes('api/' + route))
}

// Serve assets of admin area...
app.use(mount('/admin', serve(__dirname + '/../client')))

// ...and the Ember app
router.get('/admin*', function *() {
  yield* sendfile.call(this, __dirname + '/../client/index.html')
  if (!this.status) this.throw(404)
})

// Serve frontend assets
app.use(mount('/assets', serve(process.cwd() + '/dist')))

router.get('/login', function *(next) {
  yield next
  this.redirect('/admin/login')
})

// Log HTTP requests to console
app.use(function *(next){
  let start = new Date
  yield next
  let ms = new Date - start

  if (this.url.split('/')[1] == 'api') {
    return
  }

  console.log(chalk.blue('[muffin]') + ' %s %s - %sms', this.method, this.url, ms)
})

// Load front routes
import frontRouter from '../lib/routes/front'

{'fraction_non_alphanumeric': 0.17701924985116094, 'fraction_numerical': 0.0031752331811867435, 'mean_word_length': 3.7457627118644066, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3693667', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1726, 'n_words': 497}