comparison serve.luan @ 0:dfc36e7ed22c

init
author Vadim Filimonov <fffilimonov@yandex.ru>
date Thu, 12 May 2022 13:51:59 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfc36e7ed22c
1 local dir = "src"
2 local port = 8080
3
4
5 require "java"
6 local Luan = require "luan:Luan.luan"
7 local error = Luan.error
8 local ipairs = Luan.ipairs or error()
9
10
11 local log_to_console = false
12 for _, arg in ipairs{...} do
13 if arg == "console" then
14 log_to_console = true
15 end
16 end
17 if not log_to_console then
18 local FileWriter = require "java:java.io.FileWriter"
19 local LuanLogger = require "java:luan.modules.logging.LuanLogger"
20 local Layouts = require "java:goodjava.logger.Layouts"
21 local DateLayout = require "java:goodjava.logger.DateLayout"
22 local ListLayout = require "java:goodjava.logger.ListLayout"
23 local Level = require "java:goodjava.logger.Level"
24 local WriterAppender = require "java:goodjava.logger.WriterAppender"
25 local LevelAppender = require "java:goodjava.logger.LevelAppender"
26 local ListAppender = require "java:goodjava.logger.ListAppender"
27
28 local layout = ListLayout.new(DateLayout.new("yyyy-MM-dd HH:mm:ss,SSS")," ",Layouts.LEVEL_PADDED," ",Layouts.LOGGER," - ",Layouts.MESSAGE,"\n",Layouts.THROWABLE)
29
30 local function new_appender(file,level)
31 local writer = FileWriter.new(file,true)
32 local appender = WriterAppender.new(layout,writer)
33 appender = LevelAppender.new(appender,level)
34 return appender
35 end
36
37 local err = new_appender("logs/luan_error.log",Level.ERROR)
38 local warn = new_appender("logs/luan_warn.log",Level.WARN)
39 local info = new_appender("logs/luan_info.log",Level.INFO)
40 local appender = ListAppender.new(err,warn,info)
41 LuanLogger.configure(appender)
42 end
43
44
45 local Server = require "luan:http/Server.luan"
46 local FileHandler = require "java:goodjava.webserver.handlers.FileHandler"
47 local LuanHandler = require "java:luan.modules.http.LuanHandler"
48 local ListHandler = require "java:goodjava.webserver.handlers.ListHandler"
49 local ContentTypeHandler = require "java:goodjava.webserver.handlers.ContentTypeHandler"
50 local IndexHandler = require "java:goodjava.webserver.handlers.IndexHandler"
51 local DirHandler = require "java:goodjava.webserver.handlers.DirHandler"
52 local NotFound = require "java:luan.modules.http.NotFound"
53 local HeadersHandler = require "java:goodjava.webserver.handlers.HeadersHandler"
54 local SafeHandler = require "java:goodjava.webserver.handlers.SafeHandler"
55 local JavaServer = require "java:goodjava.webserver.Server"
56
57 Server.init_dir(dir)
58 local file_handler = FileHandler.new(dir)
59 local luan_handler = LuanHandler.new()
60 local handler = ListHandler.new( luan_handler, file_handler )
61 handler = ContentTypeHandler.new(handler)
62 handler = IndexHandler.new(handler)
63 local dir_handler = DirHandler.new(file_handler)
64 local not_found_hander = NotFound.new(luan_handler)
65 not_found_hander = ContentTypeHandler.new(not_found_hander)
66 handler = ListHandler.new( handler, dir_handler, not_found_hander )
67 handler = HeadersHandler.new(handler)
68 handler = SafeHandler.new(handler)
69 local server = JavaServer.new(port,handler)
70 Server.start(server)