fix: minor code refactor

This commit is contained in:
Tobias Reisinger 2020-08-13 11:26:06 +02:00
parent 505faa6df2
commit f167f9caec
22 changed files with 83 additions and 30 deletions

2
.gitignore vendored
View file

@ -4,4 +4,4 @@ docs/
tests/testing_tmp/ tests/testing_tmp/
tests/testing_bak/ tests/testing_bak/
include/migrations/*.sql.h include/sql/*.h

View file

@ -20,19 +20,21 @@ aux_source_directory(src/models MODELS_SRC)
aux_source_directory(src/helpers HELPERS_SRC) aux_source_directory(src/helpers HELPERS_SRC)
aux_source_directory(src/handlers HANDLERS_SRC) aux_source_directory(src/handlers HANDLERS_SRC)
aux_source_directory(src/endpoints ENDPOINTS_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("core.ini" "core.ini" COPYONLY)
configure_file("version.h.in" "version.h" @ONLY) 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}/include)
target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor)
target_include_directories(core PRIVATE ${CMAKE_BINARY_DIR}) target_include_directories(core PRIVATE ${CMAKE_BINARY_DIR})
add_custom_target(migrations add_custom_target(sql
COMMAND ./compile_migrations.sh COMMAND ./compile_sql.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
) )

View file

@ -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;

37
compile_sql.sh Executable file
View file

@ -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;

11
include/cache.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef CORE_CACHE_H
#define CORE_CACHE_H
#include <sqlite3.h>
extern sqlite3 *cache_database;
void
cache_init();
#endif /* CORE_CACHE_H */

5
sql/cache.sql Normal file
View file

@ -0,0 +1,5 @@
CREATE TABLE `cache` (
`key` STRING PRIMARY KEY,
`value` TEXT NOT NULL,
`expiration` INT DEFAULT 0
);

17
src/cache.c Normal file
View file

@ -0,0 +1,17 @@
#include <cache.h>
#include <logger.h>
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;
}
}

View file

@ -2,8 +2,9 @@
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <command.h>
#include <mpack.h> #include <mpack.h>
#include <command.h>
#include <logger.h> #include <logger.h>
#include <helpers.h> #include <helpers.h>
#include <enums.h> #include <enums.h>

View file

@ -4,7 +4,7 @@
#include <logger.h> #include <logger.h>
#include <database.h> #include <database.h>
#include <migrations/0.sql.h> #include <sql/migration_0.h>
sqlite3 *global_database; sqlite3 *global_database;

View file

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <argparse.h> #include <argparse.h>
#include <config.h> #include <config.h>
#include <logger.h> #include <logger.h>
#include <helpers.h> #include <helpers.h>

View file

@ -5,6 +5,8 @@
#include <syslog.h> #include <syslog.h>
#include <mongoose.h> #include <mongoose.h>
#include <confini.h>
#include <router.h> #include <router.h>
#include <logger.h> #include <logger.h>
#include <config.h> #include <config.h>
@ -13,7 +15,6 @@
#include <enums.h> #include <enums.h>
#include <helpers.h> #include <helpers.h>
#include <status.h> #include <status.h>
#include <confini.h>
#include <models/controller.h> #include <models/controller.h>
static struct mg_mgr mgr; static struct mg_mgr mgr;

View file

View file

View file

View file

View file

View file

View file