Page 1 of 1

Low level operations in a Mod

Posted: Sun Feb 25, 2018 14:03
by carloslfu
Hi. There is a way for performing low level operations such as fetching an url, write a file, setup an http server inside a Minetest mod?

Thanks in advance.

Re: Low level operations in a Mod

Posted: Wed Mar 21, 2018 21:41
by orwell
Fetching an URL works via the curl HTTP API. Look into lua_api.txt how to obtain this. In every case your mod then needs to be trusted to use HTTP (added to the secure.http_mods list)
Writing to a file uses the standard Lua io library, but without the insecure environment (also needs to be explicitly enabled via secure.trusted_mods) you can only write into the world directory and read from there and the mod directory
setting up an HTTP server: why? No, there won't be a way to do this because you won't need it and it is a security issue

Re: Low level operations in a Mod

Posted: Thu Mar 22, 2018 17:17
by Linuxdirk
orwell wrote:setting up an HTTP server: why? No, there won't be a way to do this because you won't need it and it is a security issue
if you find a way to implement HTTP in plain Lua then it would of course work.

Re: Low level operations in a Mod

Posted: Thu Mar 22, 2018 17:43
by sorcerykid
carloslfu wrote:Hi. There is a way for performing low level operations such as fetching an url, write a file, setup an http server inside a Minetest mod?
An HTTP server needs to be asynchronous. That would require a Lua coroutine for the socket and the I/O operations. Unless you need realtime client-server communication, you might be better off finding some way to accomplish this through IPC (such as an independent CGI-script polling a FIFO).

Re: Low level operations in a Mod

Posted: Thu Mar 22, 2018 17:44
by rubenwardy
Don't run a web server from Minetest, there's no need and it will cause massive performance issues. Instead run a server separately, and have Minetest poll it.

Use the HTTP API to make HTTP requests

Use the built-in Lua IO library for writing to a file, however I recommend avoiding that and using mod_storage instead
Linuxdirk wrote:
orwell wrote:setting up an HTTP server: why? No, there won't be a way to do this because you won't need it and it is a security issue
if you find a way to implement HTTP in plain Lua then it would of course work.
No, that's not how you'd do it if you needed to, which you don't

Re: Low level operations in a Mod

Posted: Thu Mar 22, 2018 18:27
by sofar
I could see some value in remote debugging capabilities if there was some sort of object model that could be inspected while a server was running, especially if it included the entire Lua context.

But I'd likely just link libmicrohttpd and then some really hard and complex work would have to be done to safely expose the available data over that interface. That seems not worth it, considering the vagueness of your question.

Re: Low level operations in a Mod

Posted: Fri Mar 23, 2018 09:26
by Linuxdirk
rubenwardy wrote:
Linuxdirk wrote:if you find a way to implement HTTP in plain Lua then it would of course work.
No, that's not how you'd do it if you needed to, which you don't
So you know what I want for fun and entertainment? That's great. Thanks for telling me.

Not.

Re: Low level operations in a Mod

Posted: Fri Mar 23, 2018 09:52
by rubenwardy
You can't write a HTTP implementation in plain Lua, as Lua doesn't support sockets. You'd need to depend on sockets at which point you might as well use a http library

Also, I said need. Not want

Re: Low level operations in a Mod

Posted: Fri Mar 23, 2018 14:40
by Linuxdirk
rubenwardy wrote:You can't write a HTTP implementation in plain Lua, as Lua doesn't support sockets.
Mmmh, challenge accepted :)

Having something like an HTTP server (at least the general idea behind HTTP) inside Minetest should be easy to do using some Lua magic ad the Minetest API.

Re: Low level operations in a Mod

Posted: Fri Mar 23, 2018 17:36
by sofar
Linuxdirk wrote: Having something like an HTTP server (at least the general idea behind HTTP) inside Minetest should be easy to do using some Lua magic ad the Minetest API.
There is some scientific value in doing this - you can learn from it, figure out why it's not a good idea, and that on a production server it most likely will cause significant lag to players if you're trying to push any significant amount of data through it. Of course, you could push it a lot further and solve the underlying problems too and come up with a full async Lua environment that could handle this, and learn from that experience as well.

However, if you have significant amount of data that needs to be available to other processes, you're much better off storing that data in a shared storage "thing" that other processes can already access at the same time. This has been proven successful on serveral servers and this is what e.g. ITB does to calculate player scores and publish the ITB website stats.

This design is much more reliable and actually far simpler. You'd make MT more complex by attaching functionality to it that doesn't need to be in the same process space, and can safely live in a separate process. Ultimately not doing this is a better scaling approach than stuffing everything in the same process space.

Re: Low level operations in a Mod

Posted: Fri Mar 23, 2018 17:49
by Linuxdirk
sofar wrote:figure out why it's not a good idea, and that on a production server it most likely will cause significant lag to players if you're trying to push any significant amount of data through it.
It's a stupid idea trying to add an HTTP server to Minetest via mods (even adding something working like an HTTP server within Minetest would be stupid), yes. But it sounds like a fun weekend project :)

Re: Low level operations in a Mod

Posted: Wed Apr 11, 2018 07:34
by Byakuren
Linuxdirk wrote:
sofar wrote:figure out why it's not a good idea, and that on a production server it most likely will cause significant lag to players if you're trying to push any significant amount of data through it.
It's a stupid idea trying to add an HTTP server to Minetest via mods (even adding something working like an HTTP server within Minetest would be stupid), yes. But it sounds like a fun weekend project :)
Did you ever get around to trying this out?