Added ability to register webadmin pages through Info.lua #5003
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
This is just something I've thought about for years, but never had the time to actually make. I'd like to get other opinions on this.
I've always felt the way web pages are handled in Cuberite is less than ideal. I've had a few ideas on how to make it a bit better for years. Now I finally have the time to work on it.
Info.lua
With this PR you can define your webpages inside the
Info.lua
file:Controllers
Inside the
Info.lua
you can define all the web pages, but instead of providing a callback function you can provide a class. This class inherits fromWebControllerBase
which i've created insideInfoReg.lua
. This controller handles the web pages for a single page, but it can have multiple endpoints. TheWebControllerBase
class provides additional functionality to return json, files and views (more on that later).Currently I have a really simple routing system in place. It takes the HTTP method and puts it in front of the actionname (default is Index). If I want to access a different endpoint I'd have to add a query parameter called
action
.Views
Returning the html code is, in my opinion, really clunky at the moment. This PR includes a really simple templating engine that allows you to write Lua code through html. The views are loaded from a subfolder called
webviews
inside the plugin's own folder. For example you can write code like this:It's also possible to pass variables from the controller to the view. For example, using the controller code above:
Problems
Currently I'm struggling with a few things in this PR.
WebControllerBase
. This means, to be safe, you'll have to usedofile
in every file which contains a controller.