增加了数据库下载的功能
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#include <string>
|
||||
#include "crow.h"
|
||||
#include <iostream>
|
||||
#include "json/json.h"
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include "../pPowerManger/SQLiteDB.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 读取 HTML 文件内容
|
||||
string read_file(const string& path) {
|
||||
ifstream file(path);
|
||||
stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
return buffer.str();
|
||||
}
|
||||
|
||||
int main() {
|
||||
crow::SimpleApp app;
|
||||
SQLiteDB db("/home/zjk/project/H100/H100PowerManger/data/power_data.db");
|
||||
// 初始化 SQLite 数据库
|
||||
|
||||
// 路由返回 HTML 页面
|
||||
CROW_ROUTE(app, "/")([](){
|
||||
// 读取 HTML 文件
|
||||
string html_content = read_file("/home/zjk/project/H100/H100PowerManger/src/appPowerMangerWebUI/index.html");
|
||||
return html_content;
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/db-status")
|
||||
([&db]() {
|
||||
crow::json::wvalue response;
|
||||
if (db.isDatabaseConnected("/home/zjk/project/H100/H100PowerManger/data", "power_data.db")) {
|
||||
response["connected"] = true;
|
||||
response["database"] = "YourDatabaseName"; // 替换为实际的数据库名称
|
||||
} else {
|
||||
response["connected"] = false;
|
||||
}
|
||||
return response;
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/download-db")
|
||||
([&db]() {
|
||||
std::ifstream file("/home/zjk/project/H100/H100PowerManger/data/power_data.db", std::ios::binary);
|
||||
if (!file) {
|
||||
return crow::response(404, "Database file not found");
|
||||
}
|
||||
std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
// 将 buffer.size() 转换为 std::string
|
||||
std::string contentLength = std::to_string(buffer.size());
|
||||
crow::response response(buffer.size(), contentLength, "application/octet-stream");
|
||||
response.add_header("Content-Disposition", "attachment; filename=power_data.db");
|
||||
return response;
|
||||
});
|
||||
|
||||
// 启动服务器,监听端口 18080
|
||||
app.port(18080).multithreaded().run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user