From f167f9caec30037ac4d6163a0327810fefd22ef3 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Thu, 13 Aug 2020 11:26:06 +0200 Subject: [PATCH] fix: minor code refactor --- .gitignore | 2 +- CMakeLists.txt | 10 +++++--- compile_migrations.sh | 22 ----------------- compile_sql.sh | 37 ++++++++++++++++++++++++++++ include/cache.h | 11 +++++++++ include/{migrations => sql}/.gitkeep | 0 sql/cache.sql | 5 ++++ src/cache.c | 17 +++++++++++++ src/command.c | 3 ++- src/database.c | 2 +- src/helpers/parse_cli.c | 1 + src/main.c | 3 ++- {src => vendor}/argparse.c | 0 {include => vendor}/argparse.h | 0 {src => vendor}/cJSON.c | 0 {include => vendor}/cJSON.h | 0 {src => vendor}/confini.c | 0 {include => vendor}/confini.h | 0 {src => vendor}/mongoose.c | 0 {include => vendor}/mongoose.h | 0 {src => vendor}/mpack.c | 0 {include => vendor}/mpack.h | 0 22 files changed, 83 insertions(+), 30 deletions(-) delete mode 100755 compile_migrations.sh create mode 100755 compile_sql.sh create mode 100644 include/cache.h rename include/{migrations => sql}/.gitkeep (100%) create mode 100644 sql/cache.sql create mode 100644 src/cache.c rename {src => vendor}/argparse.c (100%) rename {include => vendor}/argparse.h (100%) rename {src => vendor}/cJSON.c (100%) rename {include => vendor}/cJSON.h (100%) rename {src => vendor}/confini.c (100%) rename {include => vendor}/confini.h (100%) rename {src => vendor}/mongoose.c (100%) rename {include => vendor}/mongoose.h (100%) rename {src => vendor}/mpack.c (100%) rename {include => vendor}/mpack.h (100%) diff --git a/.gitignore b/.gitignore index 293edcd..02a311e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ docs/ tests/testing_tmp/ tests/testing_bak/ -include/migrations/*.sql.h +include/sql/*.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e5622c8..d2bddf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,19 +20,21 @@ aux_source_directory(src/models MODELS_SRC) aux_source_directory(src/helpers HELPERS_SRC) aux_source_directory(src/handlers HANDLERS_SRC) aux_source_directory(src/endpoints ENDPOINTS_SRC) +aux_source_directory(vendor VENDOR_SRC) -add_dependencies(core migrations) +add_dependencies(core sql) configure_file("core.ini" "core.ini" COPYONLY) configure_file("version.h.in" "version.h" @ONLY) -target_sources(core PRIVATE ${SRC_DIR} ${HANDLERS_SRC} ${HELPERS_SRC} ${MODELS_SRC} ${ENDPOINTS_SRC}) +target_sources(core PRIVATE ${SRC_DIR} ${HANDLERS_SRC} ${HELPERS_SRC} ${MODELS_SRC} ${ENDPOINTS_SRC} ${VENDOR_SRC}) target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor) target_include_directories(core PRIVATE ${CMAKE_BINARY_DIR}) -add_custom_target(migrations - COMMAND ./compile_migrations.sh +add_custom_target(sql + COMMAND ./compile_sql.sh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/compile_migrations.sh b/compile_migrations.sh deleted file mode 100755 index 7a0a1fe..0000000 --- a/compile_migrations.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env sh - -cd "$(dirname "$0")"; - -migration_num=0; - -while [ -f ./sql/migration_$migration_num.sql ] -do - if [ -f ./include/migrations/$migration_num.sql.h ] - then - if [ -z $(find ./sql/migration_$migration_num.sql -newer ./include/migrations/$migration_num.sql.h -print) ] - then - migration_num=$((migration_num+1)) - continue - fi - fi - echo "Recompiling migration $migration_num" - xxd -i sql/migration_$migration_num.sql | sed 's/\([0-9a-f]\)$/\0, 0x00/' > ./include/migrations/$migration_num.sql.h - migration_num=$((migration_num+1)) -done - -cd $PWD; diff --git a/compile_sql.sh b/compile_sql.sh new file mode 100755 index 0000000..24625a5 --- /dev/null +++ b/compile_sql.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env sh + +function to_header() +{ + if [ -f ./include/sql/$1.h ] + then + if [ -z $(find ./sql/$1.sql -newer ./include/sql/$1.h -print) ] + then + return + fi + fi + echo "Compiling $1" + xxd -i sql/$1.sql | sed 's/\([0-9a-f]\)$/\0, 0x00/' > ./include/sql/$1.h + +} + +cd "$(dirname "$0")"; + +migration_num=0; + +while [ -f ./sql/migration_$migration_num.sql ] +do + if [ -f ./include/migrations/$migration_num.sql.h ] + then + if [ -z $(find ./sql/migration_$migration_num.sql -newer ./include/sql/migration_$migration_num.h -print) ] + then + migration_num=$((migration_num+1)) + continue + fi + fi + to_header "migration_$migration_num" + migration_num=$((migration_num+1)) +done + +to_header "cache" + +cd $PWD; diff --git a/include/cache.h b/include/cache.h new file mode 100644 index 0000000..3e1ca63 --- /dev/null +++ b/include/cache.h @@ -0,0 +1,11 @@ +#ifndef CORE_CACHE_H +#define CORE_CACHE_H + +#include + +extern sqlite3 *cache_database; + +void +cache_init(); + +#endif /* CORE_CACHE_H */ diff --git a/include/migrations/.gitkeep b/include/sql/.gitkeep similarity index 100% rename from include/migrations/.gitkeep rename to include/sql/.gitkeep diff --git a/sql/cache.sql b/sql/cache.sql new file mode 100644 index 0000000..a8904b5 --- /dev/null +++ b/sql/cache.sql @@ -0,0 +1,5 @@ +CREATE TABLE `cache` ( + `key` STRING PRIMARY KEY, + `value` TEXT NOT NULL, + `expiration` INT DEFAULT 0 +); diff --git a/src/cache.c b/src/cache.c new file mode 100644 index 0000000..e6c80d3 --- /dev/null +++ b/src/cache.c @@ -0,0 +1,17 @@ +#include +#include + + +sqlite3 *cache_database; + +void +cache_init() +{ + int rc = sqlite3_open(":memory:", &cache_database); + + if(rc) + { + LOGGER_CRIT("can't open cache database: %s\n", sqlite3_errmsg(cache_database)); + return; + } +} diff --git a/src/command.c b/src/command.c index bb9b3a5..141e5a6 100644 --- a/src/command.c +++ b/src/command.c @@ -2,8 +2,9 @@ #include #include -#include #include + +#include #include #include #include diff --git a/src/database.c b/src/database.c index 2429786..cb8baa7 100644 --- a/src/database.c +++ b/src/database.c @@ -4,7 +4,7 @@ #include #include -#include +#include sqlite3 *global_database; diff --git a/src/helpers/parse_cli.c b/src/helpers/parse_cli.c index a65caed..f205541 100644 --- a/src/helpers/parse_cli.c +++ b/src/helpers/parse_cli.c @@ -3,6 +3,7 @@ #include #include + #include #include #include diff --git a/src/main.c b/src/main.c index 3ee3b8f..a618bc9 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,8 @@ #include #include +#include + #include #include #include @@ -13,7 +15,6 @@ #include #include #include -#include #include static struct mg_mgr mgr; diff --git a/src/argparse.c b/vendor/argparse.c similarity index 100% rename from src/argparse.c rename to vendor/argparse.c diff --git a/include/argparse.h b/vendor/argparse.h similarity index 100% rename from include/argparse.h rename to vendor/argparse.h diff --git a/src/cJSON.c b/vendor/cJSON.c similarity index 100% rename from src/cJSON.c rename to vendor/cJSON.c diff --git a/include/cJSON.h b/vendor/cJSON.h similarity index 100% rename from include/cJSON.h rename to vendor/cJSON.h diff --git a/src/confini.c b/vendor/confini.c similarity index 100% rename from src/confini.c rename to vendor/confini.c diff --git a/include/confini.h b/vendor/confini.h similarity index 100% rename from include/confini.h rename to vendor/confini.h diff --git a/src/mongoose.c b/vendor/mongoose.c similarity index 100% rename from src/mongoose.c rename to vendor/mongoose.c diff --git a/include/mongoose.h b/vendor/mongoose.h similarity index 100% rename from include/mongoose.h rename to vendor/mongoose.h diff --git a/src/mpack.c b/vendor/mpack.c similarity index 100% rename from src/mpack.c rename to vendor/mpack.c diff --git a/include/mpack.h b/vendor/mpack.h similarity index 100% rename from include/mpack.h rename to vendor/mpack.h