changeset 1598:c78d6a4e2d9a

web logging
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Apr 2021 15:09:51 -0600
parents cd2a0c41b23f
children f2a663a4ba9e
files src/goodjava/webserver/handlers/LogHandler.java src/luan/host/WebHandler.java src/luan/host/run.luan
diffstat 3 files changed, 36 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/webserver/handlers/LogHandler.java	Fri Mar 26 20:20:21 2021 -0600
+++ b/src/goodjava/webserver/handlers/LogHandler.java	Sun Apr 04 15:09:51 2021 -0600
@@ -21,9 +21,9 @@
 
 
 public final class LogHandler implements Handler {
+	private static final Layout layout = new ListLayout(new DateLayout("yyyy-MM-dd:HH:mm:ss")," ",Layouts.MESSAGE,"\n");
 
 	public static Logger consoleLogger() {
-		Layout layout = new ListLayout(new DateLayout("yyyy-MM-dd:HH:mm:ss")," ",Layouts.MESSAGE,"\n");
 		Appender appender = new ConsoleAppender(layout,System.err);
 		GoodLogger logger = new GoodLogger("HTTP");
 		logger.appender = appender;
@@ -39,8 +39,7 @@
 		} catch(IOException e) {
 			throw new RuntimeException(e);
 		}
-		Layout layout = new ListLayout(new DateLayout("yyyy-MM-dd:HH:mm:ss")," ",Layouts.MESSAGE,"\n");
-		DateFormat fmt = new SimpleDateFormat( "'" + dir + "/'yyyy_MM_dd_HH_mm'.log'" );
+		DateFormat fmt = new SimpleDateFormat( "'" + dir + "/'yyyy_MM_dd'.log'" );
 		Appender appender = new DailyRollingFileAppender(layout,fmt);
 		GoodLogger logger = new GoodLogger("HTTP");
 		logger.appender = appender;
--- a/src/luan/host/WebHandler.java	Fri Mar 26 20:20:21 2021 -0600
+++ b/src/luan/host/WebHandler.java	Sun Apr 04 15:09:51 2021 -0600
@@ -9,6 +9,11 @@
 import goodjava.webserver.Request;
 import goodjava.webserver.Response;
 import goodjava.webserver.handlers.DomainHandler;
+import goodjava.webserver.handlers.IndexHandler;
+import goodjava.webserver.handlers.ListHandler;
+import goodjava.webserver.handlers.ContentTypeHandler;
+import goodjava.webserver.handlers.SafeHandler;
+import goodjava.webserver.handlers.LogHandler;
 import luan.Luan;
 import luan.LuanException;
 import luan.LuanTable;
@@ -16,12 +21,27 @@
 import luan.LuanClosure;
 import luan.LuanRuntimeException;
 import luan.modules.http.LuanHandler;
+import luan.modules.http.NotFound;
 import luan.modules.logging.LuanLogger;
 
 
 public class WebHandler implements Handler {
 	private static final Logger logger = LoggerFactory.getLogger(WebHandler.class);
 
+	private static final class MyHandler implements Handler {
+		private final Handler handler;
+		final LuanHandler luanHandler;
+
+		MyHandler(Handler handler,LuanHandler luanHandler) {
+			this.handler = handler;
+			this.luanHandler = luanHandler;
+		}
+
+		@Override public Response handle(Request request) {
+			return handler.handle(request);
+		}
+	}
+
 	private static final DomainHandler.Factory factory = new DomainHandler.Factory() {
 		public Handler newHandler(String domain) {
 			File dir = new File(sitesDir,domain);
@@ -38,11 +58,22 @@
 
 			Luan luan = new Luan();
 			initLuan(luan,dirStr,domain);
-			return new LuanHandler(luan,domain);
+			LuanHandler luanHandler = new LuanHandler(luan,domain);
+
+			Handler notFoundHander = new NotFound(luanHandler);
+			Handler handler = new IndexHandler(luanHandler);
+			handler = new ListHandler( handler, notFoundHander );
+			handler = new ContentTypeHandler(handler);
+			handler = new SafeHandler(handler);
+			if( luanLogging )
+				handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir+"2")));
+
+			return new MyHandler(handler,luanHandler);
 		}
 	};
 
 	public static String securityPassword = "password";  // change for security
+	public static boolean luanLogging = false;
 	private static final DomainHandler domainHandler = new DomainHandler(factory);
 	private static String sitesDir = null;
 
@@ -60,8 +91,8 @@
 	}
 
 	public static Object callSite(String domain,String fnName,Object... args) throws LuanException {
-		LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain);
-		return luanHandler.call_rpc(fnName,args);
+		MyHandler handler = (MyHandler)domainHandler.getHandler(domain);
+		return handler.luanHandler.call_rpc(fnName,args);
 	}
 
 	private static void initLuan(Luan luan,String dir,String domain) {
--- a/src/luan/host/run.luan	Fri Mar 26 20:20:21 2021 -0600
+++ b/src/luan/host/run.luan	Sun Apr 04 15:09:51 2021 -0600
@@ -7,8 +7,6 @@
 local Hosted = require "luan:host/Hosted.luan"
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "run"
-local NotFound = require "java:luan.modules.http.NotFound"
-local ListHandler = require "java:goodjava.webserver.handlers.ListHandler"
 local WebHandler = require "java:luan.host.WebHandler"
 Hosted.WebHandler = WebHandler
 
@@ -22,16 +20,8 @@
 -- web server
 
 local Server = require "java:goodjava.webserver.Server"
-local IndexHandler = require "java:goodjava.webserver.handlers.IndexHandler"
-local ContentTypeHandler = require "java:goodjava.webserver.handlers.ContentTypeHandler"
-local SafeHandler = require "java:goodjava.webserver.handlers.SafeHandler"
 
 local handler = WebHandler.new(Hosted.sites_dir)
-local not_found_hander = NotFound.new(handler)
-handler = IndexHandler.new(handler)
-handler = ListHandler.new( handler, not_found_hander )
-handler = ContentTypeHandler.new(handler)
-handler = SafeHandler.new(handler)
 local server = Server.ForAddress.new("127.0.0.1",8080,handler)
 server.start()