diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d9d45a..0942e78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,9 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) message(STATUS "CXX Standard: ${CMAKE_CXX_STANDARD}") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++20") SET(ENABLE_MYSQL ON) + SET(ENABLE_PG OFF) SET(ENABLE_SQLITE3 OFF) @@ -82,8 +84,8 @@ if (ENABLE_MYSQL) endif() if (ENABLE_SQLITE3) - target_link_libraries(${PROJECT_NAME} sqlite3) - target_include_directories(${PROJECT_NAME} PRIVATE ormpp) + target_link_libraries(feather sqlite3 -ldl) + target_include_directories(feather PRIVATE ormpp) endif() if (ENABLE_PG) diff --git a/cinatra b/cinatra index 018a180..3fa0f9c 160000 --- a/cinatra +++ b/cinatra @@ -1 +1 @@ -Subproject commit 018a18056db4bc0c0e0c28feac59db650a484882 +Subproject commit 3fa0f9cc5433b22b002ca6babb5313dd125d2a6e diff --git a/feather.h b/feather.h index e655be6..4c41fc1 100644 --- a/feather.h +++ b/feather.h @@ -10,6 +10,14 @@ #include "iguana/json_writer.hpp" #include "ormpp/connection_pool.hpp" #include "ormpp/dbng.hpp" +#ifdef ORMPP_ENABLE_MYSQL #include "ormpp/mysql.hpp" +#endif +#ifdef ORMPP_ENABLE_SQLITE3 +#include "ormpp/sqlite.hpp" +#endif +#ifdef ORMPP_ENABLE_PG +#include "ormpp/postgresql.hpp" +#endif #include "util.hpp" #endif // FEATHER_FEATHER_H diff --git a/main.cpp b/main.cpp index 38961da..132abc1 100644 --- a/main.cpp +++ b/main.cpp @@ -21,9 +21,18 @@ void init(const feather_cfg& cfg) { nanolog::Logger::initialize(nanolog::GuaranteedLogger(), cfg.log_path, cfg.log_name, cfg.roll_file_size); +#ifdef ORMPP_ENABLE_MYSQL dao_t>::init(cfg.db_conn_num, cfg.db_ip.data(), cfg.user_name.data(), cfg.pwd.data(), cfg.db_name.data(), cfg.timeout); +#endif +#ifdef ORMPP_ENABLE_SQLITE3 +#endif +#ifdef ORMPP_ENABLE_PG + dao_t>::init(cfg.db_conn_num, cfg.db_ip.data(), + cfg.user_name.data(), cfg.pwd.data(), + cfg.db_name.data(), cfg.timeout); +#endif Dao dao; ormpp_auto_key key{SID(cncppcon2018_user::id)}; @@ -65,111 +74,95 @@ int main() { purecpp_controller purecpp_ctl; server.set_http_handler( - "/login_page", &purecpp_controller::login_page, &purecpp_ctl); + "/login_page", &purecpp_controller::login_page, purecpp_ctl); server.set_http_handler("/login", &purecpp_controller::login, - &purecpp_ctl, - {std::make_shared()}); + purecpp_ctl, check_login_input{}); server.set_http_handler( - "/sign_up_page", &purecpp_controller::sign_up_page, &purecpp_ctl); + "/sign_up_page", &purecpp_controller::sign_up_page, purecpp_ctl); + server.set_http_handler("/sign_up", &purecpp_controller::sign_up, + purecpp_ctl, check_sign_out_input{}); + server.set_http_handler("/member_edit_page", + &purecpp_controller::member_edit_page, + purecpp_ctl, check_login{}); server.set_http_handler( - "/sign_up", &purecpp_controller::sign_up, &purecpp_ctl, - {std::make_shared()}); - server.set_http_handler( - "/member_edit_page", &purecpp_controller::member_edit_page, &purecpp_ctl, - {std::make_shared()}); - server.set_http_handler( - "/member_edit", &purecpp_controller::member_edit, &purecpp_ctl, - {std::make_shared(), - std::make_shared()}); + "/member_edit", &purecpp_controller::member_edit, purecpp_ctl, + check_login{}, check_member_edit_input{}); server.set_http_handler("/quit", &purecpp_controller::quit, - &purecpp_ctl, - {std::make_shared()}); + purecpp_ctl, check_login{}); server.set_http_handler( - "/course_page", &purecpp_controller::course_page, &purecpp_ctl); - server.set_http_handler( - "/book_course", &purecpp_controller::book_course, &purecpp_ctl, - {std::make_shared()}); + "/course_page", &purecpp_controller::course_page, purecpp_ctl); + server.set_http_handler("/book_course", + &purecpp_controller::book_course, + purecpp_ctl, check_join_cncppcon2018{}); server.set_http_handler("/cncppcon_page2019", &purecpp_controller::cncppcon_page2019, - &purecpp_ctl); + purecpp_ctl); server.set_http_handler("/cncppcon_page2019/dash", &purecpp_controller::cncppcon_page2019, - &purecpp_ctl); + purecpp_ctl); server.set_http_handler("/cncppcon_page2019/apply/lecturer", &purecpp_controller::cncppcon_page2019, - &purecpp_ctl); + purecpp_ctl); server.set_http_handler( - "/api/enrol/speaker", &purecpp_controller::enrol_speakers, &purecpp_ctl); + "/api/enrol/speaker", &purecpp_controller::enrol_speakers, purecpp_ctl); server.set_http_handler( "/cncppcon_query_page2018", &purecpp_controller::cncppcon_query_page2018, - &purecpp_ctl); - server.set_http_handler( - "/join_cncppcon2018", &purecpp_controller::join_cncppcon2018, - &purecpp_ctl, {std::make_shared()}); - server.set_http_handler( - "/query_cncppcon2018", &purecpp_controller::query_cncppcon2018, - &purecpp_ctl, {std::make_shared()}); + purecpp_ctl); + server.set_http_handler("/join_cncppcon2018", + &purecpp_controller::join_cncppcon2018, + purecpp_ctl, check_join_cncppcon2018{}); + server.set_http_handler("/query_cncppcon2018", + &purecpp_controller::query_cncppcon2018, + purecpp_ctl, check_query_cncppcon2018{}); server.set_http_handler("/register_user_group", &purecpp_controller::register_user_group, - &purecpp_ctl); - server.set_http_handler( - "/join_user_group", &purecpp_controller::join_user_group, &purecpp_ctl, - {std::make_shared()}); - - server.set_http_handler("/", &purecpp_controller::home, &purecpp_ctl, - {std::make_shared()}); - server.set_http_handler("/home", &purecpp_controller::home, &purecpp_ctl, - {std::make_shared()}); + purecpp_ctl); + server.set_http_handler("/join_user_group", + &purecpp_controller::join_user_group, + purecpp_ctl, check_join_cncppcon2018{}); + + server.set_http_handler("/", &purecpp_controller::home, purecpp_ctl, + check_start_end_input{}); + server.set_http_handler("/home", &purecpp_controller::home, purecpp_ctl, + check_start_end_input{}); server.set_http_handler("/detail", &purecpp_controller::detail, - &purecpp_ctl, - {std::make_shared()}); + purecpp_ctl, check_detail_input{}); server.set_http_handler("/category", &purecpp_controller::category, - &purecpp_ctl, - {std::make_shared()}); + purecpp_ctl, check_category_input{}); server.set_http_handler("/search", &purecpp_controller::search, - &purecpp_ctl, - {std::make_shared()}); + purecpp_ctl, check_search_input{}); server.set_http_handler("/comment", &purecpp_controller::comment, - &purecpp_ctl, - {std::make_shared(), - std::make_shared()}); + purecpp_ctl, check_login{}, + check_comment_input{}); server.set_http_handler( - "/remove_comment", &purecpp_controller::remove_comment, &purecpp_ctl, - {std::make_shared(), - std::make_shared()}); + "/remove_comment", &purecpp_controller::remove_comment, purecpp_ctl, + check_login{}, check_remove_comment_input{}); - server.set_http_handler( - "/add_post_page", &purecpp_controller::add_post_page, &purecpp_ctl, - {std::make_shared()}); + server.set_http_handler("/add_post_page", + &purecpp_controller::add_post_page, + purecpp_ctl, check_login{}); server.set_http_handler("/add_post", &purecpp_controller::add_post, - &purecpp_ctl, - {std::make_shared()}); + purecpp_ctl, check_login{}); server.set_http_handler( - "/remove_post", &purecpp_controller::remove_post, &purecpp_ctl, - {std::make_shared(), - std::make_shared()}); + "/remove_post", &purecpp_controller::remove_post, purecpp_ctl, + check_login{}, check_edit_post_input{}); server.set_http_handler( - "/edit_post_page", &purecpp_controller::edit_post_page, &purecpp_ctl, - {std::make_shared(), - std::make_shared()}); + "/edit_post_page", &purecpp_controller::edit_post_page, purecpp_ctl, + check_login{}, check_edit_post_input{}); server.set_http_handler( - "/edit_post", &purecpp_controller::edit_post, &purecpp_ctl, - {std::make_shared()}); + "/edit_post", &purecpp_controller::edit_post, purecpp_ctl, check_login{}); server.set_http_handler( - "/pass_post", &purecpp_controller::pass_post, &purecpp_ctl, - {std::make_shared(), - std::make_shared()}); + "/pass_post", &purecpp_controller::pass_post, purecpp_ctl, check_login{}, + check_edit_post_input{}); + server.set_http_handler("/my_post", &purecpp_controller::my_post, + purecpp_ctl, check_login{}, + check_start_end_input{}); server.set_http_handler( - "/my_post", &purecpp_controller::my_post, &purecpp_ctl, - {std::make_shared(), - std::make_shared()}); - server.set_http_handler("/upload_file", - &purecpp_controller::upload, &purecpp_ctl, - {std::make_shared()}); + "/upload_file", &purecpp_controller::upload, purecpp_ctl, check_login{}); server.sync_start(); diff --git a/ormpp b/ormpp index 083123b..00c1082 160000 --- a/ormpp +++ b/ormpp @@ -1 +1 @@ -Subproject commit 083123b5cfe8498d8de9be2f319cb38fbce7a1cd +Subproject commit 00c10827d050b6f039d9c1ecb427bf9fb7d6b5b1 diff --git a/purecpp_controller.hpp b/purecpp_controller.hpp index 0f2b2d9..01516d8 100644 --- a/purecpp_controller.hpp +++ b/purecpp_controller.hpp @@ -15,7 +15,15 @@ using namespace ormpp; using namespace cinatra; namespace feather { +#ifdef ORMPP_ENABLE_MYSQL using Dao = dao_t>; +#endif +#ifdef ORMPP_ENABLE_SQLITE3 +using Dao = dao_t>; +#endif +#ifdef ORMPP_ENABLE_PG +using Dao = dao_t>; +#endif class purecpp_controller { public: purecpp_controller() { diff --git a/validate.hpp b/validate.hpp index d1f66ef..83ce21e 100644 --- a/validate.hpp +++ b/validate.hpp @@ -30,7 +30,7 @@ bool len_more_than(T... args) { return r; } -struct check_login : public base_aspect { +struct check_login { bool before(request& req, response& res) { auto v = get_user_info(req); if (v.empty() || v[0].empty()) { @@ -42,7 +42,7 @@ struct check_login : public base_aspect { } }; -struct check_comment_input : public base_aspect { +struct check_comment_input { bool before(request& req, response& res) { auto post_id = req.get_query_value("post_id"); if (!is_integer(post_id)) { @@ -84,13 +84,13 @@ inline bool check_integers(request& req, response& res, T... key) { return r; } -struct check_remove_comment_input : public base_aspect { +struct check_remove_comment_input { bool before(request& req, response& res) { return check_integers(req, res, "id", "post_id"); } }; -struct check_detail_input : public base_aspect { +struct check_detail_input { bool before(request& req, response& res) { return check_integer(req, res, "id"); } @@ -112,7 +112,7 @@ inline bool set_start_end(request& req, response& res, std::string& s, return true; } -struct check_category_input : public base_aspect { +struct check_category_input { bool before(request& req, response& res) { std::string s = "0"; std::string lens = "10"; @@ -130,7 +130,7 @@ struct check_category_input : public base_aspect { } }; -struct check_search_input : public base_aspect { +struct check_search_input { bool before(request& req, response& res) { std::string s = "0"; std::string lens = "10"; @@ -160,7 +160,7 @@ struct check_search_input : public base_aspect { } }; -struct check_start_end_input : public base_aspect { +struct check_start_end_input { bool before(request& req, response& res) { std::string s = "0"; std::string lens = "10"; @@ -196,7 +196,7 @@ inline bool check_input(response& res, T... args) { return r; } -struct check_login_input : public base_aspect { +struct check_login_input { bool before(request& req, response& res) { auto user_name = req.get_query_value("user_name"); auto password = req.get_query_value("password"); @@ -215,7 +215,7 @@ struct check_login_input : public base_aspect { } }; -struct check_sign_out_input : public base_aspect { +struct check_sign_out_input { bool before(request& req, response& res) { auto user_name = req.get_query_value("user_name"); auto email = req.get_query_value("email"); @@ -247,7 +247,7 @@ struct check_sign_out_input : public base_aspect { } }; -struct check_book_course : public base_aspect { +struct check_book_course { bool before(request& req, response& res) { // std::string result; // for (size_t i = 0; i < req.get_form_url_map().size(); i++) { @@ -270,7 +270,7 @@ struct check_book_course : public base_aspect { } }; -struct check_join_cncppcon2018 : public base_aspect { +struct check_join_cncppcon2018 { bool before(request& req, response& res) { auto user_name = req.get_query_value("user_name"); auto email = req.get_query_value("user_email"); @@ -310,7 +310,7 @@ struct check_join_cncppcon2018 : public base_aspect { } }; -struct check_query_cncppcon2018 : public base_aspect { +struct check_query_cncppcon2018 { bool before(request& req, response& res) { auto phone = req.get_query_value("user_phone"); auto answer = req.get_query_value("user_answer"); @@ -337,7 +337,7 @@ struct check_query_cncppcon2018 : public base_aspect { } }; -struct check_member_edit_input : public base_aspect { +struct check_member_edit_input { bool before(request& req, response& res) { auto old_password = req.get_query_value("old_password"); auto new_pwd = req.get_query_value("new_password"); @@ -361,7 +361,7 @@ struct check_member_edit_input : public base_aspect { } }; -struct check_edit_post_input : public base_aspect { +struct check_edit_post_input { bool before(request& req, response& res) { if (!check_integer(req, res, "id")) { return false;