Skip to content

Commit 580caef

Browse files
author
Olivier Bonnaure
committed
feat: aql pages
1 parent 3002ef8 commit 580caef

File tree

17 files changed

+766
-18
lines changed

17 files changed

+766
-18
lines changed

.init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ProgramMaxPayloadSize(10485760) -- 10 MB
1212

1313
-- ArangoDB connection
1414
local db_config = DecodeJson(LoadAsset("config/database.json"))
15+
print(EncodeJson(db_config[BeansEnv]))
1516
InitDB(db_config)
1617

1718
Views = {}
@@ -56,10 +57,9 @@ function OnHttpRequest()
5657
Params = GetParams()
5758
PrepareMultiPartParams() -- if you handle file uploads
5859

59-
-- Uncomment code if you use arangodb
60-
-- if (db_config ~= nil and db_config["engine"] == "arangodb") then
61-
-- Adb.RefreshToken(db_config[BeansEnv]) -- reconnect to arangoDB if needed
62-
-- end
60+
if Adb then
61+
Adb.RefreshToken(db_config[BeansEnv]) -- reconnect to arangoDB if needed
62+
end
6363

6464
-- Uncomment code if you use surrealdb
6565
-- if (db_config ~= nil and db_config["engine"] == "surrealdb") then

.lua/luaonbeans.lua

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function LoadViewsRecursively(path)
1010
end
1111
if kind == unix.DT_REG then
1212
if string.match(file, "%.etlua$") then
13-
Views[path .. "/" .. file] = LoadAsset(path .. "/" .. file)
13+
Views[path .. "/" .. file] = Etlua.compile(LoadAsset(path .. "/" .. file))
1414
end
1515
end
1616

@@ -26,16 +26,19 @@ end
2626

2727
function Page(view, layout, bindVarsView, bindVarsLayout)
2828
if (BeansEnv == "development") then
29-
Views["app/views/layouts/" .. layout .. "/index.html.etlua"] = LoadAsset("app/views/layouts/" ..
30-
layout .. "/index.html.etlua")
29+
Views["app/views/layouts/" .. layout .. "/index.html.etlua"] = Etlua.compile(LoadAsset("app/views/layouts/" ..
30+
layout .. "/index.html.etlua"))
3131
end
3232

3333
if (BeansEnv == "development") then
34-
Views["app/views/" .. view .. ".etlua"] = LoadAsset("app/views/" .. view .. ".etlua")
34+
Views["app/views/" .. view .. ".etlua"] = Etlua.compile(LoadAsset("app/views/" .. view .. ".etlua"))
3535
end
3636

37-
layout = Etlua.compile(Views["app/views/layouts/" .. layout .. "/index.html.etlua"])(bindVarsLayout or {})
38-
view = Etlua.compile(Views["app/views/" .. view .. ".etlua"])(bindVarsView or {})
37+
layout = Views["app/views/layouts/" .. layout .. "/index.html.etlua"](bindVarsLayout or {})
38+
39+
if Views["app/views/" .. view .. ".etlua"] then
40+
view = Views["app/views/" .. view .. ".etlua"](bindVarsView or {})
41+
end
3942

4043
local content
4144
if view:find("%%") then
@@ -62,11 +65,11 @@ function Partial(partial, bindVars)
6265
end
6366

6467
if (BeansEnv == "development") then
65-
Views["app/views/partials/" .. partial .. ".html.etlua"] = LoadAsset("app/views/partials/" ..
66-
partial .. ".html.etlua")
68+
Views["app/views/partials/" .. partial .. ".html.etlua"] = Etlua.compile(LoadAsset("app/views/partials/" ..
69+
partial .. ".html.etlua"))
6770
end
6871

69-
return Etlua.compile(Views["app/views/partials/" .. partial .. ".html.etlua"])(bindVars)
72+
return Views["app/views/partials/" .. partial .. ".html.etlua"](bindVars)
7073
end
7174

7275
local function assignRoute(method, name, options, value)
@@ -250,8 +253,30 @@ end
250253

251254
function HandleRequest()
252255
if Params.action == nil then
253-
if RoutePath("/public" .. GetPath()) == false then
254-
handle_404_error()
256+
local path = GetPath()
257+
258+
259+
if GetPath() == "/" then
260+
path = {
261+
["GET"] = "/index",
262+
["POST"] = "/create",
263+
["PUT"] = "/update",
264+
["PATCH"] = "/update",
265+
["DELETE"] = "/delete"
266+
}
267+
path = path[GetMethod()]
268+
end
269+
270+
local aql_page = false
271+
if not string.match(path, "%.") then -- check only if there is no extension
272+
aql_page = LoadAsset("aqlpages/" .. path .. ".aql")
273+
end
274+
if aql_page then
275+
HandleAqlPage(aql_page)
276+
else
277+
if RoutePath("/public" .. path) == false then
278+
handle_404_error()
279+
end
255280
end
256281
else
257282
if BeansEnv == "production" then

.lua/utilities.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require "utilities.table"
1111
require "utilities.string"
1212
require "utilities.multipart"
1313
require "utilities.csrf"
14+
require "utilities.aqlpages"
1415

1516
HandleCronJob = require("utilities.cronjobs").HandleCronJob
1617

.lua/utilities/aqlpages.lua

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
function PageAQL(view, layout, bindVarsView, bindVarsLayout)
2+
if (BeansEnv == "development") then
3+
Views["app/views/layouts/" .. layout .. "/index.html.etlua"] = Etlua.compile(LoadAsset("app/views/layouts/" ..
4+
layout .. "/index.html.etlua"))
5+
end
6+
7+
layout = Views["app/views/layouts/" .. layout .. "/index.html.etlua"](bindVarsLayout or {})
8+
9+
local content
10+
if view:find("%%") then
11+
content = layout:gsub("@yield", view:gsub("%%", "%%%%"))
12+
else
13+
content = layout:gsub("@yield", view)
14+
end
15+
local etag = EncodeBase64(Md5(content))
16+
17+
if etag == GetHeader("If-None-Match") then
18+
SetStatus(304)
19+
else
20+
SetHeader("Etag", etag)
21+
Write(content)
22+
end
23+
end
24+
25+
26+
function CreateComponent(component)
27+
local page_content = ""
28+
29+
if component.component ~= "page" and LoadAsset("app/views/partials/components/" .. component.component .. ".html.etlua") then
30+
local widget = Partial("components/" .. component.component, { component = component })
31+
page_content = page_content .. widget
32+
end
33+
return page_content
34+
end
35+
36+
function HandleAqlPage(aql, use_layout, write)
37+
if write == nil then write = true end
38+
if use_layout == nil then use_layout = true end
39+
40+
local data = Adb.Aql(aql)
41+
42+
local layout = ""
43+
local page_title = ""
44+
local page_description = ""
45+
local page_keywords = ""
46+
local page_author = ""
47+
local page_canonical = ""
48+
local page_image = ""
49+
local page_url = ""
50+
local page_content = ""
51+
52+
if data.result == nil then
53+
local error = "<pre class=\"col-span-12\"><code>Error on AQL request : \n" .. EncodeJson(data, { pretty = true }) .."</code></pre>"
54+
if write then Write(error) end
55+
return error
56+
end
57+
58+
for key, item in pairs(data.result[1]) do
59+
if #item == 0 then
60+
if item.component == "page" then
61+
page_title = item.title
62+
page_description = item.description
63+
page_keywords = item.keywords
64+
page_author = item.author
65+
page_canonical = item.canonical
66+
page_image = item.image
67+
page_url = item.url
68+
else
69+
page_content = page_content .. CreateComponent(item)
70+
end
71+
else
72+
for _, component in pairs(item) do
73+
page_content = page_content .. CreateComponent(component)
74+
end
75+
end
76+
end
77+
78+
if use_layout then
79+
PageAQL(page_content, "app", {}, { title = page_title, page_content = page_content })
80+
else
81+
if write then
82+
Write(page_content)
83+
end
84+
end
85+
86+
return page_content
87+
end
88+
89+
function DisplayAqlPage(filename)
90+
local aql = LoadAsset("aqlpages/" .. filename .. ".aql")
91+
local page_content = HandleAqlPage(aql, false, false)
92+
93+
return page_content
94+
end

app/views/layouts/app/css_tw_src.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,27 @@ code {
3636
a {
3737
@apply text-purple-400 hover:text-blue-900;
3838
}
39+
40+
.material-icons-outlined.md-18 { font-size: 18px; }
41+
.material-icons-outlined.md-24 { font-size: 24px; }
42+
.material-icons-outlined.md-36 { font-size: 36px; }
43+
.material-icons-outlined.md-48 { font-size: 48px; }
44+
45+
46+
table {
47+
@apply w-full border-collapse border;
48+
}
49+
50+
table th,
51+
table td {
52+
@apply border p-2;
53+
}
54+
55+
table th, table tfoot td {
56+
@apply bg-gray-800 text-white;
57+
}
58+
59+
table tr:nth-child(odd) {
60+
@apply bg-gray-50;
61+
}
62+

app/views/layouts/app/index.html.etlua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
<link rel="stylesheet" href="<%= PublicPath("/app.css") %>" />
99
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/atom-one-dark.min.css">
1010
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
11+
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Outlined" rel="stylesheet">
1112
<script src="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script>
12-
<script>hljs.initHighlightingOnLoad();</script>
13+
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
14+
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin=""/>
15+
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
1316
</head>
1417

1518
<style>
@@ -72,7 +75,9 @@
7275
<body class="min-h-screen flex flex-col">
7376
<div class="container mx-auto">
7477
<%- Partial("header") %>
75-
<div class="grow mb-10 bg-white shadow rounded p-5 text-black">@yield</div>
78+
<div class="grow mb-10 bg-white shadow rounded p-5 text-black">
79+
<div class="grid grid-cols-12 gap-4">@yield</div>
80+
</div>
7681
<%- Partial("footer") %>
7782
</div>
7883
</body>

0 commit comments

Comments
 (0)