Skip to content

Instantly share code, notes, and snippets.

View daurnimator's full-sized avatar

daurnimator

View GitHub Profile
@daurnimator
daurnimator / fengari-vue.lua
Last active June 17, 2021 11:43
Playing with Vue from fengari
-- Load Vue library
package.loadlib("https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js", "*")
-- Get Object helper
local Object = dofile("https://gist.githubusercontent.com/daurnimator/5a7fa933e96e14333962093322e0ff95/raw/8c6968be0111c7becc485a692162ad100e87d9c7/Object.lua").Object
local myapp = js.new(js.global.Vue, Object{
el = "#foo";
template = [[
<div id="foo">{{message}}</div>
]];
@daurnimator
daurnimator / tarantool_cqueues.lua
Last active February 20, 2023 05:56
Tarantool + cqueues for lua-http
-- This code monkey patches cqueues primitives to allow for transparent use of cqueues inside of tarantool
local cqueues = require "cqueues"
local fiber = require "fiber"
local socket = require "socket" -- https://tarantool.org/en/doc/1.7/reference/reference_lua/socket.html (not luasocket)
local old_step; old_step = cqueues.interpose("step", function(self, timeout)
if cqueues.running() then
fiber.yield()
return old_step(self, timeout)
@daurnimator
daurnimator / http_server.sh
Created March 1, 2018 06:09
Quick openssl server
# Generate a key + cert. see https://daurnimator.com/post/115624714644/howto-generate-a-self-signed-ssl-cert-in-one
openssl req -newkey 2048 -keyout server.pem -nodes -x509 -out server.pem -batch
# Start http server
openssl s_server -port 4555 -www
@daurnimator
daurnimator / tarantool-websockets.lua
Created February 15, 2018 04:11
Tarantool + lua-http websockets nonblocking
#!/usr/bin/env tarantool
local cqueues = require "cqueues"
local fiber = require "fiber"
local socket = require "socket"
package.loaded["http.client"] = nil -- tarantool has a namespace clash
local websocket = require "http.websocket"
local cq = cqueues.new()
-- Hook up cqueues loop inside tarantool fiber
@daurnimator
daurnimator / http.conf
Created January 14, 2018 02:20
Nginx config snippet for serving acme-challenges on port 80 + redirect to https
server {
listen 80 default_server;
listen [::]:80 default_server;
location /.well-known/acme-challenge {
alias /etc/dehydrated/.acme-challenges;
}
location / {
return 302 https://$host$request_uri;
@daurnimator
daurnimator / nodepackagesearchers.js
Last active January 14, 2018 04:41
Add node's require() to lua's package.searchers
{ /* add node's require() to lua searchers */
lauxlib.luaL_requiref(L, lua.to_luastring("package"), lualib.luaopen_package, 0);
if (lua.lua_getfield(L, -1, lua.to_luastring("searchers")) != lua.LUA_TTABLE)
lauxlib.luaL_error(L, lua.to_luastring("'package.searchers' must be a table"));
lua.lua_pushcfunction(L, function(L) {
let name = lauxlib.luaL_checkstring(L, 1);
let path;
try {
path = require.resolve(lua.to_jsstring(name));
} catch(e) {
@daurnimator
daurnimator / Object.lua
Created January 8, 2018 11:48
Fengari table=>Object helper
local js = require "js"
-- Helper to copy lua table to a new JavaScript Object
-- e.g. Object{mykey="myvalue"}
local function Object(t)
local o = js.new(js.global.Object)
for k, v in pairs(t) do
assert(type(k) == "string" or js.typeof(k) == "symbol", "JavaScript only has string and symbol keys")
o[k] = v
end
@daurnimator
daurnimator / dom-create.lua
Last active February 5, 2018 17:34
Snippet for creating dom elements with fengari
local js = require "js"
local window = js.global
local document = window.document
local function setElement(elem, props)
for k, v in pairs(props) do
if type(k) == "number" then
-- skip
elseif type(v) == "table" then
local to = elem[k]
@daurnimator
daurnimator / peerfinder.script
Last active September 16, 2017 04:23
https://dn42.us/peers/script.sh in mikrotik scripting lanaguage
{
:local peerid $dn42peerid
:local nbpings 5
/tool fetch url=("https://dn42.us/peers/pending/" . $peerid) dst-path=dn42-peerfinder
:local pending [/file get dn42-peerfinder contents]
/file remove dn42-peerfinder
:local reqidstart [:find $pending "\"req_id\": \"" -1]
:if ($reqidstart > 0) do={
:set reqidstart ($reqidstart+11)
:local reqid [:pick $pending $reqidstart [:find $pending "\"" $reqidstart]]
@daurnimator
daurnimator / htmlgen.lua
Last active August 4, 2017 09:33
experiment for creating html in fengari
local document = js.global.document;
local extra = {}
function extra:append(x)
self.__wrapped__:append(x)
return self
end
local function wrap(e)
return setmetatable({