Mercurial Hosting > luan
comparison src/luan/modules/http/Server.luan @ 1136:d30d400fd43d
add http/jetty
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 29 Jan 2018 17:50:49 -0700 |
parents | 1fcf5c25b3bd |
children | 3a0f58d09ee7 |
comparison
equal
deleted
inserted
replaced
1135:707a5d874f3e | 1136:d30d400fd43d |
---|---|
1 local Luan = require "luan:Luan.luan" | 1 local Implementation = require "luan:http/Implementation.luan" |
2 local error = Luan.error | 2 return require(Implementation.luan.."Server.luan") |
3 local String = require "luan:String.luan" | |
4 local gsub = String.gsub or error() | |
5 local matches = String.matches or error() | |
6 local Io = require "luan:Io.luan" | |
7 local Package = require "luan:Package.luan" | |
8 local Rpc = require "luan:Rpc.luan" | |
9 local Thread = require "luan:Thread.luan" | |
10 local Http = require "luan:http/Http.luan" | |
11 require "luan:logging/init.luan" -- initialize logging | |
12 local Logging = require "luan:logging/Logging.luan" | |
13 local logger = Logging.logger "http/Server" | |
14 | |
15 java() | |
16 local TimeZone = require "java:java.util.TimeZone" | |
17 local JavaServer = require "java:org.eclipse.jetty.server.Server" | |
18 local NCSARequestLog = require "java:org.eclipse.jetty.server.NCSARequestLog" | |
19 local DefaultHandler = require "java:org.eclipse.jetty.server.handler.DefaultHandler" | |
20 local HandlerList = require "java:org.eclipse.jetty.server.handler.HandlerList" | |
21 local HandlerCollection = require "java:org.eclipse.jetty.server.handler.HandlerCollection" | |
22 local ResourceHandler = require "java:org.eclipse.jetty.server.handler.ResourceHandler" | |
23 local RequestLogHandler = require "java:org.eclipse.jetty.server.handler.RequestLogHandler" | |
24 local ContextHandler = require "java:org.eclipse.jetty.server.handler.ContextHandler" | |
25 local GzipHandler = require "java:org.eclipse.jetty.server.handler.GzipHandler" | |
26 local HandlerWrapper = require "java:org.eclipse.jetty.server.handler.HandlerWrapper" | |
27 local SessionHandler = require "java:org.eclipse.jetty.server.session.SessionHandler" | |
28 local AuthenticationHandler = require "java:luan.modules.http.AuthenticationHandler" | |
29 local LuanHandler = require "java:luan.modules.http.LuanHandler" | |
30 local NotFound = require "java:luan.modules.http.NotFound" | |
31 | |
32 local Server = {} | |
33 | |
34 Server.port = 8080 | |
35 | |
36 Server.welcome_file = "index.html" | |
37 | |
38 | |
39 Server.authentication_handler = AuthenticationHandler.new("/private/") | |
40 | |
41 Server.luan_handler = LuanHandler.new() | |
42 | |
43 Server.resource_handler = ResourceHandler.new() | |
44 Server.resource_handler.setDirectoriesListed(true) | |
45 | |
46 Server.handlers = HandlerList.new() | |
47 Server.handlers.setHandlers { Server.authentication_handler, Server.luan_handler, Server.resource_handler } | |
48 | |
49 function Server.add_folder(context,dir) | |
50 local rh = ResourceHandler.new() | |
51 rh.setResourceBase(dir) | |
52 rh.setDirectoriesListed(true) | |
53 local ch = ContextHandler.new(context) | |
54 ch.setHandler(rh) | |
55 Server.handlers.addHandler(ch) | |
56 return rh | |
57 end | |
58 | |
59 Server.handler_wrapper = HandlerWrapper.new() | |
60 Server.handler_wrapper.setHandler(Server.handlers) | |
61 | |
62 function Server.zip() | |
63 local h = GzipHandler.new() | |
64 h.setHandler(Server.handler_wrapper.getHandler()) | |
65 Server.handler_wrapper.setHandler(h) | |
66 end | |
67 | |
68 Server.log = NCSARequestLog.new() | |
69 Server.log.setExtended(false) | |
70 Server.log.setLogTimeZone(TimeZone.getDefault().getID()) | |
71 Server.log_handler = RequestLogHandler.new() | |
72 Server.log_handler.setRequestLog(Server.log) | |
73 | |
74 function Server.set_log_file(file_name) | |
75 Server.log.setFilename(file_name) | |
76 end | |
77 | |
78 local hc = HandlerCollection.new() | |
79 hc.setHandlers { SessionHandler.new(), Server.handler_wrapper, DefaultHandler.new(), Server.log_handler } | |
80 | |
81 | |
82 function Server.init(dir) | |
83 dir = gsub(dir,"/$","") -- remove trailing '/' if any | |
84 Http.dir = dir | |
85 function Io.schemes.site(path) | |
86 return Io.uri( dir..path ) | |
87 end | |
88 Server.authentication_handler.setPassword(Io.password) | |
89 local base = dir | |
90 if matches(base,"^classpath:") then | |
91 base = dir.."#"..Server.welcome_file.."#"..Server.welcome_file..".luan" | |
92 end | |
93 Server.resource_handler.setResourceBase(Io.uri(base).to_string()) | |
94 Server.resource_handler.setWelcomeFiles {Server.welcome_file} | |
95 Server.luan_handler.setWelcomeFile(Server.welcome_file) | |
96 Server.handlers.addHandler(NotFound.new(Server.luan_handler)) | |
97 Server.server = JavaServer.new(Server.port) | |
98 Server.server.setHandler(hc) | |
99 end | |
100 | |
101 function Server.start() | |
102 LuanHandler.start(Server.server) | |
103 end | |
104 | |
105 function Server.start_rpc() | |
106 function Rpc.functions.call(domain,fn_name,...) | |
107 return Server.luan_handler.call_rpc(fn_name,...) | |
108 end | |
109 | |
110 Thread.fork(Rpc.serve) | |
111 end | |
112 | |
113 function Server.serve(dir) | |
114 Server.init(dir) | |
115 Server.start_rpc() | |
116 Server.start() | |
117 end | |
118 | |
119 return Server |