view src/lib/Shared.luan @ 3:2c63b10781e1

add login
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 24 Oct 2024 21:43:44 -0600
parents ee1f91e67509
children f9e6a4cc4f7d
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local parse = Luan.parse or error()
local Io = require "luan:Io.luan"
local uri = Io.uri or error()
local Time = require "luan:Time.luan"
local Thread = require "luan:Thread.luan"
local thread_run = Thread.run or error()
local Mail = require "luan:mail/Mail.luan"
local User = require "site:/lib/User.luan"
local current_user = User.current or error()


local Shared = {}

local started = Time.now()

function Shared.head()
%>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Web Chat</title>
		<style>
			@import "/site.css?s=<%=started%>";
		</style>
		<script src="/site.js?s=<%=started%>"></script>
<%
end

local function header(crumbs)
	local user = current_user()
%>
		<div header>
			<span>
				<a href="/">Web Chat</a>
<%	for _, crumb in ipairs(crumbs or {}) do %>
				/ <%=crumb%>
<%	end %>
			</span>
			<span>
<%	if user == nil then %>
				<a href="/login.html">Login / Register</a>
<%	else %>
				<a href="/account.html"><%= user.email %></a>
<%	end %>
			</span>
		</div>
<%
end
Shared.header = header

function Shared.private_header()
	header{
		[[<a href="/private/">private</a>]]
		[[<a href="/private/tools/">tools</a>]]
	}
end

local config_file = uri("site:/private/local/config.luano")
Shared.config_file = config_file

do
	if config_file.exists() then
		Shared.config = parse( config_file.read_text() )
	else
		Shared.config = {
			mail_server = {
				host = "smtpcorp.com"
				port = 465
				username = "xxx"
				password = "xxx"
			}
		}
	end
end

local send_mail = Mail.sender(Shared.config.mail_server).send

function Shared.send_mail_async(mail)
	thread_run( function()
		send_mail(mail)
	end )
end

return Shared