changeset 1175:d6d0bd05ad8c

exit on bind conflict, serve "127.0.0.1" for serve_for_nginx
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Feb 2018 14:55:28 -0700
parents bdf27aa2a65c
children 79b1e9ffd0c0
files src/luan/cmd_line.luan src/luan/modules/http/Server.luan src/luan/webserver/Server.java
diffstat 3 files changed, 30 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/cmd_line.luan	Sun Feb 11 17:13:40 2018 -0700
+++ b/src/luan/cmd_line.luan	Mon Feb 12 14:55:28 2018 -0700
@@ -22,10 +22,10 @@
 		function()
 			local main_file = load_file(file)
 			print( main_file( Table.unpack(Luan.arg) ) )
-		end;
+		end
 		catch = function(e)
 --			java(); e.java.printStackTrace();
 			Io.print_to(Io.stderr, e )
-		end;
+		end
 	}
 end
--- a/src/luan/modules/http/Server.luan	Sun Feb 11 17:13:40 2018 -0700
+++ b/src/luan/modules/http/Server.luan	Mon Feb 12 14:55:28 2018 -0700
@@ -4,6 +4,7 @@
 local gsub = String.gsub or error()
 local match = String.match or error()
 local matches = String.matches or error()
+local try = Luan.try or error()
 local Io = require "luan:Io.luan"
 local Package = require "luan:Package.luan"
 local Rpc = require "luan:Rpc.luan"
@@ -24,6 +25,7 @@
 local ListHandler = require "java:luan.webserver.handlers.ListHandler"
 local LuanHandler = require "java:luan.modules.http.LuanHandler"
 local NotFound = require "java:luan.modules.http.NotFound"
+local System = require "java:java.lang.System"
 
 
 local Server = {}
@@ -47,14 +49,22 @@
 	return dir_path
 end
 
-local function start(handler)
+local function start()
+	try {
+		function()
+			LuanHandler.start(Server.server)
+		end
+		catch = function(e)
+--			e.java.printStackTrace();
+			Io.print_to(Io.stderr, e )
+			System.exit(-1)
+		end
+	}
+
 	function Rpc.functions.call(domain,fn_name,...)
 		return Server.luan_handler.call_rpc(fn_name,...)
 	end
 	Thread.fork(Rpc.serve)
-
-	Server.server = JavaServer.new(Server.port,handler)
-	LuanHandler.start(Server.server)
 end
 
 function Server.serve(dir)
@@ -69,7 +79,8 @@
 	handler = ContentTypeHandler.new(handler)
 	handler = SafeHandler.new(handler)
 	handler = LogHandler.new(handler)
-	start(handler)
+	Server.server = JavaServer.new(Server.port,handler)
+	start()
 end
 
 function Server.serve_for_nginx(dir)
@@ -82,7 +93,8 @@
 	handler = ContentTypeHandler.new(handler)
 	handler = SafeHandler.new(handler)
 	handler = LogHandler.new(handler)
-	start(handler)
+	Server.server = JavaServer.Local.new(Server.port,handler)
+	start()
 end
 
 return Server
--- a/src/luan/webserver/Server.java	Sun Feb 11 17:13:40 2018 -0700
+++ b/src/luan/webserver/Server.java	Mon Feb 12 14:55:28 2018 -0700
@@ -3,6 +3,7 @@
 import java.io.IOException;
 import java.net.Socket;
 import java.net.ServerSocket;
+import java.net.InetAddress;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
@@ -57,4 +58,13 @@
 		}
 	}
 
+	public static class Local extends Server {
+		public Local(int port,Handler handler) {
+			super(port,handler);
+		}
+
+		protected ServerSocket newServerSocket() throws IOException {
+			return new ServerSocket(port,0,InetAddress.getByName("127.0.0.1"));
+		}
+	}
 }