Commit 3a39f996 authored by jan.koester's avatar jan.koester
Browse files

test

parent b0beadf3
Loading
Loading
Loading
Loading
+7
Changes for src/CMakeLists.txt: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -53,9 +53,16 @@ include_directories(
#
#)

if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
    target_link_libraries(blogi PUBLIC
        blogidev
        kernel32.lib
    )
else()
    target_link_libraries(blogi PUBLIC
        blogidev
    )
endif()

install(
    TARGETS blogi  DESTINATION bin RUNTIME_DEPENDENCY_SET blogidyn
+142 −4
Changes for src/blogi.cpp: 142 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#else
#include <unistd.h>
#include <fcntl.h>
@@ -655,6 +657,7 @@ void logFiles(const char *path,int fd){
}
#endif

#ifndef Windows
int main(int argc, char* argv[]) {
    cmdplus::CmdController *BlogiCmdCtl;
    BlogiCmdCtl = &cmdplus::CmdController::getInstance();
@@ -687,7 +690,7 @@ int main(int argc, char* argv[]) {
	blogi::Blogi::Config = std::make_shared<blogi::Config>(config);

    try{
#ifndef Windows


        struct sigaction SignalAction = {0};
        /* Ignore broken pipes, which can happen when the remote side is not behaving well */
@@ -720,9 +723,6 @@ int main(int argc, char* argv[]) {
        }else{
            HttpConD blogiD(true);
        }
#else
       HttpConD blogiD(false);
#endif
    }catch(libhttppp::HTTPException &e){
        std::cerr << e.what() << std::endl;
        return -1;
@@ -730,5 +730,143 @@ int main(int argc, char* argv[]) {
    return 0;
    // delete cins;
}
#endif

#ifdef Windows
SERVICE_STATUS g_ServiceStatus = { 0 };
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;

VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
VOID WINAPI ServiceCtrlHandler(DWORD);

#define SERVICE_NAME _T(LPSTR("BlogiService"))
#define CFGPATH "yaml:.\\..\\config.yaml"

int _tmain(int argc, TCHAR* argv[]) {
    SERVICE_TABLE_ENTRY ServiceTable[] = {
        {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
        {NULL, NULL}
    };
    
    if (StartServiceCtrlDispatcher(ServiceTable) == FALSE) {
        // This is where you would handle the case where the application is
        // not run as a service (e.g., in debug mode or from the command line).
        // You can check the error code to differentiate.
        DWORD err = GetLastError();
        if (err == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
            cmdplus::CmdController* BlogiCmdCtl;
            BlogiCmdCtl = &cmdplus::CmdController::getInstance();

            BlogiCmdCtl->registerCmd("help", 'h', false, (const char*) nullptr, "Helpmenu");

            if (BlogiCmdCtl->getCmdbyKey("help") && BlogiCmdCtl->getCmdbyKey("help")->getFound()) {
                BlogiCmdCtl->printHelp();
                return 0;
            }

            BlogiCmdCtl->registerCmd("config", 'c', true, (const char*) nullptr, "Blogi Config File");
            BlogiCmdCtl->registerCmd("debug", 'd', false, (int)0, "Blogi runs Debugmod");
            BlogiCmdCtl->registerCmd("foreground", 'F', false, (int)0, "Blogi runs Foreground");

            BlogiCmdCtl->parseCmd(argc, argv);

            const char* config = BlogiCmdCtl->getCmdbyKey("config")->getValue();
            int debug = BlogiCmdCtl->getCmdbyKey("debug")->getValueInt();

            if (!config)
                return -1;

            blogi::Blogi::Config = std::make_shared<blogi::Config>(config);

            HttpConD blogiD(debug);
        }
        else {
            // Handle other errors.
            return err;
        }
    }
    return 0;
}

// Function to report service status
VOID ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint) {
    static DWORD dwCheckPoint = 1;
    g_ServiceStatus.dwCurrentState = dwCurrentState;
    g_ServiceStatus.dwWin32ExitCode = dwWin32ExitCode;
    g_ServiceStatus.dwWaitHint = dwWaitHint;

    if (dwCurrentState == SERVICE_RUNNING || dwCurrentState == SERVICE_STOPPED) {
        g_ServiceStatus.dwCheckPoint = 0;
    }
    else {
        g_ServiceStatus.dwCheckPoint = dwCheckPoint++;
    }

    SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
}

// Service control handler
VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode) {
    switch (CtrlCode) {
    case SERVICE_CONTROL_STOP:
        ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
        if (g_ServiceStopEvent) {
            SetEvent(g_ServiceStopEvent);
        }
        return;
    case SERVICE_CONTROL_INTERROGATE:
        // Just report the current status
        ReportSvcStatus(g_ServiceStatus.dwCurrentState, NO_ERROR, 0);
        return;
    default:
        break;
    }
}

// Main service entry point
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) {
    // 1. Register a service control handler
    g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
    if (g_StatusHandle == NULL) {
        return;
    }

    // Initialize service status
    g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    ReportSvcStatus(SERVICE_START_PENDING, NO_ERROR, 3000);

    // 2. Create the stop event
    g_ServiceStopEvent = CreateEvent(
        NULL,    // default security attributes
        TRUE,    // manual reset event
        FALSE,   // not signaled
        NULL);   // no name

    if (g_ServiceStopEvent == NULL) {
        ReportSvcStatus(SERVICE_STOPPED, GetLastError(), 0);
        return;
    }

    // 3. Service initialization and configuration
    try {
        blogi::Blogi::Config = std::make_shared<blogi::Config>(CFGPATH);
        ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);

        // Run the server and wait for the stop event to be signaled
        HttpConD blogiD(false);
    }
    catch (const libhttppp::HTTPException& ex) {
        // Log the exception message to the event log
        // (You would add proper event logging here)
        ReportSvcStatus(SERVICE_STOPPED, ERROR_SERVICE_SPECIFIC_ERROR, 0);
        return;
    }

    // 4. Cleanup and report stop status
    CloseHandle(g_ServiceStopEvent);
    ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
}

#endif