Beispiel #1
0
int main(int argc, char *argv[])
{
    auto config_file = Yb::StrUtils::xgetenv("CONFIG_FILE");
    if (!config_file.size())
        config_file = "/etc/card_proxy_keykeeper2/card_proxy_keykeeper2.cfg.xml";
    Yb::ILogger::Ptr logger;
    try {
        theApp::instance().init(
                IConfig::Ptr(new XmlConfig(config_file)),
                false);
        logger.reset(theApp::instance().new_logger("main").release());
    }
    catch (const std::exception &ex) {
        std::cerr << "exception: " << ex.what() << "\n";
        return 1;
    }
    try {
        std::string bind_host = theApp::instance().cfg()
            .get_value("HttpListener/Host");
        int bind_port = theApp::instance().cfg()
            .get_value_as_int("HttpListener/Port");
        std::string listen_at = "http://" + bind_host + ":"
                + Yb::to_string(bind_port) + "/";
        logger->error("listen at: " + listen_at);
        key_keeper = new KeyKeeper(theApp::instance().cfg(), *logger);
        const std::string prefix = "/" + theApp::instance().cfg()
            .get_value("HttpListener/Prefix");
        Yb::String secret = theApp::instance().cfg().get_value("KK2Secret");

        XmlHttpWrapper handlers_array[] = {
            WRAP("/", ping),
            SECURE_WRAP(prefix, secret, read),
            SECURE_WRAP(prefix, secret, get),
            SECURE_WRAP(prefix, secret, write),
            SECURE_WRAP(prefix, secret, set),
            SECURE_WRAP(prefix, secret, unset),
            SECURE_WRAP(prefix, secret, cleanup),
        };
        int n_handlers = sizeof(handlers_array)/sizeof(handlers_array[0]);
        typedef HttpServer<XmlHttpWrapper> MyHttpServer;
        MyHttpServer::HandlerMap handlers;
        for (int i = 0; i < n_handlers; ++i) {
            std::string prefix = handlers_array[i].prefix();
            handlers[prefix + handlers_array[i].name()] = handlers_array[i];
        }
        std::string error_content_type = "application/xml";
        std::string error_body = "<result><status>internal_error</status></result>";
        MyHttpServer server(
                bind_host, bind_port, 30, handlers, &theApp::instance(),
                error_content_type, error_body);
        server.serve();
    }
    catch (const std::exception &ex) {
        logger->error(std::string("exception: ") + ex.what());
        return 1;
    }
    return 0;
}
Beispiel #2
0
inline int run_server_app(const std::string &config_name,
        HttpHandler *handlers_array, int n_handlers)
{
    randomize();
    Yb::ILogger::Ptr logger;
    try {
        theApp::instance().init(IConfig::Ptr(new XmlConfig(config_name)));
        logger.reset(theApp::instance().new_logger("main").release());
    }
    catch (const std::exception &ex) {
        std::cerr << "exception: " << ex.what() << "\n";
        return 1;
    }
    try {
        std::string bind_host = theApp::instance().cfg()
            .get_value("HttpListener/Host");
        int bind_port = theApp::instance().cfg()
            .get_value_as_int("HttpListener/Port");
        std::string listen_at = "http://" + bind_host + ":"
                + Yb::to_string(bind_port) + "/";
        logger->error("listen at: " + listen_at);
        typedef HttpServer<HttpHandler> MyHttpServer;
        typename MyHttpServer::HandlerMap handlers;
        for (int i = 0; i < n_handlers; ++i) {
            std::string prefix = handlers_array[i].prefix();
            handlers[prefix + handlers_array[i].name()] = handlers_array[i];
        }
        std::string error_content_type = "text/xml";
        std::string error_body = "{\"status\": \"internal_error\"}";
        MyHttpServer server(
                bind_host, bind_port, 30, handlers, &theApp::instance(),
                error_content_type, error_body);
        server.serve();
    }
    catch (const std::exception &ex) {
        logger->error(std::string("exception: ") + ex.what());
        return 1;
    }
    return 0;
}