alcjzk / Webserv

0 stars 0 forks source link

Intitial TemplateEngine and ErrorResponse impl #25

Closed lsileoni closed 8 months ago

lsileoni commented 8 months ago

The current error response scheme was a bit janky. Ain't no one writing tens of different html files by hand. Thus, a I've made a very rudimentary templating engine which should be easy to extend for any more complex functionality.

Right now it's just taking a template.html file, performing a regexp search & substitution on it to replace a string with an error status code. More complex behaviour can be implemented but this should be enough for the subject's requirements on error statuses.

TemplateEngine is fairly simple, it's a regex, which matches template strings like so: image

And replaces them with their contemporary runtime values.

In the server side, we can define various valid placeholder variables and then add them to the replace mappings of the engine. Right now it's called like so:

// Creates the engine with the given template file(which is read from any valid document)
TemplateEngine    engine(_template_str);

// Adding value mappings is done like this
engine.set_value("status", status.text());
// So {{status}} in the template file is replaced with the actual status string of the server

// And then finally, "rendering" the text will perform all substitutions with the given replacement mappings
std::string rendered_template = engine.render();

Tests have been implemented in the TemplateEngine.*pp class files. They should show how it's expected to behave as of now.

Other minor changes in this pr:

This pull request closes #18

lsileoni commented 8 months ago

After some deliberation, a decision was to stick with a hybrid of configurability and templatability.

The templated solution covers the case where a user hasn't defined explicit error pages, but the server provides a convenient basic template for them.

If the user wants to customize error pages, they can do so through specifying the error page files in the config. These custom defined error pages also go through the templating engine.

Changes in: 5ed416c44823ca05b266f2d646f7525ca3d84e44