ServerConfig.cpp 2.8 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "ServerConfig.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

#include "config/IConfigMgr.h"

namespace zilliz {
namespace vecwise {
namespace server {

G
groot 已提交
20 21 22 23
static const std::string CONFIG_ADDRESS = "address";
static const std::string CONFIG_PORT = "port";

ServerConfig&
G
groot 已提交
24 25
ServerConfig::GetInstance() {
    static ServerConfig config;
G
groot 已提交
26
    return config;
G
groot 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}

ServerError
ServerConfig::LoadConfigFile(const std::string& config_filename) {
    std::string filename = config_filename;
    if(filename.empty()){
        std::cout << "ERROR: a config file is required" << std::endl;
        exit(1);//directly exit program if config file not specified
    }
    struct stat directoryStat;
    int statOK = stat(filename.c_str(), &directoryStat);
    if (statOK != 0) {
        std::cout << "ERROR: " << filename << " not found!" << std::endl;
        exit(1);//directly exit program if config file not found
    }

    try {
        IConfigMgr* mgr = const_cast<IConfigMgr*>(IConfigMgr::GetInstance());
        ServerError err = mgr->LoadConfigFile(filename);
        if(err != 0) {
            std::cout << "Server failed to load config file" << std::endl;
            exit(1);//directly exit program if the config file is illegal
        }
    }
    catch (YAML::Exception& e) {
        std::cout << "Server failed to load config file: " << std::endl;
        return SERVER_UNEXPECTED_ERROR;
    }

    return SERVER_SUCCESS;
}

void
ServerConfig::PrintAll() const {
    if(const IConfigMgr* mgr = IConfigMgr::GetInstance()) {
        std::string str = mgr->DumpString();
//        SERVER_LOG_INFO << "\n" << str;
        std::cout << "\n" << str << std::endl;
    }
}

ConfigNode
G
groot 已提交
69
ServerConfig::GetConfig(const std::string& name) const {
G
groot 已提交
70 71
    const IConfigMgr* mgr = IConfigMgr::GetInstance();
    const ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
72
    return root_node.GetChild(name);
G
groot 已提交
73 74 75
}

ConfigNode&
G
groot 已提交
76
ServerConfig::GetConfig(const std::string& name) {
G
groot 已提交
77 78
    IConfigMgr* mgr = IConfigMgr::GetInstance();
    ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
79
    return root_node.GetChild(name);
G
groot 已提交
80 81 82 83
}

std::string
ServerConfig::GetServerAddress() const {
G
groot 已提交
84
    ConfigNode server_config = GetConfig(CONFIG_SERVER);
G
groot 已提交
85 86 87 88 89
    return server_config.GetValue(CONFIG_ADDRESS);
}

std::string
ServerConfig::GetServerPort() const {
G
groot 已提交
90
    ConfigNode server_config = GetConfig(CONFIG_SERVER);
G
groot 已提交
91 92 93 94 95 96 97
    return server_config.GetValue(CONFIG_PORT);
}


}
}
}