Compare commits
14 Commits
lsp
...
de69397aa5
| Author | SHA1 | Date | |
|---|---|---|---|
| de69397aa5 | |||
| 2c2dc57453 | |||
| 85bd564164 | |||
| 0a0b1fa8c3 | |||
| 608a083861 | |||
| 50a45b6a82 | |||
| 19752a0c89 | |||
| 5192919645 | |||
| f763f57001 | |||
| 629fd06be0 | |||
| bedcfe781d | |||
| 9364df2645 | |||
| 4eaead6abc | |||
| 4abf2969ca |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
fes
|
fes
|
||||||
|
*.tar.gz
|
||||||
|
/stuff/
|
||||||
|
|||||||
4
COPYING
4
COPYING
@@ -1,6 +1,6 @@
|
|||||||
ISC License
|
ISC License
|
||||||
|
|
||||||
Copyright (c) 2025 fSD
|
Copyright (c) 2025-2026 fSD
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
@@ -12,4 +12,4 @@ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|||||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
PERFORMANCE OF THIS SOFTWARE.
|
PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|||||||
17
Dockerfile
17
Dockerfile
@@ -4,24 +4,17 @@ WORKDIR /src
|
|||||||
|
|
||||||
RUN apk add --no-cache git build-base
|
RUN apk add --no-cache git build-base
|
||||||
|
|
||||||
COPY go.mod go.sum ./
|
|
||||||
RUN go mod download
|
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
ENV CGO_ENABLED=0
|
RUN make
|
||||||
ENV GOOS=linux
|
|
||||||
ENV GOARCH=amd64
|
|
||||||
|
|
||||||
RUN go build -ldflags="-X fes/modules/version.gitCommit=$(git rev-parse --short HEAD) -s -w" -o fes
|
FROM alpine:3.19
|
||||||
|
|
||||||
FROM scratch
|
COPY --from=builder /src/fes /usr/local/bin/fes
|
||||||
|
|
||||||
COPY --from=builder /src/fes /fes
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 3000
|
||||||
|
|
||||||
ENTRYPOINT ["/fes"]
|
ENTRYPOINT ["/usr/local/bin/fes"]
|
||||||
CMD ["run", "/app"]
|
CMD ["run", "/app"]
|
||||||
|
|||||||
@@ -40,6 +40,6 @@ Run `fes doc` for the documentation website or goto [docs.vxserver.dev](https://
|
|||||||
|
|
||||||
ISC License
|
ISC License
|
||||||
|
|
||||||
Copyright (C) 2025 fSD
|
Copyright (C) 2025-2026 fSD
|
||||||
|
|
||||||
See `COPYING`
|
See `COPYING`
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
22
examples/06_archive/archive/facts/seals/seals.txt
Normal file
22
examples/06_archive/archive/facts/seals/seals.txt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
Pinnipeds [2] are the seals and their relatives, a group of semi-aquatic marine
|
||||||
|
mammals. The Pinnipedia is in the Order Carnivora. There are three seal
|
||||||
|
families: Odobenidae (walruses), Otariidae (eared seals, including sea lions
|
||||||
|
and fur seals), and Phocidae (true seals).[3]
|
||||||
|
|
||||||
|
Seals are sleek-bodied and barrel-shaped. Their bodies are well adapted to the
|
||||||
|
aquatic habitat where they spend most of their lives. Pinnipeds have flippers
|
||||||
|
for hands, big bulky bodies, doggish faces, and big eyes. Unlike cetaceans,
|
||||||
|
pinnipeds have their noses on their faces, and each nostril of the nose closes
|
||||||
|
when the pinniped goes underwater. Like cetaceans, pinnipeds have a thick layer
|
||||||
|
of blubber (fat) just under their skin: this blubber keeps them warm in cold
|
||||||
|
waters and keeps them fed during times when food is not easily found. When they
|
||||||
|
cannot find food, they live off the fat in the blubber.
|
||||||
|
|
||||||
|
Pinnipeds are carnivorous. This means they eat only meat (such as fish or
|
||||||
|
squid) and not plants. However, almost all pinnipeds can be eaten by polar
|
||||||
|
bears, sharks and killer whales.
|
||||||
|
|
||||||
|
Seals are often trained in zoos or aquariums to put on shows. However, in
|
||||||
|
Sweden, it is illegal to train a seal to balance a ball on its nose.[4]
|
||||||
|
|
||||||
|
From [Pinniped Wikipedia](https://simple.wikipedia.org/wiki/Pinniped)
|
||||||
|
Before Width: | Height: | Size: 1.8 MiB After Width: | Height: | Size: 1.8 MiB |
5
examples/07_extentions/Fes.toml
Normal file
5
examples/07_extentions/Fes.toml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
[app]
|
||||||
|
|
||||||
|
name = "extentions"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["vx-clutch"]
|
||||||
33
examples/07_extentions/README.md
Normal file
33
examples/07_extentions/README.md
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# extentions
|
||||||
|
|
||||||
|
```
|
||||||
|
fes new extentions
|
||||||
|
```
|
||||||
|
|
||||||
|
> **Know what you are doing?** Delete this file. Have fun!
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
Inside your Fes project, you'll see the following directories and files:
|
||||||
|
|
||||||
|
```
|
||||||
|
.
|
||||||
|
├── Fes.toml
|
||||||
|
├── README.md
|
||||||
|
└── www
|
||||||
|
└── index.lua
|
||||||
|
```
|
||||||
|
|
||||||
|
Fes looks for `.lua` files in the `www/` directory. Each file is exposed as a route based on its file name.
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
All commands are run from the root of the project, from a terminal:
|
||||||
|
|
||||||
|
| Command | Action |
|
||||||
|
| :------------------------ | :----------------------------------------------- |
|
||||||
|
| `fes run .` | Runs the project at `.` |
|
||||||
|
|
||||||
|
## What to learn more?
|
||||||
|
|
||||||
|
Check out [Fes's docs](https://docs.vxserver.dev/static/fes.html).
|
||||||
12
examples/07_extentions/www/index.lua
Normal file
12
examples/07_extentions/www/index.lua
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
local fes = require("fes")
|
||||||
|
local site = fes.fes()
|
||||||
|
|
||||||
|
site.copyright = fes.util.copyright("https://fsd.vxserver.dev/", "fSD")
|
||||||
|
|
||||||
|
site:extend("myext", {
|
||||||
|
shout = function(self, str) return self:g(str:upper()) end
|
||||||
|
})
|
||||||
|
|
||||||
|
site.myext:shout("hello world")
|
||||||
|
|
||||||
|
return site
|
||||||
28
lib/fes.lua
28
lib/fes.lua
@@ -1,4 +1,5 @@
|
|||||||
local std = require("lib.std")
|
local std = require("lib.std")
|
||||||
|
local symbol = require("lib.symbol")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
M.__index = M
|
M.__index = M
|
||||||
@@ -318,11 +319,30 @@ em, i { font-style: italic; }
|
|||||||
return setmetatable(self, M)
|
return setmetatable(self, M)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:custom(str)
|
function M:g(str)
|
||||||
table.insert(self.parts, str)
|
table.insert(self.parts, str)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M:extend(name, tbl)
|
||||||
|
if type(name) ~= "string" then
|
||||||
|
error("First argument to extend must be a string (namespace name)")
|
||||||
|
end
|
||||||
|
if type(tbl) ~= "table" then
|
||||||
|
error("Second argument to extend must be a table of functions")
|
||||||
|
end
|
||||||
|
self[name] = {}
|
||||||
|
for k, v in pairs(tbl) do
|
||||||
|
if type(v) ~= "function" then
|
||||||
|
error("Extension values must be functions, got " .. type(v) .. " for key " .. k)
|
||||||
|
end
|
||||||
|
self[name][k] = function(...)
|
||||||
|
return v(self, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
for name, func in pairs(std) do
|
for name, func in pairs(std) do
|
||||||
if type(func) == "function" then
|
if type(func) == "function" then
|
||||||
M[name] = function(self, ...)
|
M[name] = function(self, ...)
|
||||||
@@ -340,9 +360,11 @@ function M:build()
|
|||||||
header = header:gsub(
|
header = header:gsub(
|
||||||
"{{FAVICON}}",
|
"{{FAVICON}}",
|
||||||
favicon_html
|
favicon_html
|
||||||
or [[<link rel="icon" href="data:image/svg+xml,<svg xmlns=%%22http://www.w3.org/2000/svg%%22 viewBox=%%220 0 100 100%%22><text y=%%22.9em%%22 font-size=%%2290%%22>🔥</text></svg>">]]
|
or
|
||||||
|
[[<link rel="icon" href="data:image/svg+xml,<svg xmlns=%%22http://www.w3.org/2000/svg%%22 viewBox=%%220 0 100 100%%22><text y=%%22.9em%%22 font-size=%%2290%%22>🔥</text></svg>">]]
|
||||||
)
|
)
|
||||||
local footer = self.footer:gsub("{{COPYRIGHT}}", self.copyright or "© The Copyright Holder")
|
local footer = self.footer:gsub("{{COPYRIGHT}}",
|
||||||
|
self.copyright or symbol.legal.copyright .. "The Copyright Holder")
|
||||||
return header .. table.concat(self.parts, "\n") .. footer
|
return header .. table.concat(self.parts, "\n") .. footer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
27
lib/site.lua
Normal file
27
lib/site.lua
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.name()
|
||||||
|
local fes_mod = package.loaded.fes
|
||||||
|
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.name then
|
||||||
|
return fes_mod.config.site.name
|
||||||
|
end
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.version()
|
||||||
|
local fes_mod = package.loaded.fes
|
||||||
|
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.version then
|
||||||
|
return fes_mod.config.site.version
|
||||||
|
end
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.authors()
|
||||||
|
local fes_mod = package.loaded.fes
|
||||||
|
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.authors then
|
||||||
|
return fes_mod.config.site.authors
|
||||||
|
end
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
161
lib/std.lua
161
lib/std.lua
@@ -1,155 +1,163 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.fes_version()
|
function M.element(tag, attrs, content)
|
||||||
local fes_mod = package.loaded.fes
|
local out = { "<", tag }
|
||||||
if fes_mod and fes_mod.config and fes_mod.config.fes and fes_mod.config.fes.version then
|
|
||||||
return fes_mod.config.fes.version
|
|
||||||
end
|
|
||||||
return ""
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.site_version()
|
if attrs then
|
||||||
local fes_mod = package.loaded.fes
|
for k, v in pairs(attrs) do
|
||||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.version then
|
if v ~= false and v ~= nil then
|
||||||
return fes_mod.config.site.version
|
if v == true then
|
||||||
|
out[#out + 1] = " " .. k
|
||||||
|
else
|
||||||
|
out[#out + 1] = " " .. k .. "=\"" .. tostring(v) .. "\""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return ""
|
|
||||||
|
if content == nil then
|
||||||
|
out[#out + 1] = " />"
|
||||||
|
return table.concat(out)
|
||||||
|
end
|
||||||
|
|
||||||
|
out[#out + 1] = ">"
|
||||||
|
out[#out + 1] = tostring(content)
|
||||||
|
out[#out + 1] = "</"
|
||||||
|
out[#out + 1] = tag
|
||||||
|
out[#out + 1] = ">"
|
||||||
|
|
||||||
|
return table.concat(out)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.a(link, str)
|
function M.a(link, str)
|
||||||
link = link or "https://example.com"
|
link = link or "https://example.com"
|
||||||
str = str or link
|
str = str or link
|
||||||
return '<a href="' .. link .. '">' .. str .. "</a>"
|
return M.element("a", { href = link }, str)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.download(link, str, downloadName)
|
||||||
|
link = link or "."
|
||||||
|
str = str or link
|
||||||
|
return M.element("a", { href = link, download = downloadName }, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.ha(link, str)
|
function M.ha(link, str)
|
||||||
link = link or "https://example.com"
|
link = link or "https://example.com"
|
||||||
str = str or link
|
str = str or link
|
||||||
return '<a class="hidden" href="' .. link .. '">' .. str .. "</a>"
|
return M.element("a", { href = link, class = "hidden" }, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.external(link, str)
|
function M.external(link, str)
|
||||||
return '<a target="_blank" href="' .. link .. '">' .. str .. "</a>"
|
return M.element("a", { href = link, target = "_blank" }, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.note(str)
|
function M.note(str)
|
||||||
return '<div class="note">' .. str .. "</div>"
|
return M.element("div", { class = "note" }, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.muted(str)
|
function M.muted(str)
|
||||||
return '<div class="muted">' .. str .. "</div>"
|
return M.element("div", { class = "muted" }, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.callout(str)
|
function M.callout(str)
|
||||||
return '<div class="callout">' .. str .. "</div>"
|
return M.element("div", { class = "callout" }, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.h1(str)
|
function M.h1(str)
|
||||||
return "<h1>" .. (str or "") .. "</h1>"
|
return M.element("h1", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.h2(str)
|
function M.h2(str)
|
||||||
return "<h2>" .. (str or "") .. "</h2>"
|
return M.element("h2", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.h3(str)
|
function M.h3(str)
|
||||||
return "<h3>" .. (str or "") .. "</h3>"
|
return M.element("h3", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.h4(str)
|
function M.h4(str)
|
||||||
return "<h4>" .. (str or "") .. "</h4>"
|
return M.element("h4", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.h5(str)
|
function M.h5(str)
|
||||||
return "<h5>" .. (str or "") .. "</h5>"
|
return M.element("h5", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.h6(str)
|
function M.h6(str)
|
||||||
return "<h6>" .. (str or "") .. "</h6>"
|
return M.element("h6", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.p(str)
|
function M.p(str)
|
||||||
return "<p>" .. (str or "") .. "</p>"
|
return M.element("p", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.pre(str)
|
function M.pre(str)
|
||||||
return "<pre>" .. (str or "") .. "</pre>"
|
return M.element("pre", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.code(str)
|
function M.code(str)
|
||||||
return "<pre><code>" .. (str or "") .. "</code></pre>"
|
return M.element("pre", nil, M.element("code", nil, str or ""))
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.ul(items)
|
function M.ul(items)
|
||||||
items = items or {}
|
items = items or {}
|
||||||
local html = "<ul>"
|
local out = {}
|
||||||
for _, item in ipairs(items) do
|
for _, item in ipairs(items) do
|
||||||
html = html .. "<li>" .. tostring(item) .. "</li>"
|
out[#out + 1] = M.element("li", nil, item)
|
||||||
end
|
end
|
||||||
html = html .. "</ul>"
|
return M.element("ul", nil, table.concat(out))
|
||||||
return html
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.ol(items)
|
function M.ol(items)
|
||||||
items = items or {}
|
items = items or {}
|
||||||
local html = "<ol>"
|
local out = {}
|
||||||
for _, item in ipairs(items) do
|
for _, item in ipairs(items) do
|
||||||
html = html .. "<li>" .. tostring(item) .. "</li>"
|
out[#out + 1] = M.element("li", nil, item)
|
||||||
end
|
end
|
||||||
html = html .. "</ol>"
|
return M.element("ol", nil, table.concat(out))
|
||||||
return html
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.tl(items)
|
function M.tl(items)
|
||||||
items = items or {}
|
items = items or {}
|
||||||
local html = '<ul class="tl">'
|
local out = {}
|
||||||
for _, item in ipairs(items) do
|
for _, item in ipairs(items) do
|
||||||
html = html .. "<li>" .. tostring(item) .. "</li>"
|
out[#out + 1] = M.element("li", nil, item)
|
||||||
end
|
end
|
||||||
html = html .. "</ul>"
|
return M.element("ul", { class = "tl" }, table.concat(out))
|
||||||
return html
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.blockquote(str)
|
function M.blockquote(str)
|
||||||
return "<blockquote>" .. (str or "") .. "</blockquote>"
|
return M.element("blockquote", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.hr()
|
function M.hr()
|
||||||
return "<hr>"
|
return M.element("hr")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.img(src, alt)
|
function M.img(src, alt)
|
||||||
src = src or ""
|
return M.element("img", { src = src or "", alt = alt or "" })
|
||||||
alt = alt or ""
|
|
||||||
return '<img src="' .. src .. '" alt="' .. alt .. '">'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.strong(str)
|
function M.strong(str)
|
||||||
return "<strong>" .. (str or "") .. "</strong>"
|
return M.element("strong", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.em(str)
|
function M.em(str)
|
||||||
return "<em>" .. (str or "") .. "</em>"
|
return M.element("em", nil, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.br()
|
function M.br()
|
||||||
return "<br>"
|
return M.element("br")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.div(content, class)
|
function M.div(content, class)
|
||||||
content = content or ""
|
return M.element("div", class and { class = class } or nil, content or "")
|
||||||
class = class or ""
|
|
||||||
local class_attr = class ~= "" and (' class="' .. class .. '"') or ""
|
|
||||||
return "<div" .. class_attr .. ">" .. content .. "</div>"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.span(content, class)
|
function M.span(content, class)
|
||||||
content = content or ""
|
return M.element("span", class and { class = class } or nil, content or "")
|
||||||
class = class or ""
|
|
||||||
local class_attr = class ~= "" and (' class="' .. class .. '"') or ""
|
|
||||||
return "<span" .. class_attr .. ">" .. content .. "</span>"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- HTML escaping utility
|
|
||||||
function M.escape(str)
|
function M.escape(str)
|
||||||
str = tostring(str or "")
|
str = tostring(str or "")
|
||||||
str = str:gsub("&", "&")
|
str = str:gsub("&", "&")
|
||||||
@@ -160,55 +168,28 @@ function M.escape(str)
|
|||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get site name from config
|
|
||||||
function M.site_name()
|
|
||||||
local fes_mod = package.loaded.fes
|
|
||||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.name then
|
|
||||||
return fes_mod.config.site.name
|
|
||||||
end
|
|
||||||
return ""
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Get site title from config
|
|
||||||
function M.site_title()
|
|
||||||
local fes_mod = package.loaded.fes
|
|
||||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.title then
|
|
||||||
return fes_mod.config.site.title
|
|
||||||
end
|
|
||||||
return ""
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Get site authors from config
|
|
||||||
function M.site_authors()
|
|
||||||
local fes_mod = package.loaded.fes
|
|
||||||
if fes_mod and fes_mod.config and fes_mod.config.site and fes_mod.config.site.authors then
|
|
||||||
return fes_mod.config.site.authors
|
|
||||||
end
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.highlight(str)
|
function M.highlight(str)
|
||||||
return '<span class="highlight">' .. (str or "") .. "</span>"
|
return M.element("span", { class = "highlight" }, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.banner(str)
|
function M.banner(str)
|
||||||
return '<div class="banner">' .. (str or "") .. "</div>"
|
return M.element("div", { class = "banner" }, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.center(str)
|
function M.center(str)
|
||||||
return '<div class="center">' .. (str or "") .. "</div>"
|
return M.element("div", { class = "center" }, str or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.nav(link, str)
|
function M.nav(link, str)
|
||||||
link = link or "example.com"
|
link = link or "example.com"
|
||||||
str = str or link
|
str = str or link
|
||||||
return '<a class="nav" href="' .. link .. '">' .. str .. "</a>"
|
return M.element("a", { href = link, class = "nav" }, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.rl(r, l)
|
function M.rl(r, l)
|
||||||
r = r or ""
|
return
|
||||||
l = l or ""
|
M.element("span", { class = "left" }, r or "") ..
|
||||||
return string.format('<span class="left">%s</span><span class="right">%s</span>', r, l)
|
M.element("span", { class = "right" }, l or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -1,7 +1,75 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.copyright = "©"
|
local function get(s)
|
||||||
M.registered_trademark = "®"
|
return "&" .. (s or "") .. ";"
|
||||||
M.trademark = "™"
|
end
|
||||||
|
|
||||||
|
M.legal = {
|
||||||
|
copyright = get("copy"),
|
||||||
|
registered_trademark = get("reg"),
|
||||||
|
trademark = get("trade"),
|
||||||
|
}
|
||||||
|
|
||||||
|
M.currency = {
|
||||||
|
euro = get("euro"),
|
||||||
|
pound = get("pound"),
|
||||||
|
yen = get("yen"),
|
||||||
|
cent = get("cent"),
|
||||||
|
dollar = "$",
|
||||||
|
}
|
||||||
|
|
||||||
|
M.math = {
|
||||||
|
plus_minus = get("plusmn"),
|
||||||
|
multiply = get("times"),
|
||||||
|
divide = get("divide"),
|
||||||
|
not_equal = get("ne"),
|
||||||
|
less_equal = get("le"),
|
||||||
|
greater_equal = get("ge"),
|
||||||
|
infinity = get("infin"),
|
||||||
|
approx = get("asymp"),
|
||||||
|
}
|
||||||
|
|
||||||
|
M.arrows = {
|
||||||
|
left = get("larr"),
|
||||||
|
right = get("rarr"),
|
||||||
|
up = get("uarr"),
|
||||||
|
down = get("darr"),
|
||||||
|
left_right = get("harr"),
|
||||||
|
}
|
||||||
|
|
||||||
|
M.punctuation = {
|
||||||
|
left_double_quote = get("ldquo"),
|
||||||
|
right_double_quote = get("rdquo"),
|
||||||
|
left_single_quote = get("lsquo"),
|
||||||
|
right_single_quote = get("rsquo"),
|
||||||
|
ellipsis = get("hellip"),
|
||||||
|
em_dash = get("mdash"),
|
||||||
|
en_dash = get("ndash"),
|
||||||
|
}
|
||||||
|
|
||||||
|
M.whitespace = {
|
||||||
|
non_breaking = get("nbsp"),
|
||||||
|
thin = get("thinsp"),
|
||||||
|
}
|
||||||
|
|
||||||
|
M.symbols = {
|
||||||
|
degree = get("deg"),
|
||||||
|
micro = get("micro"),
|
||||||
|
section = get("sect"),
|
||||||
|
paragraph = get("para"),
|
||||||
|
check = get("check"),
|
||||||
|
cross = get("cross"),
|
||||||
|
bullet = get("bull"),
|
||||||
|
middle_dot = get("middot"),
|
||||||
|
broken_bar = get("brvbar"),
|
||||||
|
}
|
||||||
|
|
||||||
|
M.html = {
|
||||||
|
less_than = get("lt"),
|
||||||
|
greater_than = get("gt"),
|
||||||
|
ampersand = get("amp"),
|
||||||
|
double_quote = get("quot"),
|
||||||
|
single_quote = get("apos"),
|
||||||
|
}
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
27
lib/util.lua
27
lib/util.lua
@@ -3,12 +3,33 @@ local symbol = require("lib.symbol")
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.cc(tbl)
|
function M.cc(tbl, sep)
|
||||||
return table.concat(tbl)
|
return table.concat(tbl, sep or "")
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.year(y)
|
||||||
|
return y or os.date("%Y")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.copyright(link, holder)
|
function M.copyright(link, holder)
|
||||||
return symbol.copyright .. " " .. std.external(link, holder)
|
return symbol.legal.copyright .. " " .. std.external(link, holder)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.license(name)
|
||||||
|
return symbol.legal.registered .. " " .. name
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.ls(dir)
|
||||||
|
local p = io.popen('ls -A -1 -- ' .. string.format('%q', dir))
|
||||||
|
if not p then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
local t = {}
|
||||||
|
for line in p:lines() do
|
||||||
|
t[#t + 1] = line
|
||||||
|
end
|
||||||
|
p:close()
|
||||||
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
36
main.go
36
main.go
@@ -2,10 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
|
||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"fes/modules/doc"
|
"fes/modules/doc"
|
||||||
"fes/modules/new"
|
"fes/modules/new"
|
||||||
"fes/modules/server"
|
"fes/modules/server"
|
||||||
|
"fes/modules/ui"
|
||||||
"fes/modules/version"
|
"fes/modules/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,18 +30,21 @@ func init() {
|
|||||||
config.Docker = flag.Bool("docker", false, "Create a docker project")
|
config.Docker = flag.Bool("docker", false, "Create a docker project")
|
||||||
config.Lib = lib
|
config.Lib = lib
|
||||||
config.Doc = documentation
|
config.Doc = documentation
|
||||||
|
config.Verbose = flag.Bool("verbose", false, "Enable verbose logging")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var m runtime.MemStats
|
||||||
|
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] <command> <project_dir>\n", os.Args[0])
|
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] <command> <project_dir>\n", os.Args[0])
|
||||||
fmt.Fprintf(os.Stderr, "Commands:")
|
fmt.Fprintln(flag.CommandLine.Output(), "Commands:")
|
||||||
fmt.Fprintf(os.Stderr, " new <project_dir> Create a new project")
|
fmt.Fprintln(flag.CommandLine.Output(), " new <project_dir> Create a new project")
|
||||||
fmt.Fprintf(os.Stderr, " doc Open documentation")
|
fmt.Fprintln(flag.CommandLine.Output(), " doc Open documentation")
|
||||||
fmt.Fprintf(os.Stderr, " run <project_dir> Start the server")
|
fmt.Fprintln(flag.CommandLine.Output(), " run <project_dir> Start the server")
|
||||||
fmt.Fprintf(os.Stderr, "Options:")
|
fmt.Fprintln(flag.CommandLine.Output(), "Options:")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
fmt.Fprintf(os.Stderr, "For bug reports, contact a developer and describe the issue. Provide the output of the `-V1` flag.")
|
fmt.Fprintln(flag.CommandLine.Output(), "For bug reports, contact a developer and describe the issue. Provide the output of the `-V1` flag.")
|
||||||
}
|
}
|
||||||
|
|
||||||
showVersion := flag.Bool("version", false, "Show version and exit")
|
showVersion := flag.Bool("version", false, "Show version and exit")
|
||||||
@@ -89,16 +93,16 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
case "run":
|
case "run":
|
||||||
if err := server.Start(dir); err != nil {
|
if *config.Port == 3000 {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
ui.WARNING("Using default port, this may lead to conflicts with other services")
|
||||||
fmt.Fprintf(os.Stderr, "%s does not exist\n", dir)
|
|
||||||
fmt.Fprintf(os.Stderr, "Try: fes new %s\n", dir)
|
|
||||||
os.Exit(1)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintln(os.Stderr, "Error:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
ui.Log("Fes is starting")
|
||||||
|
ui.Log("Fes version=%s, commit=%s, just started", version.VERSION, version.GetCommit())
|
||||||
|
|
||||||
|
runtime.ReadMemStats(&m)
|
||||||
|
ui.Log("FRE memory usage when created %v Mb", m.TotalAlloc/1024/1024)
|
||||||
|
|
||||||
|
server.Start(dir)
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", cmd)
|
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", cmd)
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ var Port *int
|
|||||||
var Color *bool
|
var Color *bool
|
||||||
var Static *bool
|
var Static *bool
|
||||||
var Docker *bool
|
var Docker *bool
|
||||||
|
var Verbose *bool
|
||||||
|
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
App struct {
|
App struct {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package new
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fes/modules/config"
|
"fes/modules/config"
|
||||||
|
"fes/modules/ui"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -114,5 +115,16 @@ All commands are run from the root of the project, from a terminal:
|
|||||||
## What to learn more?
|
## What to learn more?
|
||||||
|
|
||||||
Check out [Fes's docs](https://docs.vxserver.dev/static/fes.html).`, "$$", "`"), dir, dir)
|
Check out [Fes's docs](https://docs.vxserver.dev/static/fes.html).`, "$$", "`"), dir, dir)
|
||||||
|
|
||||||
|
ui.Hint("you can run this with `fes run %s`", dir)
|
||||||
|
|
||||||
|
fmt.Println("Created new Fes project at", func () string {
|
||||||
|
if cwd, err := os.Getwd(); err != nil {
|
||||||
|
return dir
|
||||||
|
} else {
|
||||||
|
return cwd
|
||||||
|
}
|
||||||
|
}())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
6
modules/server/render.go
Normal file
6
modules/server/render.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
/* returns a string of rendered html */
|
||||||
|
func render(luapath string) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
@@ -2,470 +2,17 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fes/modules/config"
|
"fes/modules/config"
|
||||||
"fes/modules/ui"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pelletier/go-toml/v2"
|
"log"
|
||||||
lua "github.com/yuin/gopher-lua"
|
|
||||||
"html/template"
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/* this is the request data we pass over the bus to the application, via the fes.bus interface */
|
var routes map[string]string
|
||||||
type reqData struct {
|
|
||||||
path string
|
|
||||||
params map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
/* performs relavent handling based on the directory passaed
|
|
||||||
*
|
|
||||||
* Special directories
|
|
||||||
* - www/ <= contains lua routes.
|
|
||||||
* - static/ <= static content accessable at /static/path or /static/dir/path.
|
|
||||||
* - include/ <= globally accessable lua functions, cannot directly access "fes" right now.
|
|
||||||
* - archive/ <= contains user facing files such as archives or dists.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
func handleDir(entries []os.DirEntry, dir string, routes map[string]string, base string, isStatic bool) error {
|
|
||||||
for _, entry := range entries {
|
|
||||||
path := filepath.Join(dir, entry.Name())
|
|
||||||
if entry.IsDir() {
|
|
||||||
nextBase := joinBase(base, entry.Name())
|
|
||||||
subEntries, err := os.ReadDir(path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to read directory %s: %w", path, err)
|
|
||||||
}
|
|
||||||
if err := handleDir(subEntries, path, routes, nextBase, isStatic); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
route := joinBase(base, entry.Name())
|
|
||||||
if !isStatic && strings.HasSuffix(entry.Name(), ".lua") {
|
|
||||||
name := strings.TrimSuffix(entry.Name(), ".lua")
|
|
||||||
if name == "index" {
|
|
||||||
routes[basePath(base)] = path
|
|
||||||
routes[route] = path
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
route = joinBase(base, name)
|
|
||||||
} else if !isStatic && strings.HasSuffix(entry.Name(), ".md") {
|
|
||||||
name := strings.TrimSuffix(entry.Name(), ".md")
|
|
||||||
if name == "index" {
|
|
||||||
routes[basePath(base)] = path
|
|
||||||
routes[route] = path
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
route = joinBase(base, name)
|
|
||||||
}
|
|
||||||
routes[route] = path
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(vx-clutch): this should not be a function
|
|
||||||
func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
|
||||||
app := L.NewTable()
|
|
||||||
ents, err := os.ReadDir(includeDir)
|
|
||||||
if err != nil {
|
|
||||||
return app
|
|
||||||
}
|
|
||||||
for _, e := range ents {
|
|
||||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".lua") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
base := strings.TrimSuffix(e.Name(), ".lua")
|
|
||||||
path := filepath.Join(includeDir, e.Name())
|
|
||||||
if _, err := os.Stat(path); err != nil {
|
|
||||||
tbl := L.NewTable()
|
|
||||||
tbl.RawSetString("error", lua.LString(fmt.Sprintf("file not found: %s", path)))
|
|
||||||
app.RawSetString(base, tbl)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := L.DoFile(path); err != nil {
|
|
||||||
tbl := L.NewTable()
|
|
||||||
tbl.RawSetString("error", lua.LString(err.Error()))
|
|
||||||
app.RawSetString(base, tbl)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val := L.Get(-1)
|
|
||||||
L.Pop(1)
|
|
||||||
tbl, ok := val.(*lua.LTable)
|
|
||||||
if !ok || tbl == nil {
|
|
||||||
tbl = L.NewTable()
|
|
||||||
}
|
|
||||||
app.RawSetString(base, tbl)
|
|
||||||
}
|
|
||||||
return app
|
|
||||||
}
|
|
||||||
|
|
||||||
/* renders the given lua route */
|
|
||||||
func renderRoute(entry string, cfg *config.AppConfig, requestData reqData) ([]byte, error) {
|
|
||||||
L := lua.NewState()
|
|
||||||
defer L.Close()
|
|
||||||
|
|
||||||
libFiles, err := fs.ReadDir(config.Lib, "lib")
|
|
||||||
if err == nil {
|
|
||||||
for _, de := range libFiles {
|
|
||||||
if de.IsDir() || !strings.HasSuffix(de.Name(), ".lua") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
path := filepath.Join("lib", de.Name())
|
|
||||||
fileData, err := config.Lib.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
L.DoString(string(fileData))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
preloadLuaModule := func(name, path string) {
|
|
||||||
L.PreloadModule(name, func(L *lua.LState) int {
|
|
||||||
fileData, err := config.Lib.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if err := L.DoString(string(fileData)); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
L.Push(L.Get(-1))
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
preloadLuaModule("lib.std", "lib/std.lua")
|
|
||||||
preloadLuaModule("lib.symbol", "lib/symbol.lua")
|
|
||||||
preloadLuaModule("lib.util", "lib/util.lua")
|
|
||||||
|
|
||||||
L.PreloadModule("fes", func(L *lua.LState) int {
|
|
||||||
mod := L.NewTable()
|
|
||||||
libModules := []string{}
|
|
||||||
if ents, err := fs.ReadDir(config.Lib, "lib"); err == nil {
|
|
||||||
for _, e := range ents {
|
|
||||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".lua") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
libModules = append(libModules, strings.TrimSuffix(e.Name(), ".lua"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, modName := range libModules {
|
|
||||||
path := filepath.Join("lib", modName+".lua")
|
|
||||||
fileData, err := config.Lib.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := L.DoString(string(fileData)); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val := L.Get(-1)
|
|
||||||
L.Pop(1)
|
|
||||||
tbl, ok := val.(*lua.LTable)
|
|
||||||
if !ok || tbl == nil {
|
|
||||||
tbl = L.NewTable()
|
|
||||||
}
|
|
||||||
if modName == "fes" {
|
|
||||||
tbl.ForEach(func(k, v lua.LValue) { mod.RawSet(k, v) })
|
|
||||||
} else {
|
|
||||||
mod.RawSetString(modName, tbl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod.RawSetString("app", loadIncludeModules(L, filepath.Join(".", "include")))
|
|
||||||
|
|
||||||
if cfg != nil {
|
|
||||||
site := L.NewTable()
|
|
||||||
site.RawSetString("version", lua.LString(cfg.App.Version))
|
|
||||||
site.RawSetString("name", lua.LString(cfg.App.Name))
|
|
||||||
authors := L.NewTable()
|
|
||||||
for i, a := range cfg.App.Authors {
|
|
||||||
authors.RawSetInt(i+1, lua.LString(a))
|
|
||||||
}
|
|
||||||
site.RawSetString("authors", authors)
|
|
||||||
mod.RawSetString("site", site)
|
|
||||||
}
|
|
||||||
|
|
||||||
bus := L.NewTable()
|
|
||||||
bus.RawSetString("url", lua.LString(requestData.path))
|
|
||||||
params := L.NewTable()
|
|
||||||
for k, v := range requestData.params {
|
|
||||||
params.RawSetString(k, lua.LString(v))
|
|
||||||
}
|
|
||||||
bus.RawSetString("params", params)
|
|
||||||
mod.RawSetString("bus", bus)
|
|
||||||
|
|
||||||
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
|
|
||||||
L.Push(lua.LString(markdownToHTML(L.ToString(1))))
|
|
||||||
return 1
|
|
||||||
}))
|
|
||||||
|
|
||||||
L.Push(mod)
|
|
||||||
return 1
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := L.DoFile(entry); err != nil {
|
|
||||||
return []byte(""), err
|
|
||||||
}
|
|
||||||
|
|
||||||
if L.GetTop() == 0 {
|
|
||||||
return []byte(""), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
L.SetGlobal("__fes_result", L.Get(-1))
|
|
||||||
if err := L.DoString("return tostring(__fes_result)"); err != nil {
|
|
||||||
L.GetGlobal("__fes_result")
|
|
||||||
if s := L.ToString(-1); s != "" {
|
|
||||||
return []byte(s), nil
|
|
||||||
}
|
|
||||||
return []byte(""), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if s := L.ToString(-1); s != "" {
|
|
||||||
return []byte(s), nil
|
|
||||||
}
|
|
||||||
return []byte(""), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/* this indexes and generate the page for viewing the archive directory */
|
|
||||||
func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
|
||||||
info, err := os.Stat(fsPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if !info.IsDir() {
|
|
||||||
return "", fmt.Errorf("not a directory")
|
|
||||||
}
|
|
||||||
ents, err := os.ReadDir(fsPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
type entryInfo struct {
|
|
||||||
name string
|
|
||||||
isDir bool
|
|
||||||
href string
|
|
||||||
size int64
|
|
||||||
mod time.Time
|
|
||||||
}
|
|
||||||
var list []entryInfo
|
|
||||||
for _, e := range ents {
|
|
||||||
n := e.Name()
|
|
||||||
full := filepath.Join(fsPath, n)
|
|
||||||
st, err := os.Stat(full)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
isd := st.IsDir()
|
|
||||||
displayName := n
|
|
||||||
if isd {
|
|
||||||
displayName = n + "/"
|
|
||||||
}
|
|
||||||
href := path.Join(urlPath, n)
|
|
||||||
if isd && !strings.HasSuffix(href, "/") {
|
|
||||||
href = href + "/"
|
|
||||||
}
|
|
||||||
size := int64(-1)
|
|
||||||
if !isd {
|
|
||||||
size = st.Size()
|
|
||||||
}
|
|
||||||
list = append(list, entryInfo{name: displayName, isDir: isd, href: href, size: size, mod: st.ModTime()})
|
|
||||||
}
|
|
||||||
sort.Slice(list, func(i, j int) bool {
|
|
||||||
if list[i].isDir != list[j].isDir {
|
|
||||||
return list[i].isDir
|
|
||||||
}
|
|
||||||
return strings.ToLower(list[i].name) < strings.ToLower(list[j].name)
|
|
||||||
})
|
|
||||||
|
|
||||||
urlPath = basePath(strings.TrimPrefix(urlPath, "/archive"))
|
|
||||||
|
|
||||||
var b strings.Builder
|
|
||||||
b.WriteString("<html>\n<head><title>Index of ")
|
|
||||||
b.WriteString(template.HTMLEscapeString(urlPath))
|
|
||||||
b.WriteString("</title></head>\n<body>\n<h1>Index of ")
|
|
||||||
b.WriteString(template.HTMLEscapeString(urlPath))
|
|
||||||
b.WriteString("</h1><hr><pre>")
|
|
||||||
if urlPath != "/archive" && urlPath != "/archive/" {
|
|
||||||
up := path.Dir(urlPath)
|
|
||||||
if up == "." {
|
|
||||||
up = "/archive"
|
|
||||||
}
|
|
||||||
if !strings.HasSuffix(up, "/") {
|
|
||||||
up = "/archive" + filepath.Dir(up) + "/"
|
|
||||||
}
|
|
||||||
b.WriteString(`<a href="` + template.HTMLEscapeString(up) + `">../</a>` + "\n")
|
|
||||||
} else {
|
|
||||||
b.WriteString(`<a href="../">../</a>` + "\n")
|
|
||||||
}
|
|
||||||
nameCol := 50
|
|
||||||
for _, ei := range list {
|
|
||||||
escapedName := template.HTMLEscapeString(ei.name)
|
|
||||||
dateStr := ei.mod.Local().Format("02-Jan-2006 15:04")
|
|
||||||
var sizeStr string
|
|
||||||
if ei.isDir {
|
|
||||||
sizeStr = "-"
|
|
||||||
} else {
|
|
||||||
sizeStr = fmt.Sprintf("%d", ei.size)
|
|
||||||
}
|
|
||||||
spaces := 1
|
|
||||||
if len(escapedName) < nameCol {
|
|
||||||
spaces = nameCol - len(escapedName)
|
|
||||||
}
|
|
||||||
line := `<a href="` + template.HTMLEscapeString(ei.href) + `">` + escapedName + `</a>` + strings.Repeat(" ", spaces) + dateStr + strings.Repeat(" ", 19-len(sizeStr)) + sizeStr + "\n"
|
|
||||||
b.WriteString(line)
|
|
||||||
}
|
|
||||||
b.WriteString("</pre><hr></body>\n</html>")
|
|
||||||
return b.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/* generates the data for the not found page. Checks for user-defined source in this order
|
|
||||||
* 404.lua => 404.md => 404.html => default.
|
|
||||||
*/
|
|
||||||
func generateNotFoundData(cfg *config.AppConfig) []byte {
|
|
||||||
notFoundData := []byte(`
|
|
||||||
<html>
|
|
||||||
<head><title>404 Not Found</title></head>
|
|
||||||
<body>
|
|
||||||
<center><h1>404 Not Found</h1></center>
|
|
||||||
<hr><center>fes</center>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`)
|
|
||||||
if _, err := os.Stat(filepath.Join("www", "404.lua")); err == nil {
|
|
||||||
if nf, err := renderRoute("www/404.lua", cfg, reqData{}); err == nil {
|
|
||||||
notFoundData = nf
|
|
||||||
}
|
|
||||||
} else if _, err := os.Stat("www/404.md"); err == nil {
|
|
||||||
if buf, err := os.ReadFile("www/404.html"); err == nil {
|
|
||||||
notFoundData = []byte(markdownToHTML(string(buf)))
|
|
||||||
}
|
|
||||||
} else if _, err := os.Stat("www/404.html"); err == nil {
|
|
||||||
if buf, err := os.ReadFile("www/404.html"); err == nil {
|
|
||||||
notFoundData = buf
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return notFoundData
|
|
||||||
}
|
|
||||||
|
|
||||||
/* helper to load all special directories */
|
|
||||||
func loadDirs() map[string]string {
|
|
||||||
routes := make(map[string]string)
|
|
||||||
|
|
||||||
if entries, err := os.ReadDir("www"); err == nil {
|
|
||||||
if err := handleDir(entries, "www", routes, "", false); err != nil {
|
|
||||||
ui.Warning("failed to handle www directory", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if entries, err := os.ReadDir("static"); err == nil {
|
|
||||||
if err := handleDir(entries, "static", routes, "/static", true); err != nil {
|
|
||||||
ui.Warning("failed to handle static directory", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if entries, err := os.ReadDir("archive"); err == nil {
|
|
||||||
if err := handleDir(entries, "archive", routes, "/archive", true); err != nil {
|
|
||||||
ui.Warning("failed to handle archive directory", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return routes
|
|
||||||
}
|
|
||||||
|
|
||||||
/* helper to parse the Fes.toml and generate config */
|
|
||||||
func parseConfig() config.AppConfig {
|
|
||||||
tomlDocument, err := os.ReadFile("Fes.toml")
|
|
||||||
if err != nil {
|
|
||||||
ui.Error("failed to read Fes.toml", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
docStr := fixMalformedToml(string(tomlDocument))
|
|
||||||
var cfg config.AppConfig
|
|
||||||
if err := toml.Unmarshal([]byte(docStr), &cfg); err != nil {
|
|
||||||
ui.Warning("failed to parse Fes.toml", err)
|
|
||||||
cfg.App.Authors = []string{"unknown"}
|
|
||||||
cfg.App.Name = "unknown"
|
|
||||||
cfg.App.Version = "unknown"
|
|
||||||
}
|
|
||||||
return cfg
|
|
||||||
}
|
|
||||||
|
|
||||||
/* helper to read the archive files */
|
|
||||||
func readArchive(w http.ResponseWriter, route string) error {
|
|
||||||
fsPath := "." + route
|
|
||||||
if info, err := os.Stat(fsPath); err == nil && info.IsDir() {
|
|
||||||
if page, err := generateArchiveIndex(fsPath, route); err == nil {
|
|
||||||
w.Write([]byte(page))
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/* start the Fes server */
|
|
||||||
func Start(dir string) error {
|
|
||||||
if err := os.Chdir(dir); err != nil {
|
|
||||||
return ui.Error(fmt.Sprintf("failed to change directory to %s", dir), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg := parseConfig()
|
|
||||||
notFoundData := generateNotFoundData(&cfg)
|
|
||||||
routes := loadDirs()
|
|
||||||
|
|
||||||
|
func Start(dir string) {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
route, ok := routes[r.URL.Path]
|
w.Write([]byte("<h1>Sup bitch</h1>"))
|
||||||
|
|
||||||
var err error = nil
|
|
||||||
|
|
||||||
/* defer won't update paramaters unless we do this. */
|
|
||||||
defer func() {
|
|
||||||
ui.Path(route, err)
|
|
||||||
}()
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
err = config.ErrRouteMiss
|
|
||||||
route = r.URL.Path
|
|
||||||
|
|
||||||
if strings.HasPrefix(route, "/archive") {
|
|
||||||
err = readArchive(w, route)
|
|
||||||
} else {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
w.Write([]byte(notFoundData))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
params := make(map[string]string)
|
|
||||||
for k, v := range r.URL.Query() {
|
|
||||||
if len(v) > 0 {
|
|
||||||
params[k] = v[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var data []byte
|
|
||||||
if strings.HasSuffix(route, ".lua") {
|
|
||||||
data, err = renderRoute(route, &cfg, reqData{path: r.URL.Path, params: params})
|
|
||||||
} else if strings.HasSuffix(route, ".md") {
|
|
||||||
data, err = os.ReadFile(route)
|
|
||||||
data = []byte(markdownToHTML(string(data)))
|
|
||||||
data = []byte("<style>body {max-width: 80ch;}</style>\n" + string(data))
|
|
||||||
} else {
|
|
||||||
data, err = os.ReadFile(route)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(data)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *config.Port), nil))
|
||||||
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *config.Port), nil)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,36 +4,8 @@ import (
|
|||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
"github.com/gomarkdown/markdown/html"
|
"github.com/gomarkdown/markdown/html"
|
||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/gomarkdown/markdown/parser"
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func joinBase(base, name string) string {
|
|
||||||
if base == "" {
|
|
||||||
return "/" + name
|
|
||||||
}
|
|
||||||
return base + "/" + name
|
|
||||||
}
|
|
||||||
|
|
||||||
func basePath(base string) string {
|
|
||||||
if base == "" || base == "." {
|
|
||||||
return "/"
|
|
||||||
}
|
|
||||||
return base
|
|
||||||
}
|
|
||||||
|
|
||||||
func fixMalformedToml(content string) string {
|
|
||||||
re := regexp.MustCompile(`(?m)^(\s*\w+\s*=\s*)$`)
|
|
||||||
return re.ReplaceAllStringFunc(content, func(match string) string {
|
|
||||||
parts := strings.Split(strings.TrimSpace(match), "=")
|
|
||||||
if len(parts) == 2 && strings.TrimSpace(parts[1]) == "" {
|
|
||||||
key := strings.TrimSpace(parts[0])
|
|
||||||
return key + " = \"\""
|
|
||||||
}
|
|
||||||
return match
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func markdownToHTML(mdText string) string {
|
func markdownToHTML(mdText string) string {
|
||||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
||||||
p := parser.NewWithExtensions(extensions)
|
p := parser.NewWithExtensions(extensions)
|
||||||
|
|||||||
109
modules/ui/ui.go
109
modules/ui/ui.go
@@ -4,61 +4,114 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"fes/modules/config"
|
"fes/modules/config"
|
||||||
"fes/modules/version"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* print out the current path (route) and relevant error */
|
const (
|
||||||
|
hintColor = 0xbda02a
|
||||||
|
)
|
||||||
|
|
||||||
|
func formatTimestamp() string {
|
||||||
|
return time.Now().Format("02 Jan 2006 15:04")
|
||||||
|
}
|
||||||
|
|
||||||
|
func logMessage(prefix string, msg string, args ...any) {
|
||||||
|
formatted := fmt.Sprintf(msg, args...)
|
||||||
|
if prefix == "" {
|
||||||
|
fmt.Printf("%s * %s\n", formatTimestamp(), formatted)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s * %s: %s\n", formatTimestamp(), prefix, formatted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic log
|
||||||
|
func Log(msg string, args ...any) {
|
||||||
|
logMessage("", msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OK message (green)
|
||||||
|
func OK(msg string, args ...any) {
|
||||||
|
formatted := fmt.Sprintf(msg, args...)
|
||||||
|
color.Green("%s * %s\n", formatTimestamp(), formatted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning message (magenta)
|
||||||
|
func WARN(msg string, args ...any) {
|
||||||
|
formatted := fmt.Sprintf(msg, args...)
|
||||||
|
color.Magenta("%s # %s\n", formatTimestamp(), formatted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning message (magenta)
|
||||||
|
func WARNING(msg string, args ...any) {
|
||||||
|
formatted := fmt.Sprintf(msg, args...)
|
||||||
|
color.Magenta("%s # WARNING %s\n", formatTimestamp(), formatted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error message (red)
|
||||||
|
func ERROR(msg string, args ...any) {
|
||||||
|
formatted := fmt.Sprintf(msg, args...)
|
||||||
|
color.Red("%s ! %s\n", formatTimestamp(), formatted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatal message and panic
|
||||||
|
func FATAL(msg string, args ...any) {
|
||||||
|
formatted := fmt.Sprintf(msg, args...)
|
||||||
|
color.Red("%s % %s\n", formatTimestamp(), formatted)
|
||||||
|
panic(formatted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hint message (custom color)
|
||||||
|
func Hint(msg string, args ...any) {
|
||||||
|
formatted := fmt.Sprintf(msg, args...)
|
||||||
|
color.RGB(func(hex int) (r, g, b int) {
|
||||||
|
r = (hex >> 16) & 0xFF
|
||||||
|
g = (hex >> 8) & 0xFF
|
||||||
|
b = hex & 0xFF
|
||||||
|
return
|
||||||
|
}(hintColor)).Printf("hint: %s\n", formatted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path logging: prints route and status
|
||||||
func Path(path string, err error) {
|
func Path(path string, err error) {
|
||||||
path = strings.TrimPrefix(path, "/")
|
path = strings.TrimPrefix(path, "/")
|
||||||
|
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = "(null)"
|
path = "(null)"
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" > %s ", path)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
OK("ok")
|
OK("Route: %s - ok", path)
|
||||||
return
|
|
||||||
} else if errors.Is(err, config.ErrRouteMiss) {
|
} else if errors.Is(err, config.ErrRouteMiss) {
|
||||||
WARN(config.ErrRouteMiss.Error())
|
WARN("Route: %s - %s", path, config.ErrRouteMiss.Error())
|
||||||
} else {
|
} else {
|
||||||
ERROR("bad")
|
ERROR("Route: %s - bad", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print general system warning */
|
// System warning with prefix
|
||||||
func Warning(msg string, err error) error {
|
func Warning(msg string, err error) error {
|
||||||
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.MagentaString("warning"), err)
|
WARN("%s: %v", msg, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print general system error */
|
// System error with prefix
|
||||||
func Error(msg string, err error) error {
|
func Error(msg string, err error) error {
|
||||||
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("error"), err)
|
ERROR("%s: %v", msg, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print fatality and panic */
|
// Fatal system error
|
||||||
func Fatal(msg string, err error) error {
|
func Fatal(msg string, err error) error {
|
||||||
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("fatal"), err)
|
FATAL("%s: %v", msg, err)
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print message using the ok status color */
|
// Log on Verbose
|
||||||
func OK(msg string) {
|
func LogVerbose(msg string, args ...any) {
|
||||||
color.Green(msg)
|
if *config.Verbose {
|
||||||
}
|
Log(msg, args...)
|
||||||
|
}
|
||||||
/* print message using the warning status color */
|
|
||||||
func WARN(msg string) {
|
|
||||||
color.Magenta(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
/* print message using the error status color */
|
|
||||||
func ERROR(msg string) {
|
|
||||||
color.Red(msg)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ var gitCommit string = "devel"
|
|||||||
|
|
||||||
const PROGRAM_NAME string = "fes"
|
const PROGRAM_NAME string = "fes"
|
||||||
const PROGRAM_NAME_LONG string = "fes/fSD"
|
const PROGRAM_NAME_LONG string = "fes/fSD"
|
||||||
const VERSION string = "beta"
|
const VERSION string = "0.3.1"
|
||||||
|
|
||||||
func Version() {
|
func Version() {
|
||||||
fmt.Printf("%s version %s\n", PROGRAM_NAME_LONG, VERSION)
|
fmt.Printf("%s version %s\n", PROGRAM_NAME_LONG, VERSION)
|
||||||
@@ -20,3 +20,7 @@ func FullVersion() {
|
|||||||
fmt.Printf("%s+%s\n", VERSION, gitCommit)
|
fmt.Printf("%s+%s\n", VERSION, gitCommit)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCommit() string {
|
||||||
|
return gitCommit
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user