Skip to content

Commit a08ab39

Browse files
Merge pull request #27 from lglglglgy/main
🚀 refined file tree system
2 parents 32b4355 + a70089b commit a08ab39

File tree

10 files changed

+54
-49
lines changed

10 files changed

+54
-49
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
2424
set(CMAKE_CXX_EXTENSIONS OFF)
2525

2626

27-
add_executable(${PROJECT_NAME} main.cc mysql.cc user_controller.cc jwt_controller.cc msg_controller.cc)
27+
add_executable(${PROJECT_NAME} main.cc mysql.cc user_controller.cc jwt_controller.cc msg_controller.cc file_controller.cc)
2828

2929

3030
# 链接MySQL Connector/C++库
@@ -52,7 +52,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE -L${MYSQL_CONNECTOR_CPP_ROOT}/lib6
5252
find_package(OpenSSL REQUIRED)
5353

5454
# 添加 OpenSSL 库到可执行文件链接参数
55-
target_link_libraries(flypen PRIVATE OpenSSL::Crypto)
55+
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto)
5656

5757
#link_directories(/home/lgy/c++kc/mysql-connector/lib64)
5858
#
@@ -95,4 +95,4 @@ target_sources(${PROJECT_NAME}
9595

9696
# ##############################################################################
9797

98-
add_subdirectory(test)
98+
#add_subdirectory(test)

file_controller.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "file_controller.h"
2+
#include <iostream>
3+
#include <stdexcept>
4+
#include <stdio.h>
5+
#include <string>
6+
#include <json/json.h>
7+
8+
9+
std::string shell_commons(const char* cmd) {
10+
char buffer[128];
11+
std::string result = "";
12+
FILE* pipe = popen(cmd, "r");
13+
if (!pipe) throw std::runtime_error("popen() failed!");
14+
try {
15+
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
16+
result += buffer;
17+
}
18+
} catch (...) {
19+
pclose(pipe);
20+
throw;
21+
}
22+
pclose(pipe);
23+
return result;
24+
}
25+
26+
std::string genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
27+
{
28+
char *pathvar;
29+
pathvar = getenv("PWD");
30+
std::string result = shell_commons(("cd "+std::string(pathvar)+"/../root " + "&&"+"tree -J" ).c_str()) ;
31+
std::cout << result << std::endl;
32+
auto res = HttpResponse::newHttpResponse();
33+
res ->setBody(result);
34+
callback(res);
35+
return result;
36+
}

file_controller.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _FILE_CONTROLLER_H_
2+
#define _FILE_CONTROLLER_H_
3+
#include <string>
4+
#include <drogon/drogon.h>
5+
using namespace drogon;
6+
std::string shell_commons(const char* cmd);
7+
std::string genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
8+
//std::string
9+
#endif

main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <drogon/drogon.h>
33
#include "msg_controller.h"
44
#include "user_controller.h"
5+
#include "file_controller.h"
56
#include <drogon/WebSocketController.h>
67
using namespace drogon;
78
std::unordered_map<std::string, WebSocketConnectionPtr> clientTable;
@@ -29,8 +30,13 @@ int main() {
2930
drogon::app().registerHandler("/api/info", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
3031
info(req, std::move(callback));
3132
});
33+
34+
drogon::app().registerHandler("/api/file/tree", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
35+
genTree(req, std::move(callback));
36+
});
3237
drogon::app().registerHandler("/api/avatar", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
3338
avatar(req, std::move(callback));
39+
3440
});
3541
drogon::app().run();
3642
return 0;

root/root文件1

Whitespace-only changes.

root/root文件2

Whitespace-only changes.

root/root里面的文件夹/root里面文件夹里面的文件夹/root里面的文件夹里面的文件夹里面的文件

Whitespace-only changes.

root/root里面的文件夹/root里面的文件夹里面的文件

Whitespace-only changes.

test/CMakeLists.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/test_main.cc

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)