From 850cb3d307a2bb4ee36f939f4eb00d1e8034140a Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Tue, 5 Oct 2010 00:05:50 +0200 Subject: [PATCH] openwrt: Switching from lucid to uhttpd. The restful.lua CGI script has been kept sober on purpose to boost performance. The RESTful API can now handle 10 calls/s. --- mote/v1/openwrt/.config | 14 ++-- mote/v1/openwrt/files/etc/config/lucid | 65 --------------- mote/v1/openwrt/files/etc/config/uhttpd | 83 +++++++++++++++++++ mote/v1/openwrt/package/flukso/Makefile | 6 +- .../v1/openwrt/package/flukso/src/restful.lua | 68 +++++++++++++++ 5 files changed, 160 insertions(+), 76 deletions(-) delete mode 100644 mote/v1/openwrt/files/etc/config/lucid create mode 100644 mote/v1/openwrt/files/etc/config/uhttpd create mode 100755 mote/v1/openwrt/package/flukso/src/restful.lua diff --git a/mote/v1/openwrt/.config b/mote/v1/openwrt/.config index 2a2300d..f71b7c1 100644 --- a/mote/v1/openwrt/.config +++ b/mote/v1/openwrt/.config @@ -1,7 +1,7 @@ # # Automatically generated make config: don't edit # OpenWrt version: Backfire (r23144) -# Sat Oct 2 11:12:24 2010 +# Mon Oct 4 13:53:08 2010 # CONFIG_HAVE_DOT_CONFIG=y # CONFIG_TARGET_ppc40x is not set @@ -1095,7 +1095,7 @@ CONFIG_PACKAGE_ntpclient=y # CONFIG_PACKAGE_arptables is not set # CONFIG_PACKAGE_avahi-autoipd is not set CONFIG_PACKAGE_avahi-daemon=y -# CONFIG_PACKAGE_avahi-dnsconfd is not set +CONFIG_PACKAGE_avahi-dnsconfd=y # CONFIG_PACKAGE_crda is not set # CONFIG_PACKAGE_ebtables is not set # CONFIG_PACKAGE_ebtables-utils is not set @@ -1129,7 +1129,9 @@ CONFIG_PACKAGE_ppp-mod-pppoe=y # CONFIG_PACKAGE_pppstats is not set CONFIG_PACKAGE_pptp=y # CONFIG_PACKAGE_tc is not set -# CONFIG_PACKAGE_uhttpd is not set +CONFIG_PACKAGE_uhttpd=y +CONFIG_PACKAGE_uhttpd-mod-lua=y +# CONFIG_PACKAGE_uhttpd-mod-tls is not set CONFIG_PACKAGE_wpa-supplicant=y # CONFIG_WPA_SUPPLICANT_NO_TIMESTAMP_CHECK is not set CONFIG_WPA_SUPPLICANT_INTERNAL=y @@ -1217,15 +1219,15 @@ CONFIG_PACKAGE_luci-httpclient=y CONFIG_PACKAGE_luci-ipkg=y CONFIG_PACKAGE_luci-json=y CONFIG_PACKAGE_luci-lmo=y -CONFIG_PACKAGE_luci-lucid=y +# CONFIG_PACKAGE_luci-lucid is not set CONFIG_PACKAGE_luci-nixio=y CONFIG_PACKAGE_luci-nixio_notls=y # CONFIG_PACKAGE_luci-nixio_axtls is not set # CONFIG_PACKAGE_luci-nixio_cyassl is not set # CONFIG_PACKAGE_luci-nixio_openssl is not set -CONFIG_PACKAGE_luci-px5g=y +# CONFIG_PACKAGE_luci-px5g is not set CONFIG_PACKAGE_luci-sgi-cgi=y -# CONFIG_PACKAGE_luci-sgi-uhttpd is not set +CONFIG_PACKAGE_luci-sgi-uhttpd=y CONFIG_PACKAGE_luci-sys=y CONFIG_PACKAGE_luci-uci=y CONFIG_PACKAGE_luci-uvl=y diff --git a/mote/v1/openwrt/files/etc/config/lucid b/mote/v1/openwrt/files/etc/config/lucid deleted file mode 100644 index 8d63ccc..0000000 --- a/mote/v1/openwrt/files/etc/config/lucid +++ /dev/null @@ -1,65 +0,0 @@ -config lucid main - option pollinterval 15000 - option threadlimit 10 - option daemonize 1 - option debug 0 - list supports tcpserver - list supports server - -config DirectoryPublisher webroot - option name 'Webserver Share' - option physical /www - option home 1 - option virtual '' - option domain '' - -config LuciWebPublisher luciweb - option name 'LuCI Webapplication' - option physical '' - list virtual /cgi-bin/luci - option domain '' - -config LuciWebPublisher fluksorest - option name 'Flukso JSON/REST API' - option physical '' - list virtual /sensor - option domain '' - -config tcpserver httpd - option entrypoint "luci.lucid.http" - list supports DirectoryPublisher - list supports LuciWebPublisher - -config daemon http - option slave httpd - list address 80 - list publisher webroot - list publisher luciweb - option nokeepalive 1 - option memlimit 1572864 - option enabled 1 - -config daemon restful - option slave httpd - list address 8080 - list publisher fluksorest - option nokeepalive 1 - option memlimit 1572864 - option enabled 1 - -config daemon https - option slave httpd - list address 443 - list publisher webroot - list publisher luciweb - option nokeepalive 1 - option memlimit 1572864 - option enabled 0 - option tls maincert - option encryption enable - -config tls maincert - option key /etc/nixio/rsa_main.der - option cert /etc/nixio/cert_main.der - option type asn1 - option generate 1 diff --git a/mote/v1/openwrt/files/etc/config/uhttpd b/mote/v1/openwrt/files/etc/config/uhttpd new file mode 100644 index 0000000..123aec8 --- /dev/null +++ b/mote/v1/openwrt/files/etc/config/uhttpd @@ -0,0 +1,83 @@ +# Server configuration +config uhttpd main + + # HTTP listen addresses, multiple allowed + list listen_http 0.0.0.0:80 +# list listen_http [::]:80 + + # HTTPS listen addresses, multiple allowed +# list listen_https 0.0.0.0:443 +# list listen_https [::]:443 + + # Server document root + option home /www + + # Reject requests from RFC1918 IP addresses + # directed to the servers public IP(s). + # This is a DNS rebinding countermeasure. + option rfc1918_filter 1 + + # Certificate and private key for HTTPS. + # If no listen_https addresses are given, + # the key options are ignored. + option cert /etc/uhttpd.crt + option key /etc/uhttpd.key + + # CGI url prefix, will be searched in docroot. + # Default is /cgi-bin + option cgi_prefix /cgi-bin + + # List of extension->interpreter mappings. + # Files with an associated interpreter can + # be called outside of the CGI prefix and do + # not need to be executable. +# list interpreter ".php=/usr/bin/php-cgi" +# list interpreter ".cgi=/usr/bin/perl" + + # Lua url prefix and handler script. + # Lua support is disabled if no prefix given. +# option lua_prefix /luci +# option lua_handler /usr/lib/lua/luci/sgi/uhttpd.lua + + # CGI/Lua timeout, if the called script does not + # write data within the given amount of seconds, + # the server will terminate the request with + # 504 Gateway Timeout response. + option script_timeout 60 + + # Network timeout, if the current connection is + # blocked for the specified amount of seconds, + # the server will terminate the associated + # request process. + option network_timeout 30 + + # Basic auth realm, defaults to local hostname +# option realm OpenWrt + + # Configuration file in busybox httpd format +# option config /etc/httpd.conf + +config uhttpd restful + list listen_http 0.0.0.0:8080 + option home /www + option cgi_prefix /sensor + option script_timeout 5 + option network_timeout 5 + +# Certificate defaults for px5g key generator +config cert px5g + + # Validity time + option days 730 + + # RSA key size + option bits 1024 + + # Location + option country DE + option state Berlin + option location Berlin + + # Common name + option commonname OpenWrt + diff --git a/mote/v1/openwrt/package/flukso/Makefile b/mote/v1/openwrt/package/flukso/Makefile index c28a700..096c524 100644 --- a/mote/v1/openwrt/package/flukso/Makefile +++ b/mote/v1/openwrt/package/flukso/Makefile @@ -33,11 +33,7 @@ endef define Package/flukso/install $(INSTALL_DIR) $(1)/usr/share/lua/flukso/ - $(CP) $(PKG_BUILD_DIR)/flukso.lua $(1)/usr/share/lua/flukso/ - $(CP) $(PKG_BUILD_DIR)/data.lua $(1)/usr/share/lua/flukso/ - $(CP) $(PKG_BUILD_DIR)/dbg.lua $(1)/usr/share/lua/flukso/ - $(CP) $(PKG_BUILD_DIR)/auth.lua $(1)/usr/share/lua/flukso/ - $(CP) $(PKG_BUILD_DIR)/heartbeat.lua $(1)/usr/share/lua/flukso/ + $(CP) $(PKG_BUILD_DIR)/{flukso,data,dbg,auth,heartbeat,restful}.lua $(1)/usr/share/lua/flukso/ $(INSTALL_DIR) $(1)/etc/init.d/ $(CP) $(PKG_BUILD_DIR)/flukso.init $(1)/etc/init.d/flukso $(INSTALL_DIR) $(1)/etc/config/ diff --git a/mote/v1/openwrt/package/flukso/src/restful.lua b/mote/v1/openwrt/package/flukso/src/restful.lua new file mode 100755 index 0000000..45042fc --- /dev/null +++ b/mote/v1/openwrt/package/flukso/src/restful.lua @@ -0,0 +1,68 @@ +#!/usr/bin/env lua + +--[[ + + restful.lua - CGI script providing a local RESTful API on the Fluksometer. + + Copyright (c) 2010 Bart Van Der Meerssche + + 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, either version 3 of the License, or + (at your option) any later version. + + 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 . + +]]-- + + +--- Decode a URL's query string +-- @param QS the to-be-decoded query string (optional) +-- @return table containing [name] = value pairs +local function query_decode(QS) + local param = {} + + for name, value in (QS or os.getenv("QUERY_STRING")):gmatch("([^&=]+)=([^&=]+)") do + param[name] = value + end + + return param +end + +--- Fetch the sensor id in the HTTP request. +-- @return sensor id +local function sensor_id() + return os.getenv("SCRIPT_NAME"):match("/sensor/([%x]+)") +end + + +local param = query_decode() +local path = "/tmp/sensor/" +local version = "1.0" + +-- Hardcoding path and version parameters lowers GET response time from 200ms to 90ms. +-- local uci = require "luci.model.uci".cursor() +-- local path = uci:get("flukso", "main", "localDir") .. "/" +-- local version = uci:get("flukso", "main", "localVersion") + +if param.interval == "minute" and param.unit == "watt" and param.version == version then + local pre, post = "", "" + + if param.jsonp_callback then + pre, post = param.jsonp_callback .. "(", ")" + end + + io.input(path .. sensor_id()) + + io.write("Content-Type: application/json", "\n\n") + io.write(pre, io.read("*all"), post) +else + io.write("status: 400 Bad Request", "\n\n") + io.write("Malformed query string: interval, unit and version query parameters are required.") +end