Mercurial Hosting > luan
comparison src/luan/host/WebHandler.java @ 1598:c78d6a4e2d9a
web logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Apr 2021 15:09:51 -0600 |
parents | c922446f53aa |
children | f2a663a4ba9e |
comparison
equal
deleted
inserted
replaced
1597:cd2a0c41b23f | 1598:c78d6a4e2d9a |
---|---|
7 import goodjava.io.IoUtils; | 7 import goodjava.io.IoUtils; |
8 import goodjava.webserver.Handler; | 8 import goodjava.webserver.Handler; |
9 import goodjava.webserver.Request; | 9 import goodjava.webserver.Request; |
10 import goodjava.webserver.Response; | 10 import goodjava.webserver.Response; |
11 import goodjava.webserver.handlers.DomainHandler; | 11 import goodjava.webserver.handlers.DomainHandler; |
12 import goodjava.webserver.handlers.IndexHandler; | |
13 import goodjava.webserver.handlers.ListHandler; | |
14 import goodjava.webserver.handlers.ContentTypeHandler; | |
15 import goodjava.webserver.handlers.SafeHandler; | |
16 import goodjava.webserver.handlers.LogHandler; | |
12 import luan.Luan; | 17 import luan.Luan; |
13 import luan.LuanException; | 18 import luan.LuanException; |
14 import luan.LuanTable; | 19 import luan.LuanTable; |
15 import luan.LuanFunction; | 20 import luan.LuanFunction; |
16 import luan.LuanClosure; | 21 import luan.LuanClosure; |
17 import luan.LuanRuntimeException; | 22 import luan.LuanRuntimeException; |
18 import luan.modules.http.LuanHandler; | 23 import luan.modules.http.LuanHandler; |
24 import luan.modules.http.NotFound; | |
19 import luan.modules.logging.LuanLogger; | 25 import luan.modules.logging.LuanLogger; |
20 | 26 |
21 | 27 |
22 public class WebHandler implements Handler { | 28 public class WebHandler implements Handler { |
23 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); | 29 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); |
30 | |
31 private static final class MyHandler implements Handler { | |
32 private final Handler handler; | |
33 final LuanHandler luanHandler; | |
34 | |
35 MyHandler(Handler handler,LuanHandler luanHandler) { | |
36 this.handler = handler; | |
37 this.luanHandler = luanHandler; | |
38 } | |
39 | |
40 @Override public Response handle(Request request) { | |
41 return handler.handle(request); | |
42 } | |
43 } | |
24 | 44 |
25 private static final DomainHandler.Factory factory = new DomainHandler.Factory() { | 45 private static final DomainHandler.Factory factory = new DomainHandler.Factory() { |
26 public Handler newHandler(String domain) { | 46 public Handler newHandler(String domain) { |
27 File dir = new File(sitesDir,domain); | 47 File dir = new File(sitesDir,domain); |
28 if( !dir.exists() ) | 48 if( !dir.exists() ) |
36 throw new RuntimeException(e); | 56 throw new RuntimeException(e); |
37 } | 57 } |
38 | 58 |
39 Luan luan = new Luan(); | 59 Luan luan = new Luan(); |
40 initLuan(luan,dirStr,domain); | 60 initLuan(luan,dirStr,domain); |
41 return new LuanHandler(luan,domain); | 61 LuanHandler luanHandler = new LuanHandler(luan,domain); |
62 | |
63 Handler notFoundHander = new NotFound(luanHandler); | |
64 Handler handler = new IndexHandler(luanHandler); | |
65 handler = new ListHandler( handler, notFoundHander ); | |
66 handler = new ContentTypeHandler(handler); | |
67 handler = new SafeHandler(handler); | |
68 if( luanLogging ) | |
69 handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir+"2"))); | |
70 | |
71 return new MyHandler(handler,luanHandler); | |
42 } | 72 } |
43 }; | 73 }; |
44 | 74 |
45 public static String securityPassword = "password"; // change for security | 75 public static String securityPassword = "password"; // change for security |
76 public static boolean luanLogging = false; | |
46 private static final DomainHandler domainHandler = new DomainHandler(factory); | 77 private static final DomainHandler domainHandler = new DomainHandler(factory); |
47 private static String sitesDir = null; | 78 private static String sitesDir = null; |
48 | 79 |
49 public WebHandler(String dir) { | 80 public WebHandler(String dir) { |
50 if( sitesDir != null ) | 81 if( sitesDir != null ) |
58 @Override public Response handle(Request request) { | 89 @Override public Response handle(Request request) { |
59 return domainHandler.handle(request); | 90 return domainHandler.handle(request); |
60 } | 91 } |
61 | 92 |
62 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { | 93 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { |
63 LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain); | 94 MyHandler handler = (MyHandler)domainHandler.getHandler(domain); |
64 return luanHandler.call_rpc(fnName,args); | 95 return handler.luanHandler.call_rpc(fnName,args); |
65 } | 96 } |
66 | 97 |
67 private static void initLuan(Luan luan,String dir,String domain) { | 98 private static void initLuan(Luan luan,String dir,String domain) { |
68 LuanLogger.startThreadLogging(luan); | 99 LuanLogger.startThreadLogging(luan); |
69 try { | 100 try { |