Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Example 03 Integration with vibe.d

The example shows how to create vibe.d daemon. The example http server binds to localhost:8080 and responds with simple hello-world page.


import dlogg.strict;
import daemonize.d;

import vibe.core.core;
import vibe.core.log;
import vibe.core.log : VibeLogLevel = LogLevel;
import vibe.http.server;

Daemon description from example01, the main difference is that you don't need manually stop daemon via false return. The exitEventLoop will stop main execution.

alias daemon = Daemon!(
    "DaemonizeExample3", // unique name
        Composition!(Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop), (logger)
            // No need to force exit here
            // main will stop after the call 
            return true; 
        Signal.HangUp, (logger)
            logger.logInfo("Hello World!");
            return true;
    // Default vibe initialization should be performed after daemon forking as inner vibe resources are thread local
    (logger, shouldExit) {
        // Default vibe initialization
        auto settings = new HTTPServerSettings;
        settings.port = 8080;
        settings.bindAddresses = [""];

        listenHTTP(settings, &handleRequest);

        // All exceptions are caught by daemonize
        return runEventLoop();

The simpliest vibe handler that responds with "Hello World!" to any request:

void handleRequest(HTTPServerRequest req,
                   HTTPServerResponse res)
    res.writeBody("Hello World!", "text/plain");

The most important part is to configure vibe logger or you will get 'Invalid file handle' error at application initialization:

int main()
    // Setting vibe logger 
    // daemon closes stdout/stderr and vibe logger will crash
    // if not suppress printing to console
    version(Windows) enum vibeLogName = "C:\\vibe.log";
    else enum vibeLogName = "vibe.log";

    // no stdout/stderr output
    version(Windows) {}
    else setLogLevel(VibeLogLevel.none);


    version(Windows) enum logFileName = "C:\\logfile.log";
    else enum logFileName = "logfile.log";

    auto logger = new shared DloggLogger(logFileName);
    return buildDaemon!;
You can’t perform that action at this time.