2019-04-30 19:29:17 -06:00
|
|
|
#!/usr/bin/lua
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
|
2022-07-13 15:36:53 -06:00
|
|
|
Copyright (C) 2021 Darryl Quinn
|
2019-04-30 19:29:17 -06:00
|
|
|
See Contributors file for additional contributors
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation version 3 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Additional Terms:
|
|
|
|
|
|
|
|
Additional use restrictions exist on the AREDN(TM) trademark and logo.
|
|
|
|
See AREDNLicense.txt for more info.
|
|
|
|
|
|
|
|
Attributions to the AREDN Project must be retained in the source code.
|
|
|
|
If importing this code into a new or existing project attribution
|
|
|
|
to the AREDN project must be added to the source code.
|
|
|
|
|
|
|
|
You must not misrepresent the origin of the material contained within.
|
|
|
|
|
|
|
|
Modified versions must be modified to attribute to the original source
|
|
|
|
and be marked in reasonable ways as differentiate it from the original
|
|
|
|
version
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
require("uci")
|
|
|
|
require("aredn.uci")
|
|
|
|
require("aredn.utils")
|
|
|
|
require("aredn.http")
|
|
|
|
require("nixio")
|
|
|
|
local json = require("luci.jsonc")
|
|
|
|
require("iwinfo")
|
|
|
|
|
|
|
|
-- Function extensions
|
|
|
|
os.capture = capture
|
|
|
|
|
2022-07-13 15:36:53 -06:00
|
|
|
|
|
|
|
_G.switch = function(param, case_table)
|
|
|
|
local case = case_table[param]
|
|
|
|
if case then return case() end
|
|
|
|
local def = case_table['default']
|
|
|
|
return def and def() or nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function addQueryStringData(qs, qsset)
|
|
|
|
local res = {}
|
|
|
|
res['raw'] = qs
|
|
|
|
if res['raw'] ~= nil then
|
|
|
|
res['params'] = qsset
|
|
|
|
end
|
|
|
|
return res
|
2019-04-30 19:29:17 -06:00
|
|
|
end
|
|
|
|
|
2022-07-13 15:36:53 -06:00
|
|
|
-- ==== MAIN =====
|
|
|
|
local request={}
|
|
|
|
local response={}
|
|
|
|
local topic=nil
|
2019-04-30 19:29:17 -06:00
|
|
|
|
2022-07-13 15:36:53 -06:00
|
|
|
-- PRE-PROCESS INCOMING REQUEST
|
2019-04-30 19:29:17 -06:00
|
|
|
local qsset={}
|
2022-07-13 15:36:53 -06:00
|
|
|
local qs=""
|
|
|
|
local method=""
|
|
|
|
|
|
|
|
if (arg[1]==nil) then -- FROM CGI
|
|
|
|
method=nixio.getenv("REQUEST_METHOD")
|
2019-04-30 19:29:17 -06:00
|
|
|
qs=nixio.getenv("QUERY_STRING")
|
2022-07-13 15:36:53 -06:00
|
|
|
qsset=parseQueryString(qs)
|
|
|
|
else -- FROM CMDLINE: ./apiprotected GET topic=setup_basic
|
|
|
|
method=arg[1]
|
|
|
|
qsset=parseQueryString(arg[2])
|
|
|
|
end
|
|
|
|
|
|
|
|
request['method']=method
|
|
|
|
request['authorization']=nixio.getenv("HTTP_AUTHORIZATION")
|
|
|
|
request['content_type']=nixio.getenv("CONTENT_TYPE")
|
|
|
|
request['query_string'] = addQueryStringData(qs, qsset)
|
|
|
|
|
|
|
|
-- ADD additional elements by METHOD type
|
|
|
|
if method=='GET' then -- READ
|
|
|
|
topic=request['query_string']['params']['topic']
|
|
|
|
elseif method=='OPTIONS' then -- OPTIONS
|
|
|
|
-- skip
|
|
|
|
elseif method=='POST' then -- UPDATE
|
|
|
|
request['content_length']=nixio.getenv("CONTENT_LENGTH")
|
|
|
|
rawcontent=io.read("*all")
|
|
|
|
request['content']=json.parse(rawcontent)
|
|
|
|
topic = request['content']['topic']
|
|
|
|
elseif method=='PUT' then -- CREATE
|
|
|
|
request['content_length']=nixio.getenv("CONTENT_LENGTH")
|
|
|
|
request['content']=io.read("*all")
|
|
|
|
elseif method=='DELETE' then -- DELETE
|
|
|
|
local x=true
|
2019-04-30 19:29:17 -06:00
|
|
|
else
|
2022-07-13 15:36:53 -06:00
|
|
|
request['content_length']=nixio.getenv("CONTENT_LENGTH")
|
|
|
|
request['content']=io.read("*all")
|
2019-04-30 19:29:17 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-07-13 15:36:53 -06:00
|
|
|
|
|
|
|
-- -------------------------------------------------
|
|
|
|
function getController(t, req)
|
|
|
|
cname = "aredn.controller." .. t
|
|
|
|
-- TODO: need to catch errors on non-existent controllers below
|
|
|
|
local status, c = pcall(require, cname)
|
|
|
|
if (status) then
|
|
|
|
return c:new(req)
|
|
|
|
else
|
|
|
|
return nil
|
2019-04-30 19:29:17 -06:00
|
|
|
end
|
2022-07-13 15:36:53 -06:00
|
|
|
end
|
2019-04-30 19:29:17 -06:00
|
|
|
|
2022-07-13 15:36:53 -06:00
|
|
|
|
|
|
|
-- determine the proper controller
|
|
|
|
if topic then
|
|
|
|
local controller = getController(topic, request) -- setup.basic, setup.meshrf, etc
|
|
|
|
if (controller) then
|
|
|
|
response=controller:process()
|
|
|
|
else
|
|
|
|
response['error']="Controller '" .. topic .. "' not found"
|
|
|
|
response['success']=false
|
2019-04-30 19:29:17 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Output the HTTP header for JSON
|
|
|
|
-- json_header()
|
|
|
|
json_header()
|
|
|
|
|
|
|
|
-- Output the info table as json
|
2024-04-01 23:15:45 -06:00
|
|
|
print(json.stringify(response,true))
|