comparison http/src/luan/modules/http/Server.luan @ 503:92c3d22745b8

make _ENV optional
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 May 2015 23:24:46 -0600
parents 2b9bc97f0439
children 9bf9ad733827
comparison
equal deleted inserted replaced
502:d3183a330ff5 503:92c3d22745b8
18 local SessionHandler = require "java:org.eclipse.jetty.server.session.SessionHandler" 18 local SessionHandler = require "java:org.eclipse.jetty.server.session.SessionHandler"
19 local AuthenticationHandler = require "java:luan.modules.http.AuthenticationHandler" 19 local AuthenticationHandler = require "java:luan.modules.http.AuthenticationHandler"
20 local LuanHandler = require "java:luan.modules.http.LuanHandler" 20 local LuanHandler = require "java:luan.modules.http.LuanHandler"
21 local NotFound = require "java:luan.modules.http.NotFound" 21 local NotFound = require "java:luan.modules.http.NotFound"
22 22
23 local M = {}
23 24
24 port = 8080 25 M.port = 8080
25 26
26 private_password = "password" 27 M.private_password = "password"
27 28
28 welcome_file = "index.html" 29 M.welcome_file = "index.html"
29 30
30 31
31 authentication_handler = AuthenticationHandler.new("/private/") 32 M.authentication_handler = AuthenticationHandler.new("/private/")
32 33
33 luan_handler = LuanHandler.new() 34 M.luan_handler = LuanHandler.new()
34 35
35 resource_handler = ResourceHandler.new() 36 M.resource_handler = ResourceHandler.new()
36 resource_handler.setDirectoriesListed(true) 37 M.resource_handler.setDirectoriesListed(true)
37 38
38 handlers = HandlerList.new() 39 M.handlers = HandlerList.new()
39 handlers.setHandlers { authentication_handler, luan_handler, resource_handler } 40 M.handlers.setHandlers { M.authentication_handler, M.luan_handler, M.resource_handler }
40 41
41 function add_folder(context,dir) 42 function M.add_folder(context,dir)
42 local rh = ResourceHandler.new() 43 local rh = ResourceHandler.new()
43 rh.setResourceBase(dir) 44 rh.setResourceBase(dir)
44 rh.setDirectoriesListed(true) 45 rh.setDirectoriesListed(true)
45 local ch = ContextHandler.new(context) 46 local ch = ContextHandler.new(context)
46 ch.setHandler(rh) 47 ch.setHandler(rh)
47 handlers.addHandler(ch) 48 M.handlers.addHandler(ch)
48 return rh 49 return rh
49 end 50 end
50 51
51 handler_wrapper = HandlerWrapper.new() 52 M.handler_wrapper = HandlerWrapper.new()
52 handler_wrapper.setHandler(handlers) 53 M.handler_wrapper.setHandler(M.handlers)
53 54
54 function zip() 55 function M.zip()
55 local h = GzipHandler.new() 56 local h = GzipHandler.new()
56 h.setHandler(handler_wrapper.getHandler()) 57 h.setHandler(M.handler_wrapper.getHandler())
57 handler_wrapper.setHandler(h) 58 M.handler_wrapper.setHandler(h)
58 end 59 end
59 60
60 log = NCSARequestLog.new() 61 M.log = NCSARequestLog.new()
61 log.setExtended(false) 62 M.log.setExtended(false)
62 log_handler = RequestLogHandler.new() 63 M.log_handler = RequestLogHandler.new()
63 log_handler.setRequestLog(log) 64 M.log_handler.setRequestLog(M.log)
64 65
65 function set_log_file(file_name) 66 function M.set_log_file(file_name)
66 log.setFilename(file_name) 67 M.log.setFilename(file_name)
67 end 68 end
68 69
69 local hc = HandlerCollection.new() 70 local hc = HandlerCollection.new()
70 hc.setHandlers { SessionHandler.new(), handler_wrapper, DefaultHandler.new(), log_handler } 71 hc.setHandlers { SessionHandler.new(), M.handler_wrapper, DefaultHandler.new(), M.log_handler }
71 72
72 73
73 function init(dir) 74 function M.init(dir)
74 dir = dir.gsub("/$","") -- remove trailing '/' if any 75 dir = dir.gsub("/$","") -- remove trailing '/' if any
75 Http.dir = dir 76 Http.dir = dir
76 function Io.schemes.site(path,add_extension) 77 function Io.schemes.site(path,add_extension)
77 return Io.uri( dir..path, add_extension ) 78 return Io.uri( dir..path, add_extension )
78 end 79 end
79 authentication_handler.setPassword(private_password) 80 M.authentication_handler.setPassword(M.private_password)
80 local base = dir 81 local base = dir
81 if base.match("^classpath:") ~= nil then 82 if base.match("^classpath:") ~= nil then
82 base = dir.."#"..welcome_file.."#"..welcome_file..".luan" 83 base = dir.."#"..M.welcome_file.."#"..M.welcome_file..".luan"
83 end 84 end
84 resource_handler.setResourceBase(Io.uri(base).to_string()) 85 M.resource_handler.setResourceBase(Io.uri(base).to_string())
85 resource_handler.setWelcomeFiles {welcome_file} 86 M.resource_handler.setWelcomeFiles {M.welcome_file}
86 luan_handler.setWelcomeFile(welcome_file) 87 M.luan_handler.setWelcomeFile(M.welcome_file)
87 handlers.addHandler(NotFound.new()) 88 M.handlers.addHandler(NotFound.new())
88 server = Server.new(port) 89 M.server = Server.new(M.port)
89 server.setHandler(hc) 90 M.server.setHandler(hc)
90 Package.load("site:/init") 91 Package.load("site:/init")
91 end 92 end
92 93
93 function start() 94 function M.start()
94 server.start() 95 M.server.start()
95 end 96 end
96 97
97 function serve(dir) 98 function M.serve(dir)
98 init(dir) 99 M.init(dir)
99 start() 100 M.start()
100 end 101 end
102
103 return M