2021-10-18 13:59:21 +08:00
|
|
|
#include "rendering.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <boost/beast.hpp>
|
|
|
|
#include <inja/inja.hpp>
|
|
|
|
|
|
|
|
std::string template_root_;
|
|
|
|
std::string static_root_;
|
|
|
|
|
|
|
|
void init_rendering(const std::string& template_root) {
|
|
|
|
template_root_ = template_root;
|
|
|
|
if (template_root_[template_root_.size() - 1] != '/')
|
|
|
|
template_root_.push_back('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_static_root(const std::string& static_root) {
|
|
|
|
static_root_ = static_root;
|
|
|
|
if (static_root_[static_root_.size() - 1] != '/')
|
|
|
|
static_root_.push_back('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
std::nullopt_t render(
|
|
|
|
bserv::response_type& response,
|
|
|
|
const std::string& template_file,
|
|
|
|
const boost::json::object& context) {
|
|
|
|
response.set(bserv::http::field::content_type, "text/html");
|
|
|
|
inja::json data = inja::json::parse(boost::json::serialize(context));
|
2021-11-25 18:00:28 +08:00
|
|
|
response.body() = inja::Environment{}.render_file(template_root_ + template_file, data);
|
2021-10-18 13:59:21 +08:00
|
|
|
response.prepare_payload();
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::nullopt_t serve(
|
|
|
|
bserv::response_type& response,
|
|
|
|
const std::string& file) {
|
2021-11-25 18:00:28 +08:00
|
|
|
return bserv::utils::file::serve(response, static_root_ + file);
|
2021-10-18 13:59:21 +08:00
|
|
|
}
|