annotate serve.luan @ 19:a90e510ccc4f

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