feat(core): implement file-based API versioning system (DAW/furt#83)
- Add VERSION file in repository root - Add read_version() function with error handling - Update /health endpoint to show file-based version - Add version display during server startup - Fallback to ?.?.? when VERSION file unreadable Enables deployment tracking across dev/test/prod environments
This commit is contained in:
parent
5b851b8bfb
commit
7053af3c0d
2 changed files with 52 additions and 28 deletions
1
VERSION
Normal file
1
VERSION
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0.1.1
|
||||||
79
src/main.lua
79
src/main.lua
|
|
@ -13,6 +13,25 @@ local AuthRoute = require("src.routes.auth")
|
||||||
-- Load configuration
|
-- Load configuration
|
||||||
local config = require("config.server")
|
local config = require("config.server")
|
||||||
|
|
||||||
|
-- Read version from VERSION file
|
||||||
|
local function read_version()
|
||||||
|
local file, err = io.open("VERSION", "r")
|
||||||
|
if not file then
|
||||||
|
print("WARNING: Could not read VERSION file: " .. (err or "unknown error"))
|
||||||
|
return "?.?.?"
|
||||||
|
end
|
||||||
|
|
||||||
|
local version = file:read("*line")
|
||||||
|
file:close()
|
||||||
|
|
||||||
|
if not version or version:match("^%s*$") then
|
||||||
|
print("WARNING: VERSION file is empty or contains only whitespace")
|
||||||
|
return "?.?.?"
|
||||||
|
end
|
||||||
|
|
||||||
|
return version:match("^%s*(.-)%s*$") -- trim whitespace
|
||||||
|
end
|
||||||
|
|
||||||
-- HTTP-Server Module
|
-- HTTP-Server Module
|
||||||
local FurtServer = {}
|
local FurtServer = {}
|
||||||
|
|
||||||
|
|
@ -47,23 +66,23 @@ function FurtServer:parse_request(client)
|
||||||
if not request_line then
|
if not request_line then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Parse request line: "POST /v1/mail/send HTTP/1.1"
|
-- Parse request line: "POST /v1/mail/send HTTP/1.1"
|
||||||
local method, path, protocol = request_line:match("(%w+) (%S+) (%S+)")
|
local method, path, protocol = request_line:match("(%w+) (%S+) (%S+)")
|
||||||
if not method then
|
if not method then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Parse headers
|
-- Parse headers
|
||||||
local headers = {}
|
local headers = {}
|
||||||
local content_length = 0
|
local content_length = 0
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local line = client:receive()
|
local line = client:receive()
|
||||||
if not line or line == "" then
|
if not line or line == "" then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
local key, value = line:match("([^:]+): (.+)")
|
local key, value = line:match("([^:]+): (.+)")
|
||||||
if key and value then
|
if key and value then
|
||||||
headers[key:lower()] = value
|
headers[key:lower()] = value
|
||||||
|
|
@ -72,13 +91,13 @@ function FurtServer:parse_request(client)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Parse body
|
-- Parse body
|
||||||
local body = ""
|
local body = ""
|
||||||
if content_length > 0 then
|
if content_length > 0 then
|
||||||
body = client:receive(content_length)
|
body = client:receive(content_length)
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
method = method,
|
method = method,
|
||||||
path = path,
|
path = path,
|
||||||
|
|
@ -97,11 +116,11 @@ function FurtServer:add_cors_headers(request)
|
||||||
"https://dragons-at-work.de",
|
"https://dragons-at-work.de",
|
||||||
"https://www.dragons-at-work.de"
|
"https://www.dragons-at-work.de"
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Check if request has Origin header
|
-- Check if request has Origin header
|
||||||
local origin = request and request.headers and request.headers.origin
|
local origin = request and request.headers and request.headers.origin
|
||||||
local cors_origin = "*" -- Default fallback
|
local cors_origin = "*" -- Default fallback
|
||||||
|
|
||||||
-- If origin is provided and in allowed list, use it
|
-- If origin is provided and in allowed list, use it
|
||||||
if origin then
|
if origin then
|
||||||
for _, allowed in ipairs(allowed_origins) do
|
for _, allowed in ipairs(allowed_origins) do
|
||||||
|
|
@ -111,7 +130,7 @@ function FurtServer:add_cors_headers(request)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
["Access-Control-Allow-Origin"] = cors_origin,
|
["Access-Control-Allow-Origin"] = cors_origin,
|
||||||
["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS",
|
["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS",
|
||||||
|
|
@ -125,38 +144,38 @@ end
|
||||||
function FurtServer:create_response(status, data, content_type, additional_headers, request)
|
function FurtServer:create_response(status, data, content_type, additional_headers, request)
|
||||||
content_type = content_type or "application/json"
|
content_type = content_type or "application/json"
|
||||||
local body = ""
|
local body = ""
|
||||||
|
|
||||||
if type(data) == "table" then
|
if type(data) == "table" then
|
||||||
body = cjson.encode(data)
|
body = cjson.encode(data)
|
||||||
else
|
else
|
||||||
body = tostring(data or "")
|
body = tostring(data or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Start with CORS headers
|
-- Start with CORS headers
|
||||||
local headers = self:add_cors_headers(request)
|
local headers = self:add_cors_headers(request)
|
||||||
|
|
||||||
-- Add standard headers
|
-- Add standard headers
|
||||||
headers["Content-Type"] = content_type
|
headers["Content-Type"] = content_type
|
||||||
headers["Content-Length"] = tostring(#body)
|
headers["Content-Length"] = tostring(#body)
|
||||||
headers["Connection"] = "close"
|
headers["Connection"] = "close"
|
||||||
headers["Server"] = "Furt-Lua/1.1"
|
headers["Server"] = "Furt-Lua/1.1"
|
||||||
|
|
||||||
-- Add additional headers if provided
|
-- Add additional headers if provided
|
||||||
if additional_headers then
|
if additional_headers then
|
||||||
for key, value in pairs(additional_headers) do
|
for key, value in pairs(additional_headers) do
|
||||||
headers[key] = value
|
headers[key] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Build response
|
-- Build response
|
||||||
local response = string.format("HTTP/1.1 %d %s\r\n", status, self:get_status_text(status))
|
local response = string.format("HTTP/1.1 %d %s\r\n", status, self:get_status_text(status))
|
||||||
|
|
||||||
for key, value in pairs(headers) do
|
for key, value in pairs(headers) do
|
||||||
response = response .. key .. ": " .. value .. "\r\n"
|
response = response .. key .. ": " .. value .. "\r\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
response = response .. "\r\n" .. body
|
response = response .. "\r\n" .. body
|
||||||
|
|
||||||
return response
|
return response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -184,23 +203,23 @@ function FurtServer:handle_client(client)
|
||||||
client:send(response)
|
client:send(response)
|
||||||
return
|
return
|
||||||
end
|
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))
|
request.method, request.path))
|
||||||
|
|
||||||
-- Handle OPTIONS preflight requests (CORS)
|
-- Handle OPTIONS preflight requests (CORS)
|
||||||
if request.method == "OPTIONS" then
|
if request.method == "OPTIONS" then
|
||||||
local response = self:create_response(204, "", "text/plain", nil, request)
|
local response = self:create_response(204, "", "text/plain", nil, request)
|
||||||
client:send(response)
|
client:send(response)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Route handling
|
-- Route handling
|
||||||
local handler = nil
|
local handler = nil
|
||||||
if self.routes[request.method] and self.routes[request.method][request.path] then
|
if self.routes[request.method] and self.routes[request.method][request.path] then
|
||||||
handler = self.routes[request.method][request.path]
|
handler = self.routes[request.method][request.path]
|
||||||
end
|
end
|
||||||
|
|
||||||
if handler then
|
if handler then
|
||||||
local success, result = pcall(handler, request, self)
|
local success, result = pcall(handler, request, self)
|
||||||
if success then
|
if success then
|
||||||
|
|
@ -223,13 +242,16 @@ function FurtServer:start()
|
||||||
if not self.server then
|
if not self.server then
|
||||||
error("Failed to bind to " .. self.host .. ":" .. self.port)
|
error("Failed to bind to " .. self.host .. ":" .. self.port)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local version = read_version()
|
||||||
|
|
||||||
print(string.format("Furt HTTP-Server started on %s:%d", self.host, self.port))
|
print(string.format("Furt HTTP-Server started on %s:%d", self.host, self.port))
|
||||||
|
print("Version: " .. version)
|
||||||
print("API-Key authentication: ENABLED")
|
print("API-Key authentication: ENABLED")
|
||||||
print("Rate limiting: ENABLED (60 req/hour per API key, 100 req/hour per IP)")
|
print("Rate limiting: ENABLED (60 req/hour per API key, 100 req/hour per IP)")
|
||||||
print("CORS enabled for configured origins")
|
print("CORS enabled for configured origins")
|
||||||
print("Press Ctrl+C to stop")
|
print("Press Ctrl+C to stop")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local client = self.server:accept()
|
local client = self.server:accept()
|
||||||
if client then
|
if client then
|
||||||
|
|
@ -245,10 +267,11 @@ local server = FurtServer:new()
|
||||||
|
|
||||||
-- Public routes (no authentication required)
|
-- Public routes (no authentication required)
|
||||||
server:add_route("GET", "/health", function(request, server)
|
server:add_route("GET", "/health", function(request, server)
|
||||||
|
local version = read_version()
|
||||||
local response_data = {
|
local response_data = {
|
||||||
status = "healthy",
|
status = "healthy",
|
||||||
service = "furt-lua",
|
service = "furt-lua",
|
||||||
version = "1.1.0",
|
version = version,
|
||||||
timestamp = os.time(),
|
timestamp = os.time(),
|
||||||
features = {
|
features = {
|
||||||
smtp_configured = config.mail and config.mail.username ~= nil,
|
smtp_configured = config.mail and config.mail.username ~= nil,
|
||||||
|
|
@ -268,15 +291,15 @@ if os.getenv("ENABLE_TEST_ENDPOINT") == "true" then
|
||||||
headers_count = 0,
|
headers_count = 0,
|
||||||
warning = "This is a development endpoint"
|
warning = "This is a development endpoint"
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Count headers
|
-- Count headers
|
||||||
for _ in pairs(request.headers) do
|
for _ in pairs(request.headers) do
|
||||||
response_data.headers_count = response_data.headers_count + 1
|
response_data.headers_count = response_data.headers_count + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
return server:create_response(200, response_data, nil, nil, request)
|
return server:create_response(200, response_data, nil, nil, request)
|
||||||
end)
|
end)
|
||||||
print("⚠️ Test endpoint enabled (development mode)")
|
print("[WARN] Test endpoint enabled (development mode)")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Protected routes (require authentication)
|
-- Protected routes (require authentication)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue