Low level operations in a Mod

Post Reply
carloslfu
New member
Posts: 1
Joined: Sun Feb 25, 2018 05:37
GitHub: carloslfu
IRC: carloslfu
In-game: carloslfu

Low level operations in a Mod

by carloslfu » Post

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.

User avatar
orwell
Member
Posts: 958
Joined: Wed Jun 24, 2015 18:45
GitHub: orwell96
IRC: orwell96_mt
In-game: orwell
Location: Raxacoricofallapatorius

Re: Low level operations in a Mod

by orwell » Post

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
Lua is great!
List of my mods
I like singing. I like dancing. I like ... niyummm...

User avatar
Linuxdirk
Member
Posts: 3217
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Low level operations in a Mod

by Linuxdirk » Post

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.

User avatar
sorcerykid
Member
Posts: 1841
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Low level operations in a Mod

by sorcerykid » Post

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).
Last edited by sorcerykid on Thu Mar 22, 2018 17:45, edited 1 time in total.

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Low level operations in a Mod

by rubenwardy » Post

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
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Low level operations in a Mod

by sofar » Post

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.

User avatar
Linuxdirk
Member
Posts: 3217
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Low level operations in a Mod

by Linuxdirk » Post

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.

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Low level operations in a Mod

by rubenwardy » Post

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
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
Linuxdirk
Member
Posts: 3217
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Low level operations in a Mod

by Linuxdirk » Post

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.

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Low level operations in a Mod

by sofar » Post

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.

User avatar
Linuxdirk
Member
Posts: 3217
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Low level operations in a Mod

by Linuxdirk » Post

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 :)

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: Low level operations in a Mod

by Byakuren » Post

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?
Every time a mod API is left undocumented, a koala dies.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests