import {resolve, relative, basename} from 'path';
import {listTree, split, join, directory} from 'q-io/fs';
import {request} from 'q-io/http';

export default function indexRouteFactory (application, configuration) {

	return async function indexRoute () {
		let routeConfig = application.configuration.routes.enabled;
		let serverConfig = application.configuration.server;

		let baseURI = `http://${serverConfig.host}:${serverConfig.port}`;

		let routes = Object.keys(routeConfig)
			.filter((routeName) => routeConfig[routeName].enabled === true)
			.map(function(routeName){
				return { 'name': routeName, 'path': routeConfig[routeName].path, 'uri': `${baseURI}${routeConfig[routeName].path}`.replace('*', '') }
			});

		let metaRoute = routes.filter((item) => item.name === 'meta')[0];
		let response = await request(metaRoute.uri);
		let meta = await response.body.read();

		meta = JSON.parse(meta.toString('utf-8'));

		this.type = 'json';
		this.body = Object.assign({}, {
			'name': application.configuration.pkg.name,
			'version': application.configuration.pkg.version,
			'environment': application.configuration.environment,
			'host': serverConfig.host,
			'port': serverConfig.port,
			'routes': routes,
			'meta': meta
		});
	};
}

@@ -12,28 +12,8 @@
 olve
-, relative, basename
 %7D fr
@@ -35,40 +35,20 @@
 rt %7B
-listTree, split, join, directory
+exists, read
 %7D fr
@@ -95,16 +95,81 @@
 o/http';
+%0Aimport marked from 'marked';%0Aimport %7Bpromisify%7D from 'bluebird';
 %0A%0Aexport
@@ -234,16 +234,53 @@
 ation) %7B
+%0A%09const markdown = promisify(marked);
 %0A%0A%09retur
@@ -938,16 +938,291 @@
 ead();%0A%0A
+%09%09let readmePath = resolve(application.runtime.cwd, 'patterns', 'README.md');%0A%09%09var readme = '';%0A%0A%09%09if (await exists(readmePath)) %7B%0A%09%09%09let readMeSource = await read(readmePath);%0A%09%09%09readMeSource = readMeSource.toString('utf-8');%0A%09%09%09readme = await markdown(readMeSource);%0A%09%09%7D%0A%0A
 %09%09meta =
@@ -1259,16 +1259,17 @@
 -8'));%0A%0A
+%0A
 %09%09this.t
@@ -1569,16 +1569,37 @@
 a': meta
+,%0A%09%09%09'readme': readme
 %0A%09%09%7D);%0A%09

Send parsed readme along with index route