Skip to content

Commit a12f22e

Browse files
Merge pull request #49 from 0WAQ/main
formatting
2 parents e618afe + 94bb40b commit a12f22e

File tree

10 files changed

+121
-108
lines changed

10 files changed

+121
-108
lines changed

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"session_timeout": 0,
106106
//string value of SameSite attribute of the Set-Cookie HTTP respone header
107107
//valid value is either 'Null' (default), 'Lax', 'Strict' or 'None'
108-
"session_same_site" : "Null",
108+
"session_same_site": "Null",
109109
//document_root: Root path of HTTP document, defaut path is ./
110110
"document_root": "./",
111111
//home_page: Set the HTML file of the home page, the default value is "index.html"
@@ -317,4 +317,4 @@
317317
],
318318
//custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method.
319319
"custom_config": {}
320-
}
320+
}

file_controller.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include <iostream>
88
#include <stdexcept>
99
#include <string>
10-
std:: string return_status(std::string result,std::string command )
10+
std::string return_status(std::string result, std::string command)
1111
{
1212
if (result != "")
1313
result = "success";
1414
else
15-
result = " error in :"+command;
15+
result = " error in :" + command;
1616
return result;
1717
}
1818
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
@@ -83,12 +83,12 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
8383
break;
8484
case cp:
8585
result = shell_commands(("cp -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
86-
result = return_status(result,"cp");
86+
result = return_status(result, "cp");
8787

8888
break;
8989
case mv:
9090
result = shell_commands(("mv -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
91-
result = return_status(result,"mv");
91+
result = return_status(result, "mv");
9292
break;
9393
case rm:
9494
if (params1.find("..") != std::string::npos)
@@ -97,11 +97,11 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
9797
break;
9898
}
9999
result = shell_commands(("rm -rf -v " + std::string(pathvar) + "/../root/" + params1).c_str());
100-
result = return_status(result,"rm");
100+
result = return_status(result, "rm");
101101
break;
102102
case mkdir:
103103
result = shell_commands(("mkdir -v " + std::string(pathvar) + "/../root/" + params1).c_str());
104-
result = return_status(result,"mkdir");
104+
result = return_status(result, "mkdir");
105105
break;
106106
case touch:
107107
if ("" == shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()))

jwt_controller.cc

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,56 @@
88

99
using namespace jwt;
1010

11-
std::string jwtGen(const Json::Value& req_json)
11+
std::string jwtGen(const Json::Value &req_json)
1212
{
1313
auto now = std::chrono::system_clock::now();
1414
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
1515
std::string timestamp = std::to_string(ms);
1616
auto token = jwt::create()
17-
.set_type("JWS")
18-
.set_payload_claim("name", jwt::claim(req_json["username"].asString()))
19-
.set_payload_claim("time", jwt::claim(timestamp))
20-
.sign(jwt::algorithm::hs256{"secret"});
17+
.set_type("JWS")
18+
.set_payload_claim("name", jwt::claim(req_json["username"].asString()))
19+
.set_payload_claim("time", jwt::claim(timestamp))
20+
.sign(jwt::algorithm::hs256{"secret"});
2121
return std::string(token);
2222
}
2323

24-
std::string jwtDecrypt(const std::string& token)
24+
std::string jwtDecrypt(const std::string &token)
2525
{
26-
try {
26+
try
27+
{
2728
auto decoded_token = jwt::decode(token);
2829
auto verifier = jwt::verify()
29-
.allow_algorithm(jwt::algorithm::hs256{"secret"})
30-
.with_type("JWS");
30+
.allow_algorithm(jwt::algorithm::hs256{"secret"})
31+
.with_type("JWS");
3132
verifier.verify(decoded_token);
3233
return decoded_token.get_payload_claim("name").as_string();
33-
} catch (const std::exception& e) {
34-
std::cout<<"Failed to decrypt JWT: " + std::string(e.what())<<std::endl;
34+
}
35+
catch (const std::exception &e)
36+
{
37+
std::cout << "Failed to decrypt JWT: " + std::string(e.what()) << std::endl;
3538
throw std::runtime_error("Failed to decrypt JWT");
3639
}
3740
}
3841

39-
bool jwtVerify(const drogon::HttpRequestPtr &req){
42+
bool jwtVerify(const drogon::HttpRequestPtr &req)
43+
{
4044
std::string authHeader = req->getHeader("Authorization");
41-
if (authHeader.substr(0, 7) == "Bearer ") {
45+
if (authHeader.substr(0, 7) == "Bearer ")
46+
{
4247
std::string bearerToken = authHeader.substr(7);
43-
try {
48+
try
49+
{
4450
std::string sender = jwtDecrypt(bearerToken);
4551
return true;
46-
} catch (const std::exception &e) {
52+
}
53+
catch (const std::exception &e)
54+
{
4755
std::cout << "Wrong token" << std::endl;
4856
return false;
4957
}
50-
} else {
58+
}
59+
else
60+
{
5161
std::cout << "No Authorization" << std::endl;
5262
return false;
5363
}

jwt_controller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <json/json.h>
66
#include <cstring>
77

8-
std::string jwtGen(const Json::Value& req_json);
9-
std::string jwtDecrypt(const std::string& token);
8+
std::string jwtGen(const Json::Value &req_json);
9+
std::string jwtDecrypt(const std::string &token);
1010
bool jwtVerify(const drogon::HttpRequestPtr &req);
1111

1212
#endif

main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main()
3737
drogon::app().registerHandler("/api/file/commands", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
3838
{ commandsCtrl(req, std::move(callback)); });
3939
drogon::app().registerHandler("/api/file/lock", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
40-
{add_lock(req, std::move(callback));});
40+
{ add_lock(req, std::move(callback)); });
4141
drogon::app().setUploadPath("./uploads").run();
4242
return 0;
4343
}

models/model.json

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,28 @@
2323
//convert: the value can be changed by a function call before it is stored into database or
2424
//after it is read from database
2525
"convert": {
26-
"enabled": false,
27-
"items":[{
28-
"table": "user",
29-
"column": "password",
30-
"method": {
31-
//after_db_read: name of the method which is called after reading from database, signature: void([const] std::shared_ptr [&])
32-
"after_db_read": "decrypt_password",
33-
//before_db_write: name of the method which is called before writing to database, signature: void([const] std::shared_ptr [&])
34-
"before_db_write": "encrypt_password"
35-
},
36-
"includes": [
37-
"\"file_local_search_path.h\"","<file_in_global_search_path.h>"
38-
]
39-
}]
26+
"enabled": false,
27+
"items": [
28+
{
29+
"table": "user",
30+
"column": "password",
31+
"method": {
32+
//after_db_read: name of the method which is called after reading from database, signature: void([const] std::shared_ptr [&])
33+
"after_db_read": "decrypt_password",
34+
//before_db_write: name of the method which is called before writing to database, signature: void([const] std::shared_ptr [&])
35+
"before_db_write": "encrypt_password"
36+
},
37+
"includes": [
38+
"\"file_local_search_path.h\"",
39+
"<file_in_global_search_path.h>"
40+
]
41+
}
42+
]
4043
},
4144
"relationships": {
4245
"enabled": false,
43-
"items": [{
46+
"items": [
47+
{
4448
"type": "has one",
4549
"original_table_name": "products",
4650
"original_table_alias": "product",
@@ -101,4 +105,4 @@
101105
// generate_base_only: false by default. Set to true to avoid overwriting custom subclasses.
102106
"generate_base_only": false
103107
}
104-
}
108+
}

mysql.cc

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -315,68 +315,67 @@ Json::Value get_my_info(std::string me)
315315
sql::Connection *con;
316316
con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
317317
con->setSchema("flypen");
318-
if (!me.empty())
319-
{
320-
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
321-
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
322-
prepStmt->setString(1, me);
323-
324-
sql::ResultSet *res = prepStmt->executeQuery();
318+
if (!me.empty())
319+
{
320+
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
321+
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
322+
prepStmt->setString(1, me);
325323

326-
if (res->next())
327-
{
328-
Json::Value user;
329-
int avatar = res->getInt("avatar");
330-
std::string friends = res->getString("friends");
331-
std::string req = res->getString("req");
324+
sql::ResultSet *res = prepStmt->executeQuery();
332325

333-
// 使用lambda函数来查询用户信息
334-
auto fetchUserInfo = [&](const std::string &token) -> Json::Value
335-
{
336-
Json::Value info;
337-
info["username"] = token;
338-
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
339-
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
340-
prepStmt->setString(1, token);
341-
sql::ResultSet *res = prepStmt->executeQuery();
342-
if (res->next())
343-
{
344-
info["avatar"] = res->getInt("avatar");
345-
}
346-
return info;
347-
};
348-
349-
Json::Value friends_array(Json::arrayValue);
350-
Json::Value req_array(Json::arrayValue);
351-
std::stringstream sf(friends);
352-
std::stringstream sr(req);
353-
std::string token;
354-
355-
// 处理好友列表
356-
while (std::getline(sf, token, ','))
357-
{
358-
Json::Value afriend = fetchUserInfo(token);
359-
friends_array.append(afriend);
360-
}
326+
if (res->next())
327+
{
328+
Json::Value user;
329+
int avatar = res->getInt("avatar");
330+
std::string friends = res->getString("friends");
331+
std::string req = res->getString("req");
361332

362-
// 处理请求列表
363-
while (std::getline(sr, token, ','))
333+
// 使用lambda函数来查询用户信息
334+
auto fetchUserInfo = [&](const std::string &token) -> Json::Value
335+
{
336+
Json::Value info;
337+
info["username"] = token;
338+
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
339+
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
340+
prepStmt->setString(1, token);
341+
sql::ResultSet *res = prepStmt->executeQuery();
342+
if (res->next())
364343
{
365-
Json::Value areq = fetchUserInfo(token);
366-
req_array.append(areq);
344+
info["avatar"] = res->getInt("avatar");
367345
}
346+
return info;
347+
};
368348

369-
user["avatar"] = avatar;
370-
user["friends"] = friends_array;
371-
user["req"] = req_array;
349+
Json::Value friends_array(Json::arrayValue);
350+
Json::Value req_array(Json::arrayValue);
351+
std::stringstream sf(friends);
352+
std::stringstream sr(req);
353+
std::string token;
372354

373-
Json::StreamWriterBuilder builder;
374-
std::string userJson = Json::writeString(builder, user);
355+
// 处理好友列表
356+
while (std::getline(sf, token, ','))
357+
{
358+
Json::Value afriend = fetchUserInfo(token);
359+
friends_array.append(afriend);
360+
}
375361

376-
json[me] = user;
362+
// 处理请求列表
363+
while (std::getline(sr, token, ','))
364+
{
365+
Json::Value areq = fetchUserInfo(token);
366+
req_array.append(areq);
377367
}
368+
369+
user["avatar"] = avatar;
370+
user["friends"] = friends_array;
371+
user["req"] = req_array;
372+
373+
Json::StreamWriterBuilder builder;
374+
std::string userJson = Json::writeString(builder, user);
375+
376+
json[me] = user;
378377
}
379-
378+
}
380379
}
381380
catch (sql::SQLException &e)
382381
{
@@ -431,7 +430,7 @@ bool sql_check(std::string user, std::string passwd)
431430
Json::Value sql_find_my_msg(std::string me, std::string connect_type)
432431

433432
{
434-
//std::cout << "login user: " << me << std::endl;
433+
// std::cout << "login user: " << me << std::endl;
435434
try
436435
{
437436
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
@@ -474,7 +473,7 @@ Json::Value sql_find_my_msg(std::string me, std::string connect_type)
474473

475474
while (res->next())
476475
{
477-
//update isread to 1
476+
// update isread to 1
478477
if (connect_type == "new")
479478
{
480479
id = res->getInt("id");

mysql.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
#ifndef _MYSQL_H_
1+
#ifndef _MYSQL_H_
22
#define _MYSQL_H_
33
#include <jdbc/mysql_driver.h>
44
#include <jdbc/mysql_connection.h>
55
#include <json/json.h>
66

77
void sql_unlocked(std::string DeleteName);
88
void sql_add(std::string username, std::string passwd, int avatar);
9-
bool sql_check(std::string , std::string passwd="@DEFAULT@");
10-
void sql_addhistory(std::string,std::string,std::string,std::string);
9+
bool sql_check(std::string, std::string passwd = "@DEFAULT@");
10+
void sql_addhistory(std::string, std::string, std::string, std::string);
1111
void sql_addconnect(std::string connectptr);
1212

13-
void sql_addrequest(std::string send,std::string receiver);
14-
void sql_process_request(std::string ,std::string,std::string);
15-
Json::Value sql_find_my_msg(std::string,std::string);
13+
void sql_addrequest(std::string send, std::string receiver);
14+
void sql_process_request(std::string, std::string, std::string);
15+
Json::Value sql_find_my_msg(std::string, std::string);
1616
int lockcheck(std::string filename);
1717
Json::Value get_my_info(std::string);
18-
void sql_delete_operation(std::string,std::string);
18+
void sql_delete_operation(std::string, std::string);
1919
void set_avatar(std::string person, int avatar);
2020
int sql_findexist(std::string receiver);
2121
#endif

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

user_controller.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include <drogon/drogon.h>
55
using namespace drogon;
66

7-
typedef void (*HandlerFunc)(const Json::Value&, std::string* str, int*);
7+
typedef void (*HandlerFunc)(const Json::Value &, std::string *str, int *);
88

99
void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, HandlerFunc handler);
10-
void registerUser(const Json::Value& req_json, std::string* msg, int* code);
11-
void loginUser(const Json::Value& req_json, std::string* msg, int* code);
10+
void registerUser(const Json::Value &req_json, std::string *msg, int *code);
11+
void loginUser(const Json::Value &req_json, std::string *msg, int *code);
1212
void avatar(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
1313
#endif

0 commit comments

Comments
 (0)