Merge pull request 'updated database and database_type in config file' (#5) from develop into main
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 12s

Reviewed-on: https://gitea.home.com:3000/TolaMironcenko/auth_service_c/pulls/5
This commit was merged in pull request #5.
This commit is contained in:
wt
2025-10-30 22:34:01 +07:00
18 changed files with 185 additions and 71 deletions
+10 -9
View File
@@ -1,10 +1,11 @@
{
"database": "users.db",
"address": "0.0.0.0:8222",
"tls_address": "0.0.0.0:8223",
"default_users_file": "users.json",
"web": "./web",
"tls_enabled": true,
"tls_cert": "/home/tola/certs/localhost/localhost.crt",
"tls_key": "/home/tola/certs/localhost/localhost.key"
}
"database": "asdf.json",
"database_type": "file",
"address": "0.0.0.0:8222",
"tls_address": "0.0.0.0:8223",
"default_users_file": "users.json",
"web": "./web",
"tls_enabled": true,
"tls_cert": "/home/tola/certs/localhost/localhost.crt",
"tls_key": "/home/tola/certs/localhost/localhost.key"
}
+22 -7
View File
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config/ConfigManager.h"
#include "config/config.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
@@ -10,17 +12,30 @@
void init_db() {
#ifdef USE_SQLITE3
sqlite_init_db();
#else
return;
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
sqlite_init_db();
free(dbtype);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return;
#ifdef USE_SQLITE3
}
#endif
}
void migrate_db() {
#ifdef USE_SQLITE3
sqlite_migrate_db();
#else
return;
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
sqlite_migrate_db();
free(dbtype);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return;
#ifdef USE_SQLITE3
}
#endif
}
+12 -4
View File
@@ -1,15 +1,23 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
bool db_delete_user(const int id) {
#ifdef USE_SQLITE3
return sqlite_delete_user(id);
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_delete_user(id);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_delete_user(id);
#ifdef USE_SQLITE3
}
return filedb_delete_user(id);
#endif
}
+12 -4
View File
@@ -1,15 +1,23 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
char *db_get_all_users() {
#ifdef USE_SQLITE3
return sqlite_get_all_users();
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_get_all_users();
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_get_all_users();
#ifdef USE_SQLITE3
}
return filedb_get_all_users();
#endif
}
+12 -4
View File
@@ -1,15 +1,23 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
User *db_get_user_by_id(const int id) {
#ifdef USE_SQLITE3
return sqlite_get_user_by_id(id);
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_get_user_by_id(id);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_get_user_by_id(id);
#ifdef USE_SQLITE3
}
return filedb_get_user_by_id(id);
#endif
}
+12 -4
View File
@@ -1,15 +1,23 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
User *db_get_user_by_username(const char *username) {
#ifdef USE_SQLITE3
return sqlite_get_user_by_username(username);
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_get_user_by_username(username);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_get_user_by_username(username);
#ifdef USE_SQLITE3
}
return filedb_get_user_by_username(username);
#endif
}
+12 -4
View File
@@ -1,15 +1,23 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
char *db_get_user_field_by_id(const int id, const char *field) {
#ifdef USE_SQLITE3
return sqlite_get_user_field_by_id(id, field);
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_get_user_field_by_id(id, field);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_get_user_field_by_id(id, field);
#ifdef USE_SQLITE3
}
return filedb_get_user_field_by_id(id, field);
#endif
}
+12 -4
View File
@@ -1,17 +1,25 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
User *db_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
#ifdef USE_SQLITE3
return sqlite_insert_user(username, email, password, group, is_superuser);
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_insert_user(username, email, password, group, is_superuser);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_insert_user(username, email, password, group, is_superuser);
#ifdef USE_SQLITE3
}
return filedb_insert_user(username, email, password, group, is_superuser);
#endif
}
+14 -5
View File
@@ -1,18 +1,27 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
User *db_update_user(const int userid, const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
#ifdef USE_SQLITE3
return sqlite_update_user(userid, username, email, password, group,
is_superuser);
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_update_user(userid, username, email, password, group,
is_superuser);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_update_user(userid, username, email, password, group,
is_superuser);
#ifdef USE_SQLITE3
}
return filedb_update_user(userid, username, email, password, group,
is_superuser);
#endif
+12 -4
View File
@@ -1,15 +1,23 @@
#include "api/user.h"
#include "config/config.h"
#include "file_db/filedb.h"
#ifdef USE_SQLITE3
#include "sqlite_db/sqlite.h"
#else
#include "file_db/filedb.h"
#endif
bool db_update_user_field(const int id, const char *field, const char *value) {
#ifdef USE_SQLITE3
return sqlite_update_user_field(id, field, value);
#else
char *dbtype = cm_get_parameter_as_string(config, "database_type");
if (!strcmp(dbtype, "sqlite")) {
free(dbtype);
return sqlite_update_user_field(id, field, value);
} else if (!strcmp(dbtype, "file")) {
free(dbtype);
#endif
return filedb_update_user_field(id, field, value);
#ifdef USE_SQLITE3
}
return filedb_update_user_field(id, field, value);
#endif
}
+10 -3
View File
@@ -1,12 +1,15 @@
#include <pthread.h>
#include <stdlib.h>
#include "utils/file.h"
#include "cJSON.h"
#include "config/ConfigManager.h"
#include "config/config.h"
#include "filedb.h"
#include "utils/file.h"
bool filedb_delete_user(const int id) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
int index = 0;
@@ -22,12 +25,16 @@ bool filedb_delete_user(const int id) {
}
if (!found) {
pthread_mutex_unlock(&users_file_mutex);
free(data);
free(database_file);
cJSON_Delete(users);
return false;
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
write_file(database_file, new_data);
free(new_data);
free(data);
free(database_file);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;
+5 -2
View File
@@ -1,10 +1,13 @@
#include <pthread.h>
#include "utils/file.h"
#include "config/config.h"
#include "filedb.h"
#include "utils/file.h"
char *filedb_get_all_users() {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
free(database_file);
pthread_mutex_unlock(&users_file_mutex);
return data;
}
+5 -1
View File
@@ -1,10 +1,12 @@
#include <pthread.h>
#include "utils/file.h"
#include "filedb.h"
#include "config/config.h"
User *filedb_get_user_by_id(const int id) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
@@ -27,11 +29,13 @@ User *filedb_get_user_by_id(const int id) {
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
free(database_file);
return nullptr;
}
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
free(database_file);
return found;
}
+5 -3
View File
@@ -1,10 +1,12 @@
#include <pthread.h>
#include "utils/file.h"
#include "filedb.h"
#include "config/config.h"
User *filedb_get_user_by_username(const char *username) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
@@ -24,14 +26,14 @@ User *filedb_get_user_by_username(const char *username) {
}
if (found == nullptr) {
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
free(database_file);
return nullptr;
}
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
free(database_file);
return found;
}
+5 -3
View File
@@ -1,10 +1,12 @@
#include <pthread.h>
#include "utils/file.h"
#include "filedb.h"
#include "config/config.h"
char *filedb_get_user_field_by_id(const int id, const char *field) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
@@ -24,14 +26,14 @@ char *filedb_get_user_field_by_id(const int id, const char *field) {
}
if (found == nullptr) {
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
free(database_file);
return nullptr;
}
pthread_mutex_unlock(&users_file_mutex);
// if (user != nullptr) cJSON_Delete(user);
cJSON_Delete(users);
free(data);
free(database_file);
return found->password;
}
+10 -4
View File
@@ -1,12 +1,14 @@
#include <pthread.h>
#include "utils/file.h"
#include "config/config.h"
#include "filedb.h"
#include "utils/file.h"
User *filedb_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
int users_size = 0;
@@ -26,6 +28,10 @@ User *filedb_insert_user(const char *username, const char *email,
}
if (found == true) {
pthread_mutex_unlock(&users_file_mutex);
free(data);
cJSON_Delete(users);
if (user != nullptr) cJSON_Delete(user);
free(database_file);
return nullptr;
}
cJSON *new_user_data = cJSON_CreateObject();
@@ -41,15 +47,15 @@ User *filedb_insert_user(const char *username, const char *email,
}
cJSON_AddItemToArray(users, new_user_data);
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
write_file(database_file, new_data);
pthread_mutex_unlock(&users_file_mutex);
User *return_user =
new_user(users_size, username, email, password, group, is_superuser);
free(new_data);
free(data);
free(new_user_data_string);
// cJSON_Delete(new_user_data);
cJSON_Delete(users);
if (user != nullptr) cJSON_Delete(user);
free(database_file);
return return_user;
}
+7 -3
View File
@@ -1,12 +1,14 @@
#include <pthread.h>
#include "utils/file.h"
#include "config/config.h"
#include "filedb.h"
#include "utils/file.h"
User *filedb_update_user(const int userid, const char *username,
const char *email, const char *password,
const char *group, const bool *is_superuser) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
@@ -23,10 +25,11 @@ User *filedb_update_user(const int userid, const char *username,
cJSON_DeleteItemFromObject(user, "is_superuser");
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
write_file(database_file, new_data);
free(new_data);
free(data);
cJSON_Delete(users);
free(database_file);
User *ret =
new_user(userid, username, password, email, group, is_superuser);
pthread_mutex_unlock(&users_file_mutex);
@@ -34,6 +37,7 @@ User *filedb_update_user(const int userid, const char *username,
}
}
free(data);
free(database_file);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return nullptr;
+8 -3
View File
@@ -1,11 +1,13 @@
#include <pthread.h>
#include "utils/file.h"
#include "config/config.h"
#include "filedb.h"
#include "utils/file.h"
bool filedb_update_user_field(const int id, const char *field,
const char *value) {
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
char *database_file = cm_get_parameter_as_string(config, "database");
char *data = read_file(database_file);
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
cJSON_ArrayForEach(user, users) {
@@ -14,18 +16,21 @@ bool filedb_update_user_field(const int id, const char *field,
cJSON *user_field = cJSON_GetObjectItem(user, field);
if (cJSON_IsNumber(user_field)) {
// cJSON_SetNumberValue(user_field, value);
free(database_file);
return false;
} else if (cJSON_IsBool(user_field)) {
// cJSON_DeleteItemFromObject(user, field);
// cJSON_AddBoolToObject(user, field, value);
free(database_file);
return false;
} else if (cJSON_IsString(user_field)) {
cJSON_SetValuestring(user_field, value);
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
write_file(database_file, new_data);
free(new_data);
free(data);
free(database_file);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;