From 42e477098cef902daf42829e47f69e4e0c2b4c77 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 22 Sep 2025 11:34:43 +0200 Subject: [PATCH] aqhome-cgi: added code to init aqhome-cgi environment. --- apps/aqhome-cgi/0BUILD | 2 + apps/aqhome-cgi/main.c | 107 +++++++++++++++++++++++++++++++---------- 2 files changed, 83 insertions(+), 26 deletions(-) diff --git a/apps/aqhome-cgi/0BUILD b/apps/aqhome-cgi/0BUILD index b2ab60d..12c94a1 100644 --- a/apps/aqhome-cgi/0BUILD +++ b/apps/aqhome-cgi/0BUILD @@ -21,6 +21,8 @@ --include=$(srcdir) + + $(visibility_cflags) diff --git a/apps/aqhome-cgi/main.c b/apps/aqhome-cgi/main.c index fdef593..096f7f6 100644 --- a/apps/aqhome-cgi/main.c +++ b/apps/aqhome-cgi/main.c @@ -2,6 +2,10 @@ #include "./service_file.h" #include "aqhome-cgi/modules/mroot.h" +#include "aqhome-cgi/modules/common/madmin.h" +#include "aqhome-cgi/modules/common/mmodules.h" +#include "aqhome-cgi/modules/common/musers.h" +#include "aqhome-cgi/modules/mdevices.h" #include "aqhome/aqhome.h" #include @@ -20,22 +24,31 @@ #define AQHOME_CGI_LOGFILE "/var/www/aqhome-cgi/log/aqhome-cgi.log" -#define AQHOME_CGI_DEFAULT_STATIC_FILES "0-build/services/static" -#define AQHOME_CGI_DEFAULT_RUNTIME_FILES "0-build/services/runtime" +#define AQHOME_CGI_DEFAULT_STATIC_FILES AQHOME_CGI_WWWDIR"/static" +#define AQHOME_CGI_DEFAULT_RUNTIME_FILES AQHOME_CGI_WWWDIR"/data" -void _handleRequest(AQCGI_REQUEST *rq, const char *sPathStaticFiles, const char *sPathRuntimeFiles); -int _handlePath(AQH_SERVICE *sv, AQCGI_REQUEST *rq, const char *sPathStaticFiles); -AQH_MODULE *_loadModule(AQH_SERVICE *sv, AQCGI_REQUEST *rq, AQH_MODULE *mParent, const char *sModuleName); +static void _handleRequest(AQCGI_REQUEST *rq, const char *sPathStaticFiles, const char *sPathRuntimeFiles); +static int _handlePath(AQH_SERVICE *sv, AQCGI_REQUEST *rq, const char *sPathStaticFiles); static void logStart(void); +static int _init(const char *sPathRuntimeFiles); int main(int argc, char **argv) { GWEN_GUI *gui; - AQCGI_REQUEST *rq; + const char *sPathStaticFiles; + const char *sPathRuntimeFiles; + + sPathStaticFiles=getenv("AQHOME_STATIC_FILES"); + if (!(sPathStaticFiles && *sPathStaticFiles)) + sPathStaticFiles=AQHOME_CGI_DEFAULT_STATIC_FILES; + + sPathRuntimeFiles=getenv("AQHOME_RUNTIME_FILES"); + if (!(sPathRuntimeFiles && *sPathRuntimeFiles)) + sPathRuntimeFiles=AQHOME_CGI_DEFAULT_RUNTIME_FILES; GWEN_Init(); gui=GWEN_NoGui_new(); @@ -51,9 +64,6 @@ int main(int argc, char **argv) GWEN_Logger_SetLevel(AQCGI_LOGDOMAIN, GWEN_LoggerLevel_Debug); GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Debug); - DBG_ERROR(NULL, "Init CGI"); - AQCGI_Init(); - GWEN_Logger_Close(GWEN_LOGDOMAIN); GWEN_Logger_Open(GWEN_LOGDOMAIN, "gwenhywfar", AQHOME_CGI_LOGFILE, GWEN_LoggerType_File, GWEN_LoggerFacility_Daemon); @@ -61,28 +71,33 @@ int main(int argc, char **argv) GWEN_Logger_SetLevel(AQCGI_LOGDOMAIN, GWEN_LoggerLevel_Debug); GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Debug); - rq=AQCGI_ReadRequest(); - if (rq) { - const char *sPathStaticFiles; - const char *sPathRuntimeFiles; + if (argc>1 && argv[1]) { + if (0==strcasecmp(argv[1], "init")) { + int rv; - sPathStaticFiles=getenv("AQHOME_STATIC_FILES"); - if (!(sPathStaticFiles && *sPathStaticFiles)) - sPathStaticFiles=AQHOME_CGI_DEFAULT_STATIC_FILES; + rv=_init(sPathRuntimeFiles); + if (rv<0) { + fprintf(stderr, "Error on init (%d)\n", rv); + return 2; + } - sPathRuntimeFiles=getenv("AQHOME_RUNTIME_FILES"); - if (!(sPathRuntimeFiles && *sPathRuntimeFiles)) - sPathRuntimeFiles=AQHOME_CGI_DEFAULT_RUNTIME_FILES; - - _handleRequest(rq, sPathStaticFiles, sPathRuntimeFiles); + } } else { - fprintf(stdout, "Content-type: text/plain\n\n"); - fprintf(stdout, "Error: No Request!\n"); - return 0; - } + AQCGI_REQUEST *rq; - AQCGI_Fini(); + DBG_ERROR(NULL, "Init CGI"); + AQCGI_Init(); + rq=AQCGI_ReadRequest(); + if (rq) { + _handleRequest(rq, sPathStaticFiles, sPathRuntimeFiles); + } + else { + fprintf(stdout, "Content-type: text/plain\n\n"); + fprintf(stdout, "Error: No Request!\n"); + } + AQCGI_Fini(); + } return 0; } @@ -184,5 +199,45 @@ void logStart() +int _init(const char *sPathRuntimeFiles) +{ + AQH_SERVICE *sv; + int rv; + + fprintf(stdout, "Creating aqhome-cgi environment in \"%s\"\n", sPathRuntimeFiles); + sv=AQH_ServiceFiles_new(sPathRuntimeFiles); + rv=AQH_ModAdmin_Create(sv); + if (rv<0) { + DBG_ERROR(NULL, "Error creating module \"admin\""); + AQH_Service_free(sv); + return rv; + } + + rv=AQH_ModAdmModules_Create(sv); + if (rv<0) { + DBG_ERROR(NULL, "Error creating module \"modules\""); + AQH_Service_free(sv); + return rv; + } + + rv=AQH_ModAdmUsers_Create(sv); + if (rv<0) { + DBG_ERROR(NULL, "Error creating module \"users\""); + AQH_Service_free(sv); + return rv; + } + + rv=AQH_ModDevices_Create(sv); + if (rv<0) { + DBG_ERROR(NULL, "Error creating module \"devices\""); + AQH_Service_free(sv); + return rv; + } + + AQH_Service_free(sv); + return 0; +} + +