view serve.luan @ 21:b14073ab9d07

update luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 14 Sep 2024 22:17:26 -0600
parents dfc36e7ed22c
children
line wrap: on
line source

local dir = "src"
local port = 8080


require "java"
local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()


local log_to_console = false
for _, arg in ipairs{...} do
	if arg == "console" then
		log_to_console = true
	end
end
if not log_to_console then
	local FileWriter = require "java:java.io.FileWriter"
	local LuanLogger = require "java:luan.modules.logging.LuanLogger"
	local Layouts = require "java:goodjava.logger.Layouts"
	local DateLayout = require "java:goodjava.logger.DateLayout"
	local ListLayout = require "java:goodjava.logger.ListLayout"
	local Level = require "java:goodjava.logger.Level"
	local WriterAppender = require "java:goodjava.logger.WriterAppender"
	local LevelAppender = require "java:goodjava.logger.LevelAppender"
	local ListAppender = require "java:goodjava.logger.ListAppender"

	local layout = ListLayout.new(DateLayout.new("yyyy-MM-dd HH:mm:ss,SSS")," ",Layouts.LEVEL_PADDED," ",Layouts.LOGGER," - ",Layouts.MESSAGE,"\n",Layouts.THROWABLE)

	local function new_appender(file,level)
		local writer = FileWriter.new(file,true)
		local appender = WriterAppender.new(layout,writer)
		appender = LevelAppender.new(appender,level)
		return appender
	end

	local err = new_appender("logs/luan_error.log",Level.ERROR)
	local warn = new_appender("logs/luan_warn.log",Level.WARN)
	local info = new_appender("logs/luan_info.log",Level.INFO)
	local appender = ListAppender.new(err,warn,info)
	LuanLogger.configure(appender)
end


local Server = require "luan:http/Server.luan"
local FileHandler = require "java:goodjava.webserver.handlers.FileHandler"
local LuanHandler = require "java:luan.modules.http.LuanHandler"
local ListHandler = require "java:goodjava.webserver.handlers.ListHandler"
local ContentTypeHandler = require "java:goodjava.webserver.handlers.ContentTypeHandler"
local IndexHandler = require "java:goodjava.webserver.handlers.IndexHandler"
local DirHandler = require "java:goodjava.webserver.handlers.DirHandler"
local NotFound = require "java:luan.modules.http.NotFound"
local HeadersHandler = require "java:goodjava.webserver.handlers.HeadersHandler"
local SafeHandler = require "java:goodjava.webserver.handlers.SafeHandler"
local JavaServer = require "java:goodjava.webserver.Server"

Server.init_dir(dir)
local file_handler = FileHandler.new(dir)
local luan_handler = LuanHandler.new()
local handler = ListHandler.new( luan_handler, file_handler )
handler = ContentTypeHandler.new(handler)
handler = IndexHandler.new(handler)
local dir_handler = DirHandler.new(file_handler)
local not_found_hander = NotFound.new(luan_handler)
not_found_hander = ContentTypeHandler.new(not_found_hander)
handler = ListHandler.new( handler, dir_handler, not_found_hander )
handler = HeadersHandler.new(handler)
handler = SafeHandler.new(handler)
local server = JavaServer.new(port,handler)
Server.start(server)