mirror of https://github.com/aredn/aredn.git
142 lines
3.8 KiB
Lua
Executable File
142 lines
3.8 KiB
Lua
Executable File
#!/usr/bin/lua
|
|
--[[
|
|
|
|
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
|
|
Copyright (C) 2021 Darryl Quinn
|
|
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")
|
|
local aredn_olsr = require("aredn.olsr")
|
|
local aredn_info = require("aredn.info")
|
|
require("nixio")
|
|
local json = require("luci.jsonc")
|
|
require("iwinfo")
|
|
|
|
-- Function extensions
|
|
os.capture = capture
|
|
|
|
|
|
_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
|
|
end
|
|
|
|
-- ==== MAIN =====
|
|
local request={}
|
|
local response={}
|
|
local topic=nil
|
|
|
|
-- PRE-PROCESS INCOMING REQUEST
|
|
local qsset={}
|
|
local qs=""
|
|
local method=""
|
|
|
|
if (arg[1]==nil) then -- FROM CGI
|
|
method=nixio.getenv("REQUEST_METHOD")
|
|
qs=nixio.getenv("QUERY_STRING")
|
|
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
|
|
else
|
|
request['content_length']=nixio.getenv("CONTENT_LENGTH")
|
|
request['content']=io.read("*all")
|
|
end
|
|
|
|
|
|
|
|
-- -------------------------------------------------
|
|
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
|
|
end
|
|
end
|
|
|
|
|
|
-- 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
|
|
end
|
|
end
|
|
|
|
-- Output the HTTP header for JSON
|
|
-- json_header()
|
|
json_header()
|
|
|
|
-- Output the info table as json
|
|
print(json.stringify(response,true)) |