mqttagent

MQTT Lua Agent
git clone https://git.instinctive.eu/mqttagent.git
Log | Files | Refs | README | LICENSE

commit cb12004e0ad5cd9a0d6cc5e13f8bab676582994a
parent d110de738c52eab27c02fe4e45a91ee32f9cc401
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Wed,  4 Jun 2025 19:39:14 +0000

HTTP request module in mqttagent-full
Diffstat:
Mcmd/mqttagent-full/main.go | 20++++++++++++++++++++
Mgo.mod | 1+
Mgo.sum | 2++
3 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/cmd/mqttagent-full/main.go b/cmd/mqttagent-full/main.go @@ -19,10 +19,14 @@ package main import ( "database/sql" "log" + "net/http" + "net/url" "os" "runtime/debug" + "strings" "time" + "github.com/cjoudrey/gluahttp" _ "github.com/glebarez/go-sqlite" luajson "github.com/layeh/gopher-json" "github.com/yuin/gopher-lua" @@ -36,6 +40,8 @@ type fullMqttAgent struct { func (agent *fullMqttAgent) Setup(L *lua.LState) { luajson.Preload(L) + L.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader) + L.SetGlobal("urlencode", L.NewFunction(luaUrlEncode)) setBuildInfo(L, "buildinfo") setVersion(L, "version") agent.loggers = make(map[string]*sqlogger) @@ -85,6 +91,20 @@ func (agent *fullMqttAgent) ReloadEnd(oldL, newL *lua.LState) { agent.oldLoggers = nil } +func luaUrlEncode(L *lua.LState) int { + tbl := L.CheckTable(1) + result := make([]string, 0, tbl.Len()) + + L.ForEach(tbl, func(key, value lua.LValue) { + skey := url.QueryEscape(lua.LVAsString(key)) + sval := url.QueryEscape(lua.LVAsString(value)) + result = append(result, skey+"="+sval) + }) + + L.Push(lua.LString(strings.Join(result, "&"))) + return 1 +} + type sqlogger struct { db *sql.DB insertReceived *sql.Stmt diff --git a/go.mod b/go.mod @@ -3,6 +3,7 @@ module instinctive.eu/go/mqttagent go 1.21.9 require ( + github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9 github.com/glebarez/go-sqlite v1.22.0 github.com/go-mqtt/mqtt v0.0.0-20210702165922-b33ea0451b0b github.com/layeh/gopher-json v0.0.0-20201124131017-552bb3c4c3bf diff --git a/go.sum b/go.sum @@ -1,3 +1,5 @@ +github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9 h1:rdWOzitWlNYeUsXmz+IQfa9NkGEq3gA/qQ3mOEqBU6o= +github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9/go.mod h1:X97UjDTXp+7bayQSFZk2hPvCTmTZIicUjZQRtkwgAKY= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=