云备份服务端——配置信息及获取配置信息类模块

一,配置信息

使用文件配置加载一些程序的运行关键信息,如ip,端口等,可以让程序的运行更加灵活

 我们需要的配置信息如下

  •  IP地址
  • 端口号
  • 热点判断时间,也就是非热点文件的时间要求
  • 文件下载的URL前缀路径,用于表示客户端请求是下载请求,避免查看请求和下载文件同名导致冲突
  • 压缩包后缀
  • 上传文件存放路径,文件上传后的存储位置
  • 非热点文件压缩包存放路径,非热点文件压缩后存储位置,注意不是压缩包(用户可能上传压缩包)
  • 服务端备份文件信息存储文件:服务器所有备份的文件的详细信息表存放的位置
{
    "server_ip" : "124.221.185.180",
    "server_port" : 8080,
    "hot_time" : 30,
    "url_prefix" : "/download/",
    "lz_prefix" : ".lz",
    "pack_dir" : "./packdir/",
    "back_dir" : "./backdir/",
    "manager_file" : "./files.dat"
}

配置文件的编写注完全遵照Json序列化,后续读取时进行反序列化即可

二,获取配置文件信息单例类

该类主要是一个单例设计模式,用于加载配置信息,并且对外提供获取对应配置信息的接口。

其主要实现接口如下

namespace mjw_cloud
{
    #define config_file "./cloud.conf"
    class Config
    {
        public:
        Config* GetInstance();
        time_t GetHotTime();
        int GetServerPort();
        std::string GetServerIp();
        std::string GetUrlPrefix();
        std::string GetLzPrefix();
        std::string GetPackDir();
        std::string GetBackDir();
        std::string GetManagerFile();
        
        private:
        bool GetConfig();//加载配置信息
        private:
        Config();
        Config(const Config& con);
        const Config& operator=(const Config& con);

        private:
        time_t _hot_time;//热点时间
        int _server_port;//服务器端口号
        std::string _server_ip;//服务器ip地址
        std::string _url_prefix;//url下载后缀
        std::string _lz_prefix;//非热点文件压缩后缀
        std::string _pack_dir;//下载文件路径
        std::string _back_dir;//压缩文件路径
        std::string _manager_file;//备份文件信息管理表文件
    };
}

代码实现如下

#define config_file "./cloud.conf"
    class Config
    {
    public:
        static Config &GetInstance()
        {
            // C++11 后利用局部静态变量特性实现单例可保证线程安全
            static Config _eton;
            return _eton;
        }
        time_t GetHotTime()
        {
            return _hot_time;
        }
        int GetServerPort()
        {
            return _server_port;
        }
        std::string GetServerIp()
        {
            return _server_ip;
        }
        std::string GetUrlPrefix()
        {
            return _url_prefix;
        }
        std::string GetLzPrefix()
        {
            return _lz_prefix;
        }
        std::string GetPackDir()
        {
            return _pack_dir;
        }
        std::string GetBackDir()
        {
            return _back_dir;
        }
        std::string GetManagerFile() 
        {
            return _manager_file;
        }
        private:
            bool GetConfig() // 加载配置信息
            {
                FileUtil fu(config_file);
                // 读取cloud.conf的序列化配置信息
                std::string conf;
                if (fu.GetContent(&conf) == false)
                {
                    std::cout << "get config failed" << std::endl;
                    return false;
                }
                Json::Value root;
                // 反序列化
                JsonUtil::UnSerialize(conf, &root);
                // 读取root内容
                _hot_time = root["hot_time"].asInt();            // 热点时间
                _server_port = root["server_port"].asInt();      // 服务器端口号
                _server_ip = root["server_ip"].asString();       // 服务器ip地址
                _url_prefix = root["url_prefix"].asString();     // url下载后缀
                _lz_prefix = root["lz_prefix"].asString();       // 非热点文件压缩后缀
                _pack_dir = root["pack_dir"].asString();         // 下载文件路径
                _back_dir = root["back_dir"].asString();         // 压缩文件路径
                _manager_file = root["manager_file"].asString(); // 备份文件信息管理表文件

                return true;
            }

        private:
            Config()
            {
                if (GetConfig() == false)
                {
                    std::cout << "read config failed" << std::endl;
                    abort();
                }
            }
            Config(const Config &con);
            const Config &operator=(const Config &con);

        private:
            time_t _hot_time;          // 热点时间
            int _server_port;          // 服务器端口号
            std::string _server_ip;    // 服务器ip地址
            std::string _url_prefix;   // url下载后缀
            std::string _lz_prefix;    // 非热点文件压缩后缀
            std::string _pack_dir;     // 下载文件路径
            std::string _back_dir;     // 压缩文件路径
            std::string _manager_file; // 备份文件信息管理表文件
        };

 测试用例

#include "util.hpp"
#include "config.hpp"
int main()
{   
    std::cout<<mjw_cloud::Config::GetInstance().GetHotTime()<<std::endl;
    std::cout<<mjw_cloud::Config::GetInstance().GetServerPort()<<std::endl;
    std::cout<<mjw_cloud::Config::GetInstance().GetServerIp()<<std::endl;
    std::cout<<mjw_cloud::Config::GetInstance().GetUrlPrefix()<<std::endl;
    std::cout<<mjw_cloud::Config::GetInstance().GetPackDir()<<std::endl;
    std::cout<<mjw_cloud::Config::GetInstance().GetBackDir()<<std::endl;
    std::cout<<mjw_cloud::Config::GetInstance().GetManagerFile()<<std::endl;


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值