
#include "../http.h"

#include "./fileSystem.h"

#include "./text.h"

#include "./mimeTypes.h"

#include "./cache.h"


struct cache * cacheManager;



void handleRequest( struct request * requestInstance, struct text * response ) {

	text * filePath			= new text("www/");

	filePath				+= requestInstance->url;


		if( filesystem->isDirectory( filePath->value ) ) {

			filePath->value += "index.html";

			requestInstance->mimeType = "text/html";

		}

		text * content 		= cacheManager->getFile( filePath->value );

		//text * content 		= filesystem->readFile( filePath->value, "binary" );

		response			+= "HTTP/1.0 200 OK\r\n";

		response			+= "Server: webserver-c\r\n";

		response			+= "Content-type: " + requestInstance->mimeType + "\r\n\r\n";

		printf("Filename:		%s\n", filePath->value);

		printf("Source:		%s\n", content->value);

		response->appendObject( content );

		response			+= "\r\n";



	filePath->free();

}


void main() {


	cacheManager = new cache();


	
	http * serverInstance = new http();

	serverInstance->createServer( handleRequest );

	serverInstance->listen( 8080 );

}