diff options
Diffstat (limited to 'server.lua')
| -rw-r--r-- | server.lua | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/server.lua b/server.lua new file mode 100644 index 0000000..3625423 --- /dev/null +++ b/server.lua @@ -0,0 +1,48 @@ +local root_path = "/home/gemini" + +local function handle_gemini() + local sock = ngx.req.socket(true) + if not sock then return end + + sock:settimeout(5000) + local line, err = sock:receive("*l") + if not line then return end + + -- Декодируем %20 (пробелы) из URL, прилетевшего от Amfora + local url = line:match("gemini://[^/]+(.*)") + if not url or url == "" or url == "/" then + url = "/index.gmi" + end + local path = url:gsub("%%20", " ") + + local local_path = root_path .. path + local file = io.open(local_path, "rb") -- Бинарное чтение rb + + if not file then + sock:send("51 Resource not found\r\n") + return + end + + local content = file:read("*all") + file:close() + + -- Выставляем правильный MIME-тип по расширению + local mime = "text/plain; charset=utf-8" -- Дефолт для неизвестных файлов/логов + + if path:match("%.gmi$") or path:match("%.gemini$") then + mime = "text/gemini; charset=utf-8" + elseif path:match("%.md$") or path:match("%.markdown$") then + mime = "text/markdown; charset=utf-8" + elseif path:match("%.jpg$") or path:match("%.jpeg$") then + mime = "image/jpeg" + elseif path:match("%.png$") then + mime = "image/png" + elseif path:match("%.pdf$") then + mime = "application/pdf" + end + + sock:send("20 " .. mime .. "\r\n") + sock:send(content) +end + +handle_gemini() |
