From 8bd3c03468f45d7157bd3312651b1fcedda874b6 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 21 Nov 2021 15:18:42 +0100 Subject: [PATCH] Blog list. --- app/wp_application.cpp | 47 +++++++++++++++++++++++++++++++++++------- app/wp_application.h | 18 +++++++++++++--- main.cpp | 29 +++++++++++++++++++------- 3 files changed, 76 insertions(+), 18 deletions(-) diff --git a/app/wp_application.cpp b/app/wp_application.cpp index 5435f6b..c079ac7 100644 --- a/app/wp_application.cpp +++ b/app/wp_application.cpp @@ -14,13 +14,38 @@ #include "core/http/http_session.h" #include "core/http/session_manager.h" -void WPApplication::index(Object *instance, Request *request) { +void WPApplication::index_fun(Object *instance, Request *request) { + WPApplication *app = Object::cast_to(instance); - request->body += "test"; + app->index(request); +} + +void WPApplication::blog_fun(Object *instance, Request *request) { + WPApplication *app = Object::cast_to(instance); + + app->blog(request); +} + +void WPApplication::index(Request *request) { + HTMLBuilder b; + + b.div("content"); + + b.div("content_head")->f()->w("Saved blogs:")->cdiv(); + + for (int i = 0; i < _blog_data.size(); ++i) { + BlogData &bd = _blog_data[i]; + + b.div("content_row")->f()->fa("/blog/" + bd.name + "/", bd.name, "blog_link")->cdiv(); + } + + b.cdiv(); + + request->body += b.result; request->compile_and_send_body(); } -void WPApplication::blog(Object *instance, Request *request) { +void WPApplication::blog(Request *request) { request->body += "test blog"; request->compile_and_send_body(); } @@ -43,8 +68,6 @@ void WPApplication::routing_middleware(Object *instance, Request *request) { request->handler_instance = app->index_func; } else { - - const String main_route = request->get_current_path_segment(); request->push_path(); @@ -67,8 +90,8 @@ void WPApplication::routing_middleware(Object *instance, Request *request) { void WPApplication::setup_routes() { DWebApplication::setup_routes(); - index_func = HandlerInstance(index, this); - blog_func = HandlerInstance(blog, this); + index_func = HandlerInstance(index_fun, this); + blog_func = HandlerInstance(blog_fun, this); } void WPApplication::setup_middleware() { @@ -78,6 +101,15 @@ void WPApplication::setup_middleware() { void WPApplication::migrate() { } +void WPApplication::add_blog(const String &name, Database *db) { + BlogData bd; + + bd.name = name; + bd.db = db; + + _blog_data.push_back(bd); +} + void WPApplication::compile_menu() { HTMLBuilder bh; @@ -108,4 +140,3 @@ WPApplication::WPApplication() : WPApplication::~WPApplication() { } - diff --git a/app/wp_application.h b/app/wp_application.h index cb0b9d8..3d6e47b 100644 --- a/app/wp_application.h +++ b/app/wp_application.h @@ -3,8 +3,8 @@ //#include "core/http/web_application.h" #include "core/object.h" -#include "modules/drogon/web_application.h" #include "core/string.h" +#include "modules/drogon/web_application.h" #undef LOG_TRACE #undef LOG_WARN @@ -13,8 +13,11 @@ class WPApplication : public DWebApplication { RCPP_OBJECT(WPApplication, DWebApplication); public: - static void index(Object *instance, Request *request); - static void blog(Object *instance, Request *request); + static void index_fun(Object *instance, Request *request); + static void blog_fun(Object *instance, Request *request); + + void index(Request *request); + void blog(Request *request); static void routing_middleware(Object *instance, Request *request); @@ -23,6 +26,8 @@ public: virtual void migrate(); + void add_blog(const String &name, Database *db); + void compile_menu(); WPApplication(); @@ -32,6 +37,13 @@ public: String header; String footer; + + struct BlogData { + String name; + Database *db; + }; + + Vector _blog_data; }; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index b12e7ac..69fedcf 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,9 @@ #include -#include "core/settings.h" -#include "core/os/platform.h" #include "core/file_cache.h" #include "core/math/math.h" +#include "core/os/platform.h" +#include "core/settings.h" #include "database/db_init.h" @@ -15,8 +15,8 @@ #include "core/settings.h" -#include "app/wp_downloader.h" #include "app/wp_application.h" +#include "app/wp_downloader.h" void initialize_backends() { initialize_database_backends(); @@ -33,11 +33,12 @@ int main(int argc, char **argv, char **envp) { Settings *settings = new Settings(true); settings->parse_ini_file("settings.ini"); + DatabaseManager *dbm = new DatabaseManager(); + bool download = Platform::get_singleton()->arg_parser.has_arg("-d"); if (download) { Vector > downloaders; - DatabaseManager *dbm = new DatabaseManager(); bool save_original_data = settings->get_value_bool("save_original_data"); @@ -57,14 +58,13 @@ int main(int argc, char **argv, char **envp) { d->setup(s, db); - //todo - //downloaders.push_back(d); + // todo + // downloaders.push_back(d); d->run(); } } - delete dbm; } else { FileCache *file_cache = new FileCache(true); file_cache->wwwroot = "./www"; @@ -75,6 +75,20 @@ int main(int argc, char **argv, char **envp) { app->setup_routes(); app->setup_middleware(); app->add_listener("127.0.0.1", 8080); + + String sites = settings->get_value("sites"); + + int sc = sites.get_slice_count(','); + for (int i = 0; i < sc; ++i) { + String s = sites.get_slice(',', i); + + uint32_t index = dbm->create_database("sqlite"); + Database *db = dbm->databases[index]; + db->connect(database_folder + s + ".sqlite"); + + app->add_blog(s, db); + } + LOG_INFO << "Server running on 127.0.0.1:8080"; printf("Initialized!\n"); app->run(); @@ -83,6 +97,7 @@ int main(int argc, char **argv, char **envp) { delete file_cache; } + delete dbm; delete settings; PlatformInitializer::free_all();