restructure

This commit is contained in:
Dennis Eichhorn 2022-09-20 17:24:30 +02:00
parent 26c291b0df
commit 9b12b8d643
5 changed files with 274 additions and 140 deletions

View File

@ -0,0 +1,123 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef CONTROLLER_API_H
#define CONTROLLER_API_H
#include <stdio.h>
#include <stdlib.h>
#include "cOMS/Utils/ArrayUtils.h"
#include "cOMS/Utils/FileUtils.h"
#include "cOMS/Utils/WebUtils.h"
#include "cOMS/Hash/MeowHash.h"
#include "cOMS/Utils/MathUtils.h"
namespace Controller {
namespace ApiController {
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");
printf(" Run: ./App ....\n\n");
printf(" -h: Prints the help output\n");
printf(" -v: Prints the version\n");
printf("\n");
printf(" Website: https://jingga.app\n");
printf(" Copyright: jingga (c) Dennis Eichhorn\n");
}
void printVersion(int argc, char **argv)
{
printf("Version: 1.0.0\n");
}
void checkResources(int argc, char **argv)
{
unsigned long long resourceId = atoll(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
//Resource res[10];
}
inline
bool isResourceDateModified(char *filename, time_t lastChange)
{
return oms_abs(Utils::FileUtils::last_modification(filename) - lastChange) > 1;
}
inline
bool hasResourceContentChanged(Utils::FileUtils::file_body f1, Utils::FileUtils::file_body f2)
{
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);
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);
f1.size = -1;
if (hasChanged) {
free(f2.content);
f2.size = -1;
}
return f2;
}
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);
}
}
}
#endif

View File

@ -0,0 +1,55 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef CONTROLLER_INSTALL_H
#define CONTROLLER_INSTALL_H
#include <stdio.h>
#include <stdlib.h>
#include "cOMS/Utils/Parser/Json.h"
#include "../Models/InstallType.h"
namespace Controller {
namespace InstallController {
void installApplication(int argc, char **argv)
{
// @todo handle install
// create config
// check install type
// web = copy config from web
// local
// create sqlite db
// create config from template
}
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", "r");
nlohmann::json config = nlohmann::json::parse(fp);
}
}
}
#endif

62
app/server/Routes.h Normal file
View File

@ -0,0 +1,62 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef ROUTES_H
#define ROUTES_H
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include "Controller/ApiController.h"
#include "Controller/InstallController.h"
#include "Stdlib/HashTable.h"
typedef void (*Fptr)(int, char **);
Stdlib::HashTable::ht *generate_routes()
{
Stdlib::HashTable::ht *table = Stdlib::HashTable::create_table();
if (table == NULL) {
return NULL;
}
Stdlib::HashTable::set_entry(table, "^.*?\\-h *.*$", &Controller::ApiController::printHelp);
Stdlib::HashTable::set_entry(table, "^.*?\\-v *.*$", &Controller::ApiController::printVersion);
Stdlib::HashTable::set_entry(table, "^.*?\\-r *.*$", &Controller::ApiController::checkResources);
Stdlib::HashTable::set_entry(table, "^.*?\\-\\-install *.*$", &Controller::InstallController::installApplication);
return table;
}
Fptr match_route(Stdlib::HashTable::ht *routes, char *uri)
{
Fptr ptr = NULL;
Stdlib::HashTable::it itr = Stdlib::HashTable::table_iterator(routes);
regex_t regex;
while (Stdlib::HashTable::next(&itr)) {
regcomp(&regex, itr.key, 0);
int status = regexec(&regex, uri, 0, NULL, 0);
if (status == 0) {
ptr = (Fptr) itr.value;
}
}
// No endpoint found
if (ptr == NULL) {
ptr = &Controller::ApiController::printHelp;
}
return ptr;
}
#endif

@ -1 +1 @@
Subproject commit 7f3fc9a99fe49579033e7ce1456215b4ebe4a102
Subproject commit 641ff4e4e2a7de0db388cf7df73ea3e47e5583dd

View File

@ -10,179 +10,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#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"
#include "Routes.h"
#ifndef OMS_DEMO
#define OMS_DEMO false
#endif
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");
printf(" Run: ./App ....\n\n");
printf(" -h: Prints the help output\n");
printf(" -v: Prints the version\n");
printf("\n");
printf(" Website: https://jingga.app\n");
printf(" Copyright: jingga (c) Dennis Eichhorn\n");
}
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");
FILE *fp = fopen("config.json", "r");
nlohmann::json config = nlohmann::json::parse(fp);
}
inline
bool isResourceDateModified(char *filename, time_t lastChange)
char *compile_arg_line(int argc, char **argv)
{
return oms_abs(Utils::FileUtils::last_modification(filename) - lastChange) > 1;
}
size_t max = 512;
size_t length = 0;
char *arg = (char *) calloc(max, sizeof(char));
inline
bool hasResourceContentChanged(Utils::FileUtils::file_body f1, Utils::FileUtils::file_body f2)
{
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);
for (int i = 1; i < argc; ++i) {
size_t argv_length = strlen(argv[i]);
if (length + strlen(argv[i]) + 1 > max) {
char *tmp = (char *) calloc(max + 128, sizeof(char));
memcpy(tmp, arg, (length + 1) * sizeof(char));
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);
free(arg);
arg = tmp;
max += 128;
}
strcat(arg, argv[i]);
length += argv_length;
}
bool hasChanged = isFileModified || hasResourceContentChanged(f1, f2);
free(f1.content);
if (hasChanged) {
free(f2.content);
f2.size = -1;
}
return f2;
}
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);
return arg;
}
int main(int argc, char **argv)
{
bool hasHelpCmd = Utils::ArrayUtils::has_arg("-h", argv, argc);
if (hasHelpCmd) {
printHelp(argc, argv);
char *arg = compile_arg_line(argc, argv);
return 0;
}
bool hasVersionCmd = Utils::ArrayUtils::has_arg("-v", argv, argc);
if (hasVersionCmd) {
printVersion();
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(&regex, "\-h", 0);
regexec(&regex, 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
// @todo: Check is installed?
// no? install
// Load config
if (!Utils::FileUtils::file_exists("config.json")) {
printf("No config file available.");
return -1;
}
unsigned long resourceId = (unsigned long) Utils::ArrayUtils::get_arg("-r", argv, argc);
// Handle routes
Stdlib::HashTable::ht *routes = generate_routes();
if (routes == NULL) {
return -1;
}
// @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
Fptr ptr = match_route(routes, arg);
Resource res[10];
// Dispatch found endpoint
(*ptr)(argc, argv);
Stdlib::HashTable::free_table(table);
Stdlib::HashTable::free_table(routes);
free(arg);
arg = NULL;
}