From a6193462d4b7a04b8896e52a26521f9b1a0f87a1 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 20 Sep 2022 14:26:55 +0200 Subject: [PATCH] added many models and continued drafting --- app/server/Controller/ApiController.h | 0 app/server/Controller/InstallController.h | 0 app/server/Models/Account.h | 59 ++++++++++ app/server/Models/AccountStatus.h | 20 ++++ app/server/Models/Db.h | 24 +++-- app/server/Models/InstallType.h | 20 ++++ app/server/Models/Organization.h | 30 ++++++ app/server/Models/ReosurceStatus.h | 20 ++++ app/server/Models/Resource.h | 88 +++++++++++++++ app/server/Models/ResourceInfo.h | 47 ++++++++ app/server/build/install.sh | 1 + app/server/install/db.sqlite | Bin 0 -> 45056 bytes app/server/main.cpp | 125 +++++++++++++++++----- app/web/Install/db.json | 20 ++-- 14 files changed, 408 insertions(+), 46 deletions(-) create mode 100644 app/server/Controller/ApiController.h create mode 100644 app/server/Controller/InstallController.h create mode 100644 app/server/Models/Account.h create mode 100644 app/server/Models/AccountStatus.h create mode 100644 app/server/Models/InstallType.h create mode 100644 app/server/Models/Organization.h create mode 100644 app/server/Models/ReosurceStatus.h create mode 100644 app/server/Models/Resource.h create mode 100644 app/server/Models/ResourceInfo.h create mode 100644 app/server/install/db.sqlite diff --git a/app/server/Controller/ApiController.h b/app/server/Controller/ApiController.h new file mode 100644 index 0000000..e69de29 diff --git a/app/server/Controller/InstallController.h b/app/server/Controller/InstallController.h new file mode 100644 index 0000000..e69de29 diff --git a/app/server/Models/Account.h b/app/server/Models/Account.h new file mode 100644 index 0000000..c738bbc --- /dev/null +++ b/app/server/Models/Account.h @@ -0,0 +1,59 @@ +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef MODELS_ACCOUNT_H +#define MODELS_ACCOUNT_H + +#include +#include + +#include "AccountStatus.h" +#include "Organization.h" + +namespace Models { + typedef struct { + int id = 0; + + AccountStatus status = AccountStatus::ACTIVE; + + char *email = NULL; + + char *lang = NULL; + + Organization *org = NULL; + + time_t created_at = 0; + } Account; + + inline + void freeAccount(Account *obj) + { + if (obj->email != NULL) { + free(obj->email); + obj->email = NULL; + + } + + if (obj->lang != NULL) { + free(obj->lang); + obj->lang = NULL; + + } + + if (obj->org != NULL) { + freeOrganization(obj->org); + obj->org = NULL; + + } + + free(obj); + } +} + +#endif \ No newline at end of file diff --git a/app/server/Models/AccountStatus.h b/app/server/Models/AccountStatus.h new file mode 100644 index 0000000..d3780bb --- /dev/null +++ b/app/server/Models/AccountStatus.h @@ -0,0 +1,20 @@ +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef MODELS_ACCOUNT_STATUS_H +#define MODELS_ACCOUNT_STATUS_H + +namespace Models { + typedef enum { + ACTIVE = 1, + INACTIVE = 2 + } AccountStatus; +} + +#endif \ No newline at end of file diff --git a/app/server/Models/Db.h b/app/server/Models/Db.h index 77ca6b1..0eb26fb 100644 --- a/app/server/Models/Db.h +++ b/app/server/Models/Db.h @@ -10,17 +10,21 @@ #ifndef MODELS_DB_H #define MODELS_DB_H -namespace Models { - class Db { - private: +#include - public: - static inline - int setup_connection (char *cfg) - { - return 0; - } - }; +namespace Models { + namespace Db { + inline + int setup_connection (char *cfg) + { + return 0; + } + + Resource *get_unchecked_resources(time_t olderThan) + { + + } + } } #endif \ No newline at end of file diff --git a/app/server/Models/InstallType.h b/app/server/Models/InstallType.h new file mode 100644 index 0000000..76bed96 --- /dev/null +++ b/app/server/Models/InstallType.h @@ -0,0 +1,20 @@ +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef MODELS_INSTALL_TYPE_H +#define MODELS_INSTALL_TYPE_H + +namespace Models { + typedef enum { + WEB = 0, + LOCAL = 1 + } InstallType; +} + +#endif \ No newline at end of file diff --git a/app/server/Models/Organization.h b/app/server/Models/Organization.h new file mode 100644 index 0000000..8daef66 --- /dev/null +++ b/app/server/Models/Organization.h @@ -0,0 +1,30 @@ +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef MODELS_ORGANIZATION_H +#define MODELS_ORGANIZATION_H + +#include +#include + +#include "ReosurceStatus.h" + +namespace Models { + typedef struct { + int id = 0; + } Organization; + + inline + void freeOrganization(Organization *obj) + { + free(obj); + } +} + +#endif \ No newline at end of file diff --git a/app/server/Models/ReosurceStatus.h b/app/server/Models/ReosurceStatus.h new file mode 100644 index 0000000..be9300e --- /dev/null +++ b/app/server/Models/ReosurceStatus.h @@ -0,0 +1,20 @@ +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef MODELS_RESOURCE_STATUS_H +#define MODELS_RESOURCE_STATUS_H + +namespace Models { + typedef enum { + ACTIVE = 1, + INACTIVE = 2 + } ResourceStatus; +} + +#endif \ No newline at end of file diff --git a/app/server/Models/Resource.h b/app/server/Models/Resource.h new file mode 100644 index 0000000..cb239e8 --- /dev/null +++ b/app/server/Models/Resource.h @@ -0,0 +1,88 @@ +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef MODELS_RESOURCE_H +#define MODELS_RESOURCE_H + +#include +#include + +#include "ReosurceStatus.h" +#include "Organization.h" +#include "ResourceInfo.h" + +namespace Models { + typedef struct { + int id = 0; + + ResourceStatus status = ResourceStatus::INACTIVE; + + char *uri = NULL; + + char *xpath = NULL; + + char *hash = NULL; + + char *last_version_path = NULL; + + time_t last_version_date = 0; + + time_t checked_at = 0; + + Organization *org = NULL; + + time_t created_at = 0; + + ResourceInfo *info = NULL; + } Resource; + + inline + void freeResource(Resource *obj) + { + if (obj->uri != NULL) { + free(obj->uri); + obj->uri = NULL; + + } + + if (obj->xpath != NULL) { + free(obj->xpath); + obj->xpath = NULL; + + } + + if (obj->hash != NULL) { + free(obj->hash); + obj->hash = NULL; + + } + + if (obj->last_version_path != NULL) { + free(obj->last_version_path); + obj->last_version_path = NULL; + + } + + if (obj->info != NULL) { + free(obj->info); + obj->info = NULL; + + } + + if (obj->org != NULL) { + freeOrganization(obj->org); + obj->org = NULL; + + } + + free(obj); + } +} + +#endif \ No newline at end of file diff --git a/app/server/Models/ResourceInfo.h b/app/server/Models/ResourceInfo.h new file mode 100644 index 0000000..6fe6029 --- /dev/null +++ b/app/server/Models/ResourceInfo.h @@ -0,0 +1,47 @@ +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef MODELS_RESOURCE_INFO_H +#define MODELS_RESOURCE_INFO_H + +#include +#include + +#include "Account.h" + +namespace Models { + typedef struct { + int id = 0; + + char *mail = NULL; + + Account *account = NULL; + + int resource = 0; + } ResourceInfo; + + inline + void freeResourceInfo(ResourceInfo *obj) + { + if (obj->mail != NULL) { + free(obj->mail); + obj->mail = NULL; + + } + + if (obj->account != NULL) { + freeAccount(obj->account); + obj->account = NULL; + } + + free(obj); + } +} + +#endif \ No newline at end of file diff --git a/app/server/build/install.sh b/app/server/build/install.sh index 23e037a..eaaf0f8 100644 --- a/app/server/build/install.sh +++ b/app/server/build/install.sh @@ -1,5 +1,6 @@ #!/bin/bash +sudo apt-get install libcurl4-openssl-dev sudo apt-get install sqlite3 libsqlite3-dev sudo apt install default-libmysqlclient-dev sudo apt-get install libxml2-dev diff --git a/app/server/install/db.sqlite b/app/server/install/db.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..46da2cad3a64ec767b41ed9df5e0292161d0453d GIT binary patch literal 45056 zcmeI)&u-&H90zc_?&gn6(}f;#C@V}a8`EW>tT-Y_i@SrOAa#sIR^hFhs4uStrp!Qt;T|E88r?qnZ=1uw8CxP|Nh#*59*1j-~ zwPwF})auYy4~*9`rJ5Z_S@0f=8N+lCgtag z*dDP0GBhpPX!hg>M_r?Jx5Ikh$XspGvaWTF1EXtn_KgQxQfQDaKOE@vyakQ%s}kGd zsf|Ir$B1=RKmTF9T-SB^a#xI?eM;qJ5H_iKD7txNGJ5jT`chqCT%s6~vR? z4Kfqv_2btJ^AYiU$DNojYD_3Rq?4`a%wbd(Ovgmi zbiI>wfUh!&hD!bfWKD^&XDN*V{U6V_i+#0R-`bL&eI3rXcrwNtMT|F}vthVX)Akit zMqaZh*u2chJ#i)xDO=rm^3?ME?_7@#w7vGxUOI;kRT7v{h_iVlT7|Rgc)EJvIV5L( zYkVdCFkN^=XLwGyc_*`R0OY zm+PCG^3yX;PS3w0(6VV3t&Oa!uYL=`Y86cqmOrt^WH)CJ^1P1<^6o6id+3aa?|e_p zku!Euw=cV>@gvA9PHgoXyC@CYi%) zf^H_W!6a_cn)>-r3753XccLXN3R@h#RiOe`SF^2tWV=5P$##AOHafKmY;|fWWc}u;+he<5y|p?~Om{ z1|JAO00Izz00bZa0SG_<0uX=z1pZF~@8xvnO-0cw$*VKMR|37YqU==IdouVd9Oz41 zR9V@Q; #include - -#include -#include +#include #include "cOMS/Utils/ArrayUtils.h" #include "cOMS/Utils/FileUtils.h" +#include "cOMS/Utils/WebUtils.h" #include "cOMS/Hash/MeowHash.h" #include "cOMS/Utils/Parser/Json.h" +#include "Stdlib/HashTable.h" #include "Models/Db.h" +#include "Models/InstallType.h" #ifndef OMS_DEMO #define OMS_DEMO false #endif -void printHelp() +void (*f_ptr)(int, char **); + +void printHelp(int argc, char **argv) { printf(" The Online Resource Watcher app developed by jingga checks online or local resources\n"); printf(" for changes and and informs the user about them.\n\n"); @@ -42,6 +45,19 @@ void printVersion() printf("Version: 1.0.0\n"); } +void install(Models::InstallType type = Models::InstallType::LOCAL) +{ + if (type == Models::InstallType::LOCAL) { + // create sqlite database + + } else { + + } + + // create config file + nlohmann::json config; +} + void parseConfigFile() { FILE *fp = fopen("config.json"); @@ -49,39 +65,73 @@ void parseConfigFile() nlohmann::json config = nlohmann::json::parse(fp); } -bool isResourceModified(char *filename, time_t last_change) +inline +bool isResourceDateModified(char *filename, time_t lastChange) { - return oms_abs(Utils::FileUtils::last_modification(filename) - last_change) > 1; + return oms_abs(Utils::FileUtils::last_modification(filename) - lastChange) > 1; } -bool hasResourceContentChanged(char *filename1, char *filename2) +inline +bool hasResourceContentChanged(Utils::FileUtils::file_body f1, Utils::FileUtils::file_body f2) { - Utils::FileUtils::file_body f1 = Utils::FileUtils::read_file(filename1); - Utils::FileUtils::file_body f2 = Utils::FileUtils::read_file(filename2); - Hash::Meow::meow_u128 h1 = Hash::Meow::MeowHash(Hash::Meow::MeowDefaultSeed, f1.size, f1.content); Hash::Meow::meow_u128 h2 = Hash::Meow::MeowHash(Hash::Meow::MeowDefaultSeed, f2.size, f2.content); - bool areHashesEqual = Hash::Meow::MeowHashesAreEqual(h1, h2); + return Hash::Meow::MeowHashesAreEqual(h1, h2); +} + +Utils::FileUtils::file_body hasChanged(char *oldResource, char *newResource, time_t lastChange) +{ + char *t; + int length = 0; + + for (t = newResource; *t != '\0' && length < 7; ++t) { + ++length; + } + + Utils::FileUtils::file_body f1; + Utils::FileUtils::file_body f2; + + bool isFileModified = false; + if (length > 5 + && (strncmp(newResource, "https:", 6) || strncmp(newResource, "www.", 4)) + ) { + // web resource + f1 = Utils::FileUtils::read_file(oldResource); + f2 = Utils::WebUtils::download(newResource); + } else { + // local resource + isFileModified = isResourceDateModified(oldResource, lastChange); + if (isFileModified) { + f1 = Utils::FileUtils::read_file(oldResource); + f2 = Utils::FileUtils::read_file(newResource); + } + } + + bool hasChanged = isFileModified || hasResourceContentChanged(f1, f2); free(f1.content); - free(f2.content); - return areHashesEqual; + if (hasChanged) { + free(f2.content); + f2.size = -1; + } + + return f2; } -void saveResourceChange() +void saveResourceChange(char *url, char *oldResource) { + Utils::FileUtils::file_body dowloadData = Utils::WebUtils::download(url); + Utils::FileUtils::file_body fileData = Utils::FileUtils::read_file(oldResource); } -MYSQL *con = null; - int main(int argc, char **argv) { bool hasHelpCmd = Utils::ArrayUtils::has_arg("-h", argv, argc); if (hasHelpCmd) { - printHelp(); + printHelp(argc, argv); return 0; } @@ -93,6 +143,27 @@ int main(int argc, char **argv) return 0; } + f_ptr = &printHelp; + + Stdlib::HashTable::ht *table = Stdlib::HashTable::create_table(); + if (table == NULL) { + return -1; + } + + Stdlib::HashTable::set_entry(table, "-h", &printHelp); + + regex_t regex; + regcomp(®ex, "\-h", 0); + regexec(®ex, argv[0], 0, NULL, 0) == 0; + + // @todo handle install + // create config + // check install type + // web = copy config from web + // local + // create sqlite db + // create config from template + if (!Utils::FileUtils::file_exists("config.json")) { printf("No config file available."); @@ -101,15 +172,17 @@ int main(int argc, char **argv) unsigned long resourceId = (unsigned long) Utils::ArrayUtils::get_arg("-r", argv, argc); + // @todo handle resources + // load config + // get resources + // active + // last check older than 23 h + // check if resource changed + // save new version + // find differences + // inform users - // read config file - // create database connection (either mariadb or sqlite) - // @todo create wrapper for sqlite, mysql and postgresql + Resource res[10]; - con = mysql_init(NULL); - if (mysql_real_connect(con, "localhost", "root", "root_passwd", NULL, 0, NULL, 0) == NULL) { - fprintf(stderr, "%s\n", mysql_error(con)); - mysql_close(con); - exit(1); - } + Stdlib::HashTable::free_table(table); } diff --git a/app/web/Install/db.json b/app/web/Install/db.json index 1b6edba..0fcfff9 100644 --- a/app/web/Install/db.json +++ b/app/web/Install/db.json @@ -159,25 +159,25 @@ } } }, - "org_addressrel": { - "name": "org_addressrel", + "org_address_rel": { + "name": "org_address_rel", "fields": { - "org_addressrel_id": { - "name": "org_addressrel_id", + "org_address_rel_id": { + "name": "org_address_rel_id", "type": "INT", "null": false, "primary": true, "autoincrement": true }, - "org_addressrel_org": { - "name": "org_addressrel_org", + "org_address_rel_org": { + "name": "org_address_rel_org", "type": "INT", "null": false, "foreignTable": "org", "foreignKey": "org_id" }, - "org_addressrel_address": { - "name": "org_addressrel_address", + "org_address_rel_address": { + "name": "org_address_rel_address", "type": "INT", "null": false, "foreignTable": "address", @@ -413,8 +413,8 @@ "foreignTable": "resource", "foreignKey": "resource_id" }, - "resource_check_created_at": { - "name": "resource_check_created_at", + "resource_info_created_at": { + "name": "resource_info_created_at", "type": "DATETIME", "null": false }