merge: integrate merkwerk binary detection into multi-tenant

- Add universal merkwerk binary detection from main
- Required for furt installations (no install without merkwerk)
- Maintains compatibility with multi-tenant architecture

Merges main branch changes for issue #94 into feature/issue-89-multi-tenant
This commit is contained in:
michael 2025-08-28 18:20:54 +02:00
commit a5db9a633f
5 changed files with 424 additions and 68 deletions

View file

@ -13,6 +13,31 @@ local AuthRoute = require("src.routes.auth")
-- Load configuration
local config = require("config.server")
local function get_version_info()
-- Load merkwerk integration
local success, merkwerk = pcall(require, "integrations.lua-api")
if not success then
print("WARNING: merkwerk integration not available, using fallback")
return {
service = "furt-lua",
version = "?.?.?",
content_hash = "unknown",
vcs_info = { type = "none", hash = "", branch = "" },
source = "fallback-no-merkwerk"
}
end
-- Get merkwerk health info
local health_info = merkwerk.get_health_info()
-- Ensure compatibility with old VERSION-only expectations
if not health_info.version then
health_info.version = "?.?.?"
end
return health_info
end
-- HTTP-Server Module
local FurtServer = {}
@ -47,23 +72,23 @@ function FurtServer:parse_request(client)
if not request_line then
return nil
end
-- Parse request line: "POST /v1/mail/send HTTP/1.1"
local method, path, protocol = request_line:match("(%w+) (%S+) (%S+)")
if not method then
return nil
end
-- Parse headers
local headers = {}
local content_length = 0
while true do
local line = client:receive()
if not line or line == "" then
break
end
local key, value = line:match("([^:]+): (.+)")
if key and value then
headers[key:lower()] = value
@ -72,13 +97,13 @@ function FurtServer:parse_request(client)
end
end
end
-- Parse body
local body = ""
if content_length > 0 then
body = client:receive(content_length)
end
return {
method = method,
path = path,
@ -97,11 +122,11 @@ function FurtServer:add_cors_headers(request)
"https://dragons-at-work.de",
"https://www.dragons-at-work.de"
}
-- Check if request has Origin header
local origin = request and request.headers and request.headers.origin
local cors_origin = "*" -- Default fallback
-- If origin is provided and in allowed list, use it
if origin then
for _, allowed in ipairs(allowed_origins) do
@ -111,7 +136,7 @@ function FurtServer:add_cors_headers(request)
end
end
end
return {
["Access-Control-Allow-Origin"] = cors_origin,
["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS",
@ -125,38 +150,38 @@ end
function FurtServer:create_response(status, data, content_type, additional_headers, request)
content_type = content_type or "application/json"
local body = ""
if type(data) == "table" then
body = cjson.encode(data)
else
body = tostring(data or "")
end
-- Start with CORS headers
local headers = self:add_cors_headers(request)
-- Add standard headers
headers["Content-Type"] = content_type
headers["Content-Length"] = tostring(#body)
headers["Connection"] = "close"
headers["Server"] = "Furt-Lua/1.1"
-- Add additional headers if provided
if additional_headers then
for key, value in pairs(additional_headers) do
headers[key] = value
end
end
-- Build response
local response = string.format("HTTP/1.1 %d %s\r\n", status, self:get_status_text(status))
for key, value in pairs(headers) do
response = response .. key .. ": " .. value .. "\r\n"
end
response = response .. "\r\n" .. body
return response
end
@ -184,23 +209,23 @@ function FurtServer:handle_client(client)
client:send(response)
return
end
print(string.format("[%s] %s %s", os.date("%Y-%m-%d %H:%M:%S"),
print(string.format("[%s] %s %s", os.date("%Y-%m-%d %H:%M:%S"),
request.method, request.path))
-- Handle OPTIONS preflight requests (CORS)
if request.method == "OPTIONS" then
local response = self:create_response(204, "", "text/plain", nil, request)
client:send(response)
return
end
-- Route handling
local handler = nil
if self.routes[request.method] and self.routes[request.method][request.path] then
handler = self.routes[request.method][request.path]
end
if handler then
local success, result = pcall(handler, request, self)
if success then
@ -223,13 +248,18 @@ function FurtServer:start()
if not self.server then
error("Failed to bind to " .. self.host .. ":" .. self.port)
end
local version_info = get_version_info()
print(string.format("Furt HTTP-Server started on %s:%d", self.host, self.port))
print("Version: " .. version_info.version .. " (merkwerk)")
print("Content-Hash: " .. (version_info.content_hash or "unknown"))
print("VCS: " .. (version_info.vcs_info and version_info.vcs_info.hash or "none"))
print("API-Key authentication: ENABLED")
print("Rate limiting: ENABLED (60 req/hour per API key, 100 req/hour per IP)")
print("CORS enabled for configured origins")
print("Press Ctrl+C to stop")
while true do
local client = self.server:accept()
if client then
@ -245,15 +275,20 @@ local server = FurtServer:new()
-- Public routes (no authentication required)
server:add_route("GET", "/health", function(request, server)
local version_info = get_version_info()
local response_data = {
status = "healthy",
service = "furt-lua",
version = "1.1.0",
service = version_info.service or "furt-lua",
version = version_info.version,
content_hash = version_info.content_hash,
vcs_info = version_info.vcs_info,
timestamp = os.time(),
source = version_info.source,
features = {
smtp_configured = config.mail and config.mail.username ~= nil,
auth_enabled = true,
rate_limiting = true
rate_limiting = true,
merkwerk_integrated = version_info.source == "merkwerk"
}
}
return server:create_response(200, response_data, nil, nil, request)
@ -268,15 +303,15 @@ if os.getenv("ENABLE_TEST_ENDPOINT") == "true" then
headers_count = 0,
warning = "This is a development endpoint"
}
-- Count headers
for _ in pairs(request.headers) do
response_data.headers_count = response_data.headers_count + 1
end
return server:create_response(200, response_data, nil, nil, request)
end)
print("⚠️ Test endpoint enabled (development mode)")
print("[WARN] Test endpoint enabled (development mode)")
end
-- Protected routes (require authentication)