furt/config/server.lua

134 lines
4.2 KiB
Lua
Raw Normal View History

-- config/server.lua
-- Multi-Tenant server configuration using nginx-style config parser
-- Dragons@Work Digital Sovereignty Project
feat(furt): implement complete Lua HTTP-Server for digital sovereignty (#63) - Add furt-lua/ directory with pure Lua implementation - Replace planned Go implementation with Corporate-free technology - Complete Week 1 Challenge: HTTP-Server to production-ready in 48min - HTTP-Server in pure Lua (185 lines, lua-socket based) - JSON API endpoints with request/response parsing - Modular architecture: each file < 200 lines - Error handling for 404, 400, validation scenarios - GET /health - Service health check with timestamp - POST /test - Development testing with request echo - POST /v1/mail/send - Mail service foundation with validation - Comprehensive error responses with structured JSON - Smart startup script with dependency auto-detection - Automated test suite with lua-socket HTTP client - Manual curl test suite for development workflow - Complete documentation and installation guide - FROM: Go (Google-controlled) → TO: Lua (PUC-Rio University) - Corporate-free dependency chain: lua-socket + lua-cjson + lua-ssl - Performance superior: < 1ms response time, minimal memory usage - Foundation for planned C+Lua hybrid architecture - furt-lua/src/main.lua - HTTP-Server implementation - furt-lua/config/server.lua - Lua-based configuration - furt-lua/scripts/start.sh - Startup with dependency checks - furt-lua/scripts/test_curl.sh - Manual testing suite - furt-lua/tests/test_http.lua - Automated test framework - furt-lua/README.md - Implementation documentation - README.md - Document Go→Lua migration strategy - .gitignore - Add Lua artifacts, luarocks, issue-scripts All endpoints tested and working: ✓ Health check returns proper JSON status ✓ Test endpoint processes POST requests with JSON ✓ Mail endpoint validates required fields (name, email, message) ✓ Error handling returns appropriate HTTP status codes Ready for Week 2: SMTP integration with mail.dragons-at-work.de Completes #63 Related #62
2025-06-17 20:40:40 +02:00
local ConfigParser = require("src.config_parser")
-- Load configuration from furt.conf
local config = ConfigParser.load_config()
-- Configure rate limiting from config
local RateLimiter = require("src.rate_limiter")
local rate_limits = {
api_key_max = config.security and config.security.rate_limit_api_key_max or 60,
ip_max = config.security and config.security.rate_limit_ip_max or 100,
window = config.security and config.security.rate_limit_window or 3600
}
RateLimiter:configure(rate_limits)
-- Parse CORS origins from config or environment
local function get_cors_origins()
-- 1. Try config file first
if config.server.cors_allowed_origins then
local origins = {}
for origin in config.server.cors_allowed_origins:gmatch("([^,]+)") do
table.insert(origins, origin:match("^%s*(.-)%s*$"))
end
return origins
end
-- 2. Try environment variable
local env_origins = os.getenv("CORS_ALLOWED_ORIGINS")
if env_origins then
local origins = {}
for origin in env_origins:gmatch("([^,]+)") do
table.insert(origins, origin:match("^%s*(.-)%s*$"))
end
return origins
end
-- 3. Development defaults
return {
"http://localhost:1313", -- Hugo dev server
"http://127.0.0.1:1313", -- Hugo dev server alternative
"http://localhost:3000", -- Common dev port
"http://127.0.0.1:3000" -- Common dev port alternative
}
end
-- Add legacy compatibility and runtime enhancements
local server_config = {
-- HTTP Server settings (from [server] section)
host = config.server.host,
port = config.server.port,
-- Timeouts and limits
client_timeout = config.server.client_timeout or 10,
-- CORS Configuration (prioritize config file over environment)
cors = {
allowed_origins = get_cors_origins()
},
feat(furt): implement complete Lua HTTP-Server for digital sovereignty (#63) - Add furt-lua/ directory with pure Lua implementation - Replace planned Go implementation with Corporate-free technology - Complete Week 1 Challenge: HTTP-Server to production-ready in 48min - HTTP-Server in pure Lua (185 lines, lua-socket based) - JSON API endpoints with request/response parsing - Modular architecture: each file < 200 lines - Error handling for 404, 400, validation scenarios - GET /health - Service health check with timestamp - POST /test - Development testing with request echo - POST /v1/mail/send - Mail service foundation with validation - Comprehensive error responses with structured JSON - Smart startup script with dependency auto-detection - Automated test suite with lua-socket HTTP client - Manual curl test suite for development workflow - Complete documentation and installation guide - FROM: Go (Google-controlled) → TO: Lua (PUC-Rio University) - Corporate-free dependency chain: lua-socket + lua-cjson + lua-ssl - Performance superior: < 1ms response time, minimal memory usage - Foundation for planned C+Lua hybrid architecture - furt-lua/src/main.lua - HTTP-Server implementation - furt-lua/config/server.lua - Lua-based configuration - furt-lua/scripts/start.sh - Startup with dependency checks - furt-lua/scripts/test_curl.sh - Manual testing suite - furt-lua/tests/test_http.lua - Automated test framework - furt-lua/README.md - Implementation documentation - README.md - Document Go→Lua migration strategy - .gitignore - Add Lua artifacts, luarocks, issue-scripts All endpoints tested and working: ✓ Health check returns proper JSON status ✓ Test endpoint processes POST requests with JSON ✓ Mail endpoint validates required fields (name, email, message) ✓ Error handling returns appropriate HTTP status codes Ready for Week 2: SMTP integration with mail.dragons-at-work.de Completes #63 Related #62
2025-06-17 20:40:40 +02:00
-- Logging
log_level = config.server.log_level or "info",
log_requests = config.server.log_requests or true,
-- Security settings
security = {
enable_test_endpoint = config.security and config.security.enable_test_endpoint or false,
rate_limits = rate_limits
},
-- API Keys (converted from nginx-style to old format for backward compatibility)
api_keys = config.api_keys,
-- Default SMTP config (for legacy compatibility)
mail = config.smtp_default,
-- Multi-tenant mail configuration function
get_mail_config_for_api_key = function(api_key)
return ConfigParser.get_mail_config_for_api_key(config, api_key)
end,
-- Raw config access (for advanced usage)
raw_config = config
feat(furt): implement complete Lua HTTP-Server for digital sovereignty (#63) - Add furt-lua/ directory with pure Lua implementation - Replace planned Go implementation with Corporate-free technology - Complete Week 1 Challenge: HTTP-Server to production-ready in 48min - HTTP-Server in pure Lua (185 lines, lua-socket based) - JSON API endpoints with request/response parsing - Modular architecture: each file < 200 lines - Error handling for 404, 400, validation scenarios - GET /health - Service health check with timestamp - POST /test - Development testing with request echo - POST /v1/mail/send - Mail service foundation with validation - Comprehensive error responses with structured JSON - Smart startup script with dependency auto-detection - Automated test suite with lua-socket HTTP client - Manual curl test suite for development workflow - Complete documentation and installation guide - FROM: Go (Google-controlled) → TO: Lua (PUC-Rio University) - Corporate-free dependency chain: lua-socket + lua-cjson + lua-ssl - Performance superior: < 1ms response time, minimal memory usage - Foundation for planned C+Lua hybrid architecture - furt-lua/src/main.lua - HTTP-Server implementation - furt-lua/config/server.lua - Lua-based configuration - furt-lua/scripts/start.sh - Startup with dependency checks - furt-lua/scripts/test_curl.sh - Manual testing suite - furt-lua/tests/test_http.lua - Automated test framework - furt-lua/README.md - Implementation documentation - README.md - Document Go→Lua migration strategy - .gitignore - Add Lua artifacts, luarocks, issue-scripts All endpoints tested and working: ✓ Health check returns proper JSON status ✓ Test endpoint processes POST requests with JSON ✓ Mail endpoint validates required fields (name, email, message) ✓ Error handling returns appropriate HTTP status codes Ready for Week 2: SMTP integration with mail.dragons-at-work.de Completes #63 Related #62
2025-06-17 20:40:40 +02:00
}
-- Print configuration summary on load
print("Furt Multi-Tenant Configuration Loaded:")
print(" Server: " .. server_config.host .. ":" .. server_config.port)
print(" Log Level: " .. server_config.log_level)
-- Print CORS configuration
print(" CORS Origins:")
for i, origin in ipairs(server_config.cors.allowed_origins) do
print(" " .. i .. ": " .. origin)
end
-- Print security configuration
print(" Test Endpoint: " .. (server_config.security.enable_test_endpoint and "enabled" or "disabled"))
print(" Default SMTP: " .. (config.smtp_default.host or "not configured"))
-- Print API key information
local api_key_count = 0
for key_name, key_config in pairs(config.api_keys) do
api_key_count = api_key_count + 1
-- Check if this API key has mail permissions
local has_mail_permission = false
if key_config.permissions then
for _, perm in ipairs(key_config.permissions) do
if perm == "mail:send" or perm == "*" then
has_mail_permission = true
break
end
end
end
local smtp_info = ""
if key_config.mail_smtp_host then
smtp_info = " (custom SMTP: " .. key_config.mail_smtp_host .. ")"
end
if has_mail_permission then
print(" API Key: " .. key_config.name .. " -> " .. key_config.mail_to .. smtp_info)
else
print(" API Key: " .. key_config.name .. " (no mail)" .. smtp_info)
end
end
print(" Total API Keys: " .. api_key_count)
return server_config