<commit_before>var µ=require("Morgas");

var http=require("http");

var SC=µ.shortcut({
	File:"File",
	fileUtils:"File.util",
	adopt:"adopt",
	fillResponse:require.bind(null,"./lib/fillResponse"),
	gui:require.bind(null,"MorgasGui")
});

var LOG=require("./logger");
var logger=LOG.setCoreLogger(LOG("main"));
var config=require("./config");
var backGarden=require("./lib/backGarden");

var handleRequest=function(request,response)
{
	logger.info({url:request.url},"access");
	if(request.url==="/")
	{//redirect
	}
	else
	{
		var requestPath=request.url.slice(1).split("/");
		if(requestPath.length==1||requestPath[1]=="")
		{
			var newUrl="http://"+request.headers['host']+"/"+requestPath[0]+"/index.html";
			logger.info(`redirect ${request.url} to ${newUrl}`);
			SC.fillResponse(response,http.STATUS_CODES[302]+ '. Redirecting to ' + requestPath[0]+"/index.html",{
				'Location': newUrl
			},302);
		}
		else if(requestPath[0]=="morgas")
		{// morgas sources
			handleMorgasSources(request,response,requestPath);
		}
		else if(backGarden.has(requestPath[0]))
		{//app
			backGarden.handleRequest(requestPath[0],request,response,requestPath.slice(1));
			/*
			var app=activeApps.get(requestPath[0]);
			if(requestPath.length==1) requestPath.push("index.html");
			if(requestPath[1]=="rest")
			{//restService
				app.rest(request,requestPath.slice(2))
				.then(result=>SC.fillResponse(response,result.data,result.headers,result.status),
				error=>SC.fillResponse(response,error.error,error.headers,error.status||400));
			}
			else if(requestPath[1]=="event")
			{//event source
				app.eventSource(request,requestPath.slice(2).join("/").replace(/\?.*
				/,""),response);
			}
			else
			{//static resource
				SC.fillResponse(response,app.folder.clone().changePath(requestPath.slice(1).join("/").replace(/\?.*
				/,"")));
			}
			*/
		}
		else
		{//unknown app
			SC.fillResponse(response,`no such app "${requestPath[0]}"`,null,501)
		}
	}
};
var getStyle=function(path)
{
	path=path.split("?");
	path=push("default");
	return new SC.Promise([
		new SC.File(µ.gui.dirname).changePath("less/theme/"+path[1]).read(),
		new SC.File(µ.gui.dirname).changePath("less/structure/"+path[0]).read(),
		new SC.File(µ.gui.dirname).changePath("less/style/"+path[0]).read(),
	]).then(function(theme,structure,style)
	{
		return SC.less.render(theme+"\n"+structure+"\n"+style);
	});
}
var handleMorgasSources=function(request,response,requestPath)
{
	if(requestPath[1]=="gui")<commit_msg>cleanup<commit_after>var µ=require("Morgas");

var http=require("http");

var SC=µ.shortcut({
	File:"File",
	fileUtils:"File.util",
	adopt:"adopt",
	fillResponse:require.bind(null,"./lib/fillResponse"),
	gui:require.bind(null,"MorgasGui")
});

var LOG=require("./logger");
var logger=LOG.setCoreLogger(LOG("main"));
var config=require("./config");
var backGarden=require("./lib/backGarden");

var handleRequest=function(request,response)
{
	logger.info({url:request.url},"access");
	if(request.url==="/")
	{//redirect
	}
	else
	{
		var requestPath=request.url.slice(1).split("/");
		if(requestPath.length==1||requestPath[1]=="")
		{
			var newUrl="http://"+request.headers['host']+"/"+requestPath[0]+"/index.html";
			logger.info(`redirect ${request.url} to ${newUrl}`);
			SC.fillResponse(response,http.STATUS_CODES[302]+ '. Redirecting to ' + requestPath[0]+"/index.html",{
				'Location': newUrl
			},302);
		}
		else if(requestPath[0]=="morgas")
		{// morgas sources
			handleMorgasSources(request,response,requestPath);
		}
		else if(backGarden.has(requestPath[0]))
		{//app
			backGarden.handleRequest(requestPath[0],request,response,requestPath.slice(1));
		}
		else
		{//unknown app
			SC.fillResponse(response,`no such app "${requestPath[0]}"`,null,501)
		}
	}
};
var getStyle=function(path)
{
	path=path.split("?");
	path=push("default");
	return new SC.Promise([
		new SC.File(µ.gui.dirname).changePath("less/theme/"+path[1]).read(),
		new SC.File(µ.gui.dirname).changePath("less/structure/"+path[0]).read(),
		new SC.File(µ.gui.dirname).changePath("less/style/"+path[0]).read(),
	]).then(function(theme,structure,style)
	{
		return SC.less.render(theme+"\n"+structure+"\n"+style);
	});
}
var handleMorgasSources=function(request,response,requestPath)
{
	if(requestPath[1]=="gui")
{'fraction_non_alphanumeric': 0.21730994152046784, 'fraction_numerical': 0.013099415204678362, 'mean_word_length': 5.629457364341086, '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': '386406', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1549, 'n_words': 170}