import process.ruby;
import wsock.tcp.simpleHttpServer;

namespace process.ruby.simpleHttpServer;

startUrl = function(path,documentRoot,startPort){
   
    var cgiPath = ..process.ruby.install();
 	if( ! ..io.exist(cgiPath) ){
 		error("请先调用 process.ruby.path 指定 Ruby 路径")
 	}
 	
 	..wsock.tcp.simpleHttpServer.customErrors = self.customErrors;
 	..wsock.tcp.simpleHttpServer.threadNum = self.threadNum;
 	..wsock.tcp.simpleHttpServer.startPort = startPort : self.startPort;
 	..wsock.tcp.simpleHttpServer.startIp = self.startIp;
 	..wsock.tcp.simpleHttpServer.defalutDocument = "index.rb";
 	..wsock.tcp.simpleHttpServer.threadGlobal = {
 		rubyCgiPath = cgiPath; 
 	}
 	
 	..wsock.tcp.simpleHttpServer.startPort = 8080
 	return ..wsock.tcp.simpleHttpServer.startUrl(path,documentRoot,self.app); 
}

app = function(response,request,session){
	import process.ruby.cgi;
	if( string.endWith(request.path,".rb",true) ){
		process.ruby.cgi(response,request,rubyCgiPath);		
	}
	else {
		response.loadcode( request.path );
	}
}

/**intellisense(process.ruby.simpleHttpServer)
startUrl( =  查找可用端口创建PHP/CGI服务器，返回返回完整URL\n此服务端限制使用本机IP127.0.0.1访问,并随机分配端口不会出现端口冲突\n如果Ruby/CGI服务器已启动则直接返回URL而不是重复启动服务器,\n注意当前线程结束时,此服务器线程会自动退出
startUrl(.(path,documentRoot,startPort) = 查找可用端口创建PHP/CGI服务器，返回返回完整URL\n如果PHP/CGI服务器已启动则直接返回URL而不是重复启动服务器,\n省略参数返回首页URL,尾部不包含斜杠\n可选用@path参数指定请求目标文件的相对路径\n可选使用参数@documentRoot指定网站根目录,默认为"/"\n可选用 startPort 指定启动端口
threadNum = 设置服务器线程数，默认为2个线程
startPort = 设置服务器端口,默认自动分配空闲端口
startIp = 设置服务器端IP,\n默认为"127.0.0.1"
customErrors = @.customErrors = {\n    [404] = function(response){\n		response.status = "404 Not Found";\n		response.write("404 Not Found"); __/*自定义错误页*/\n	} \n}
startPort = 服务端口,不指定自动分配空闲端口
end intellisense**/