增加后端渲染

This commit is contained in:
xx
2025-01-19 21:40:44 +08:00
parent ff6ae6bb75
commit d7ac5bfa9f
5 changed files with 69 additions and 5 deletions

View File

@@ -34,7 +34,7 @@ void fastweb::subscribe_manager::start()
app()->router->other([&](network::http::request* request, network::http::response* response) {
this->other(request, response);
});
});
}
bool fastweb::subscribe_manager::add(const std::string& pattern, const std::string& filepath)
{

View File

@@ -42,6 +42,11 @@ std::string module::codec::md5(const std::string& value)
return ylib::codec::md5(value);
}
std::string module::codec::sha1(const std::string_view& value)
{
return ylib::codec::sha1(ylib::buffer(value.data(), value.length())).to_hex();
}
void module::codec::regist(sol::state* lua)
{
lua->new_usertype<module::codec>("fw_codec",
@@ -49,6 +54,7 @@ void module::codec::regist(sol::state* lua)
"url_en", &module::codec::url_en,
"to_utf8", &module::codec::to_utf8,
"to_gbk", &module::codec::to_gbk,
"md5", &module::codec::md5
"md5", &module::codec::md5,
"sha1", &module::codec::sha1
);
}

View File

@@ -30,6 +30,8 @@ namespace module
/// <returns></returns>
static std::string md5(const std::string& value);
static std::string sha1(const std::string_view& value);
static void regist(sol::state* lua);
};

View File

@@ -17,7 +17,7 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#include "response.h"
#include "net/http_reqpack.h"
#include "util/codec.h"
module::response::response(network::http::response* response) :m_response(response)
{
@@ -27,6 +27,45 @@ module::response::~response()
{
}
void module::response::set(const std::string& name, const std::string& value)
{
m_sets[name] = value;
}
void module::response::sets(sol::table& lua_table)
{
// Lambda 表达式用于递归解析 Lua 表
auto parse_lua_table = [](const sol::table& table, auto& self) -> std::map<std::string, std::string> {
std::map<std::string, std::string> result;
for (const auto& pair : table) {
auto key = pair.first.as<std::string>();
sol::object value = pair.second;
if (value.get_type() == sol::type::table) {
// 如果是子表,递归解析并将子键展开
auto sub_table = self(value.as<sol::table>(), self);
for (const auto& sub_pair : sub_table) {
result[key + "." + sub_pair.first] = sub_pair.second;
}
}
else if (value.get_type() == sol::type::string) {
// 如果是字符串
result[key] = value.as<std::string>();
}
else if (value.get_type() == sol::type::number) {
// 如果是数字,转换为字符串
result[key] = std::to_string(value.as<double>());
}
else {
std::cerr << "Unsupported Lua type for key: " << key << std::endl;
}
}
return result;
};
m_sets = parse_lua_table(lua_table, parse_lua_table);
}
void module::response::regist(sol::state* lua)
{
// 绑定 Request 类到 Lua
@@ -37,7 +76,9 @@ void module::response::regist(sol::state* lua)
"send_file", &module::response::send_file,
"header", &module::response::header,
"redirect", &module::response::redirect,
"forward", &module::response::forward
"forward", &module::response::forward,
"set", &module::response::set,
"sets", &module::response::sets
);
}
bool module::response::send_data(const char* buf, size_t buf_len, ushort stateNum, const std::string& stateDesc)
@@ -54,7 +95,18 @@ bool module::response::sendex(const std::string& value, ushort stateNum, const s
}
bool module::response::send_file(const std::string& filepath, int32 downbaud, ushort stateNum, const std::string& stateDesc)
{
return m_response->send_file(filepath, downbaud, stateNum, stateDesc);
std::string end_filepath = system::temp_path() + "/" + codec::md5(filepath) + ".tmp";
if (m_sets.size() != 0 && ylib::file::size(filepath) < 1024 * 1024)
{
std::string data = ylib::file::read(filepath);
for_iter(iter, m_sets)
data = strutils::replace(data, "{" + iter->first + "}", iter->second);
ylib::file::write(end_filepath, data);
}
else
end_filepath = filepath;
return m_response->send_file(end_filepath, downbaud, stateNum, stateDesc);
}
bool module::response::redirect(const std::string& filepath, bool MovedPermanently)
{

View File

@@ -18,9 +18,13 @@ namespace module
bool redirect(const std::string& filepath, bool MovedPermanently = false);
bool forward(const std::string& filepath);
void header(const std::string& name, const std::string& value);
void set(const std::string& name,const std::string& value);
void sets(sol::table& lua_table);
static void regist(sol::state* lua);
private:
network::http::response* m_response = nullptr;
std::map<std::string, std::string> m_sets;
};
}