added USE_SQLITE3 option
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s

This commit is contained in:
wt committed 2025-10-20 17:29:32 +07:00
1 parent 634245e3ad
commit 73a73976fe
18 files changed
+115 -32

No files matched your search

+3 -3
View File
@@ -9,7 +9,7 @@ jobs:
- name: Check out repository code
uses: actions/checkout@v3
- name: Building binary with make
run: make -j$(nproc)
run: make USE_SQLITE3=1 -j$(nproc)
- name: Testing binary
run: make test
@@ -25,7 +25,7 @@ jobs:
run: |
mkdir build && \
cd build && \
cmake .. && \
cmake -DUSE_SQLITE3=ON .. && \
make -j$(nproc)
- name: Testing binary
run: |
@@ -41,7 +41,7 @@ jobs:
uses: actions/checkout@v3
- name: Building binary with meson
run: |
meson setup build && \
meson setup build -Duse_sqlite3=true && \
ninja -C build
- name: Testing binary
run: |
+12 -2
View File
@@ -10,11 +10,21 @@ include_directories(./src/lib/ ${SQLite3_INCLUDE_DIRS})
FILE(GLOB_RECURSE SOURCES src/*.c)
find_package(SQLite3 REQUIRED)
option(USE_SQLITE3 "Enable SQLite3 support" OFF)
if(USE_SQLITE3)
find_package(SQLite3 REQUIRED)
pkg_check_modules(SQLITE3 REQUIRED sqlite3)
add_definitions(-DUSE_SQLITE3)
endif()
add_executable(
auth_service
${SOURCES}
)
target_link_libraries(auth_service ssl crypto sqlite3 pthread)
if(USE_SQLITE3)
target_link_libraries(auth_service ssl crypto sqlite3 pthread)
else()
target_link_libraries(auth_service ssl crypto pthread)
endif()
+7 -1
View File
@@ -1,13 +1,19 @@
CC = gcc
CFLAGS = -Wall -g -std=gnu2x -DMG_TLS=MG_TLS_OPENSSL -ffunction-sections -fdata-sections
LDFLAGS = -Wl,--gc-sections
LIBS = -lpthread -lcrypto -lssl -lsqlite3
INCLUDES = -I./src/lib -I./src
SRCS = $(wildcard src/*/*.c src/*.c)
OBJS = $(SRCS:.c=.o)
TARGET = auth_service
BUILD_DIR=./build
CROSS_COMPILE =
LIBS = -lpthread -lcrypto -lssl
USE_SQLITE3 ?= 0
ifeq ($(USE_SQLITE3), 1)
CFLAGS += -DUSE_SQLITE3
LIBS += -lsqlite3
endif
all: $(TARGET)
+16 -4
View File
@@ -1,8 +1,20 @@
project('auth_service', 'c', default_options: ['c_std=gnu2x'])
include_dirs = include_directories('src', 'src/lib')
deps = [dependency('openssl'), dependency('sqlite3')]
extra_c_args = [ '-ffunction-sections', '-fdata-sections' ]
extra_c_link_args = [ '-Wl,--gc-sections' ]
deps = [dependency('openssl')]
extra_c_args = ['-ffunction-sections', '-fdata-sections']
extra_c_link_args = ['-Wl,--gc-sections']
sqlite3_dep = dependency('sqlite3', required: false)
use_sqlite3 = get_option('use_sqlite3')
if use_sqlite3
if not sqlite3_dep.found()
error('SQLite3 requested but not found')
endif
deps += sqlite3_dep
add_project_arguments('-DUSE_SQLITE3', language: 'c')
endif
executable(
'auth_service',
'src/main.c',
@@ -38,5 +50,5 @@ executable(
include_directories: include_dirs,
dependencies: deps,
c_args: extra_c_args,
link_args: extra_c_link_args
link_args: extra_c_link_args,
)
+1
View File
@@ -0,0 +1 @@
option('use_sqlite3', type: 'boolean', value: false, description: 'Enable SQLite3 support')
+17 -7
View File
@@ -7,20 +7,27 @@
#include "config.h"
#include "sql.h"
sqlite3* db = nullptr;
#ifdef USE_SQLITE3
sqlite3 *db = nullptr;
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
void init_db() {
#ifdef USE_SQLITE3
if (sqlite3_open(cm_get_parameter_as_string(config, "database"), &db) !=
SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
#else
return;
#endif
}
void migrate_db() {
char* err_msg = 0;
#ifdef USE_SQLITE3
char *err_msg = 0;
pthread_mutex_lock(&db_mutex);
if (sqlite3_exec(db, CREATE_TABLE_USERS, nullptr, nullptr, &err_msg) !=
SQLITE_OK) {
@@ -30,18 +37,21 @@ void migrate_db() {
exit(1);
}
pthread_mutex_unlock(&db_mutex);
#else
return;
#endif
}
// Вспомогательная функция для экранирования специальных символов в JSON
char* json_escape(const char* str) {
char *json_escape(const char *str) {
if (!str) return NULL;
size_t len = strlen(str);
// Выделяем память с запасом (каждый символ может быть экранирован)
char* escaped = malloc(len * 6 + 1);
char *escaped = malloc(len * 6 + 1);
if (!escaped) return NULL;
char* p = escaped;
char *p = escaped;
for (size_t i = 0; i < len; i++) {
switch (str[i]) {
case '\"':
@@ -74,7 +84,7 @@ char* json_escape(const char* str) {
break;
default:
if (str[i] < 0x20) {
sprintf(p, "\\u%04x", (unsigned char)str[i]);
sprintf(p, "\\u%04x", (unsigned char) str[i]);
p += 6;
} else {
*p++ = str[i];
@@ -84,4 +94,4 @@ char* json_escape(const char* str) {
*p = '\0';
return escaped;
}
}
+5 -3
View File
@@ -1,11 +1,13 @@
#pragma once
#include <pthread.h>
#include <sqlite3.h>
extern sqlite3* db;
#ifdef USE_SQLITE3
#include <sqlite3.h>
extern sqlite3 *db;
extern pthread_mutex_t db_mutex;
#endif
void init_db();
void migrate_db();
char* json_escape(const char* str);
char *json_escape(const char *str);
+4
View File
@@ -3,6 +3,7 @@
#include "user.h"
bool db_delete_user(const int id) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
@@ -42,4 +43,7 @@ bool db_delete_user(const int id) {
pthread_mutex_unlock(&db_mutex);
return true;
#else
return true;
#endif
}
+8 -4
View File
@@ -5,6 +5,7 @@
#include "user.h"
char *db_get_all_users() {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Database not initialized\n");
@@ -41,9 +42,9 @@ char *db_get_all_users() {
const unsigned char *group = sqlite3_column_text(stmt, 4);
int is_superuser = sqlite3_column_int(stmt, 5);
char *escaped_username = json_escape((const char *)username);
char *escaped_email = json_escape((const char *)email);
char *escaped_group = json_escape((const char *)group);
char *escaped_username = json_escape((const char *) username);
char *escaped_email = json_escape((const char *) email);
char *escaped_group = json_escape((const char *) group);
if (!escaped_username || !escaped_email || !escaped_group) {
free(escaped_username);
free(escaped_email);
@@ -97,4 +98,7 @@ char *db_get_all_users() {
sqlite3_finalize(stmt);
pthread_mutex_unlock(&db_mutex);
return result;
}
#else
return "";
#endif
}
+7 -2
View File
@@ -3,6 +3,7 @@
#include "user.h"
User *db_get_user_by_id(const int id) {
#ifdef USE_SQLITE3
User *user = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
@@ -27,8 +28,8 @@ User *db_get_user_by_id(const int id) {
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
int db_is_superuser = sqlite3_column_int(stmt, 5);
user = new_user(id, (char *)db_username, (char *)db_password,
(char *)db_email, (char *)db_group, db_is_superuser);
user = new_user(id, (char *) db_username, (char *) db_password,
(char *) db_email, (char *) db_group, db_is_superuser);
}
if (rc != SQLITE_DONE) {
@@ -41,4 +42,8 @@ User *db_get_user_by_id(const int id) {
pthread_mutex_unlock(&db_mutex);
return user;
#else
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
return asdf;
#endif
}
+7 -2
View File
@@ -3,6 +3,7 @@
#include "user.h"
User *db_get_user_by_username(const char *username) {
#ifdef USE_SQLITE3
User *user = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
@@ -28,8 +29,8 @@ User *db_get_user_by_username(const char *username) {
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
int db_is_superuser = sqlite3_column_int(stmt, 5);
user = new_user(id, (char *)db_username, (char *)db_password,
(char *)db_email, (char *)db_group, db_is_superuser);
user = new_user(id, (char *) db_username, (char *) db_password,
(char *) db_email, (char *) db_group, db_is_superuser);
}
if (rc != SQLITE_DONE) {
@@ -41,4 +42,8 @@ User *db_get_user_by_username(const char *username) {
pthread_mutex_unlock(&db_mutex);
return user;
#else
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
return asdf;
#endif
}
+6 -2
View File
@@ -5,6 +5,7 @@
#include "user.h"
char *db_get_user_field_by_id(const int id, const char *field) {
#ifdef USE_SQLITE3
char *password = nullptr;
pthread_mutex_lock(&db_mutex);
if (!db) {
@@ -25,9 +26,9 @@ char *db_get_user_field_by_id(const int id, const char *field) {
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
const unsigned char *db_password = sqlite3_column_text(stmt, 0);
password = malloc(strlen((const char *)db_password) + 1);
password = malloc(strlen((const char *) db_password) + 1);
if (password) {
strcpy(password, (const char *)db_password);
strcpy(password, (const char *) db_password);
}
sqlite3_finalize(stmt);
} else if (rc == SQLITE_DONE) {
@@ -43,4 +44,7 @@ char *db_get_user_field_by_id(const int id, const char *field) {
}
pthread_mutex_unlock(&db_mutex);
return password;
#else
return "";
#endif
}
+1 -1
View File
@@ -1,5 +1,5 @@
#include <cJSON.h>
#include <sqlite3.h>
// #include <sqlite3.h>
#include "file.h"
#include "user.h"
+5
View File
@@ -5,6 +5,7 @@
User *db_insert_user(const char *username, const char *email,
const char *password, const char *group,
const bool *is_superuser) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Databse not initialized\n");
@@ -60,4 +61,8 @@ User *db_insert_user(const char *username, const char *email,
User *new_db_user =
new_user(inserted_id, username, email, password, group, is_superuser);
return new_db_user;
#else
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
return asdf;
#endif
}
+5
View File
@@ -5,6 +5,7 @@
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
pthread_mutex_lock(&db_mutex);
if (!db) {
fprintf(stderr, "Databse not initialized\n");
@@ -52,4 +53,8 @@ User *db_update_user(const int userid, const char *username, const char *email,
}
pthread_mutex_unlock(&db_mutex);
return new_user(userid, username, email, password, group, is_superuser);
#else
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
return asdf;
#endif
}
+4
View File
@@ -3,6 +3,7 @@
#include "user.h"
bool db_update_user_field(const int id, const char *field, const char *value) {
#ifdef USE_SQLITE3
pthread_mutex_lock(&db_mutex);
if (!db || !field || !value) {
fprintf(stderr, "invalid parameters\n");
@@ -32,4 +33,7 @@ bool db_update_user_field(const int id, const char *field, const char *value) {
}
pthread_mutex_unlock(&db_mutex);
return sqlite3_changes(db) > 0;
#else
return 0;
#endif
}
+4 -1
View File
@@ -1,6 +1,9 @@
#include <colors.h>
#include <mongoose.h>
#ifdef USE_SQLITE3
#include <sqlite3.h>
#endif
#include "config.h"
#include "cors.h"
@@ -67,7 +70,7 @@ int main(void) {
migrate_db();
const char *default_users_filename =
cm_get_parameter_as_string(config, "default_users_file");
if (default_users_filename) db_get_users_from_file("users.json");
if (default_users_filename) db_get_users_from_file(default_users_filename);
struct mg_mgr mgr;
mg_log_set(MG_LL_DEBUG);
+3
View File
@@ -2,7 +2,10 @@
#include <cJSON.h>
#include <mongoose.h>
#ifdef USE_SQLITE3
#include <sqlite3.h>
#endif
#define HTTP_HEADERS \
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n"