#!/usr/bin/lua -- Main CGI entry point for the Skyweave API. -- This script is intended to be deployed at /www/cgi-bin/skyweave.lua. -- -- It assumes that: -- 1. The skyweave framework modules (router, request, handlers, etc.) -- are located in /usr/lib/lua/skyweave/ -- (e.g., /usr/lib/lua/skyweave/router.lua). -- 2. The json.lua library (originally from /app/lib/json.lua) is available -- in a standard Lua path as 'json.lua' (e.g., /usr/lib/lua/json.lua). -- package.path is assumed to be configured correctly in the target OpenWrt environment. local p_response_ok, response_module = pcall(require, "skyweave.response") -- Try to load json independently for emergency_error, in case response_module loading failed or it doesn't expose json table directly. local p_json_ok, json_for_emergency = pcall(require, "json") local function emergency_error(message, status_code) status_code = status_code or 500 print("Status: " .. status_code) print("Content-Type: application/json") print("") if p_json_ok then print(json_for_emergency.encode({ errCode = -999, errMsg = "Framework Panic: " .. message })) else -- Fallback to a very basic JSON-like string if no JSON encoder is available print('{"errCode": -999, "errMsg": "Framework Panic: ' .. message:gsub('[\\"]', '\%1') .. '"}') end end local success, load_result = pcall(function() -- response_module is already attempted to be loaded by pcall above. -- If p_response_ok is false, response_module is the error object. if not p_response_ok then -- If the core response module itself failed, we cannot proceed with normal operations. -- The error from loading response_module is in 'response_module' variable here. emergency_error("Core component 'skyweave.response' failed to load: " .. tostring(response_module)) return -- Stop execution end local Router = require("skyweave.router") local request_context_builder = require("skyweave.request") local network_handlers = require("skyweave.handlers.network") local system_handlers = require("skyweave.handlers.system") local wifi_handlers = require("skyweave.handlers.wifi") local router = Router:new() router:add_route("GET", "/network_config", network_handlers.get_network_config) router:add_route("POST", "/connect_wifi", network_handlers.set_connect_wifi) router:add_route("GET", "/SOS", network_handlers.set_connect_wifi_sos) router:add_route("GET", "/wifi_scan", wifi_handlers.get_wifi_scan) router:add_route("GET", "/system_log_parsed", system_handlers.get_system_log_parsed) local request = request_context_builder.create_request_context() if not request then -- Use the already loaded response_module if available, otherwise the basic emergency_error if p_response_ok then response_module.error("Failed to create request context.", 500) else emergency_error("Failed to create request context.", 500) end return end router:dispatch(request) end) if not success then local error_message = "Critical error in main script execution: " .. tostring(load_result) -- Use the already loaded response_module if available, otherwise the basic emergency_error if p_response_ok then response_module.error(error_message, 500, -999) else emergency_error(error_message, 500) end end