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