view src/luan/host/init.luan @ 1332:11b7e11f9ed5

cleaner logging
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 21:50:26 -0700
parents f41919741100
children 21b153b4bcc4
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local do_file = Luan.do_file or error()
local String = require "luan:String.luan"
local gsub = String.gsub or error()


local dir, domain, logging = ...


-- logging

if logging then
	java()
	local Log4j = require "java:luan.modules.logging.Log4j"
	local Level = require "java:org.apache.log4j.Level"
	local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
	local RollingFileAppender = require "java:org.apache.log4j.RollingFileAppender"
	
	local logger = Log4j.getRootLogger()
	logger.removeAllAppenders()
	local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n")
	local log_dir = dir.."/site/private/local/logs/"

	local function add_appender(file,level)
		local appender = RollingFileAppender.new(layout, log_dir..file)
		appender.setMaxFileSize("1MB")
		logger.addAppender(appender)
		if level ~= logger.getEffectiveLevel() then
			appender.setThreshold(level)
		end
	end

	add_appender("error.log",Level.ERROR)
	add_appender("warn.log",Level.WARN)
	add_appender("info.log",Level.INFO)
end



-- set vars

local Io = require "luan:Io.luan"
local Http = require "luan:http/Http.luan"
local Hosting = require "luan:host/Hosting.luan"
local Mail = require "luan:mail/Mail.luan"

Io.password = do_file(dir.."/info.luan").password or error()

Http.dir = "file:"..dir.."/site"

function Io.schemes.site(path,loading)
	return Io.uri( Http.dir..path, loading )
end

Http.domain = domain


-- mail  - fix later

Hosting.send_mail = Mail.Sender{
	host = "smtpcorp.com"
	username = "smtp@luan.ws"  -- ?
	password = "luanhost"
	port = 2525
}.send


-- callback to luanhost code
do_file "file:init.luan"



java()
local WebHandler = require "java:luan.host.WebHandler"
local LuanJava = require "java:luan.Luan"

function Hosting.no_security(password)
	WebHandler.securityPassword == password or error "wrong password"
	LuanJava.setSecurity(nil)
end