Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:config file #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 这是一个注释,目前只能单独一行
# 自动忽略空格,格式为 key=value
# key value 类型默认是 std::string

user = root
passwd = root
databasename = user_db
38 changes: 38 additions & 0 deletions config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,42 @@ void Config::parse_arg(int argc, char*argv[]){
break;
}
}
}

void Config::load_conf()
{
FILE *conf_file = fopen("conf", "r");
assert(conf_file);
char buf[255];
while (fgets(buf, 255, conf_file) != NULL)
{
std::string line(buf);
bool is_comment= false;

for (auto ite = line.begin(); ite != line.end();)
{
if (*ite == ' ' || *ite == '\n' || *ite == '\t' || *ite == '\r')
{
line.erase(ite);
}
else if (*ite == '#')
{
is_comment = true;
break;
}
else
{
ite++;
}
}
if (!is_comment)
{
int pos = line.find_first_of("=");
if (pos != std::string::npos)
{
m_conf_map.insert({line.substr(0, pos), line.substr(pos + 1)});
}
}
}
fclose(conf_file);
}
9 changes: 6 additions & 3 deletions config.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef CONFIG_H
#define CONFIG_H

#include "webserver.h"

using namespace std;
#include <stdlib.h>
#include <unistd.h>
#include <cassert>
#include <map>

class Config
{
Expand All @@ -12,6 +13,8 @@ class Config
~Config(){};

void parse_arg(int argc, char*argv[]);
void load_conf();
std::map<std::string, std::string> m_conf_map;

//端口号
int PORT;
Expand Down
36 changes: 3 additions & 33 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
#include "config.h"
#include "webserver.h"

int main(int argc, char *argv[])
{
//需要修改的数据库信息,登录名,密码,库名
string user = "root";
string passwd = "root";
string databasename = "qgydb";

//命令行解析
Config config;
config.parse_arg(argc, argv);

WebServer server;

//初始化
server.init(config.PORT, user, passwd, databasename, config.LOGWrite,
config.OPT_LINGER, config.TRIGMode, config.sql_num, config.thread_num,
config.close_log, config.actor_model);


//日志
server.log_write();

//数据库
server.sql_pool();

//线程池
server.thread_pool();

//触发模式
server.trig_mode();

//监听
server.eventListen();

//运行
server.eventLoop();
server.init(argc,argv);
server.run();

return 0;
}
54 changes: 53 additions & 1 deletion webserver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "webserver.h"
#include "config.h"

WebServer::WebServer()
WebServer::WebServer() : m_init_module(false)
{
//http_conn类对象
users = new http_conn[MAX_FD];
Expand All @@ -15,6 +16,8 @@ WebServer::WebServer()

//定时器
users_timer = new client_data[MAX_FD];

m_config = std::make_shared<Config>(Config());
}

WebServer::~WebServer()
Expand All @@ -28,6 +31,33 @@ WebServer::~WebServer()
delete m_pool;
}

//读取配置,初始化参数
void WebServer::init(int argc,char *argv[])
{
m_config->parse_arg(argc,argv);

m_port = m_config->PORT ;
m_log_write = m_config->LOGWrite;
m_close_log = m_config->close_log;
m_OPT_LINGER = m_config->OPT_LINGER;
m_TRIGMode = m_config->TRIGMode;
m_actormodel = m_config->actor_model;

m_sql_num = m_config->sql_num;
m_thread_num =m_config->thread_num;

//加载配置文件
m_config->load_conf();
auto &config = m_config->m_conf_map;

m_user = config["user"];
m_passWord = config["passwd"];
m_databaseName = config["databasename"];

//初始化各个模块
m_init_module = init_module();
}

void WebServer::init(int port, string user, string passWord, string databaseName, int log_write,
int opt_linger, int trigmode, int sql_num, int thread_num, int close_log, int actor_model)
{
Expand All @@ -42,6 +72,7 @@ void WebServer::init(int port, string user, string passWord, string databaseName
m_TRIGMode = trigmode;
m_close_log = close_log;
m_actormodel = actor_model;
m_init_module = init_module();
}

void WebServer::trig_mode()
Expand Down Expand Up @@ -432,3 +463,24 @@ void WebServer::eventLoop()
}
}
}

bool WebServer::init_module()
{
// 日志
log_write();
// 数据库
sql_pool();
// 线程池
thread_pool();
// 触发模式
trig_mode();
// 监听
eventListen();

return true;
}

void WebServer::run()
{
m_init_module ? eventLoop() : perror("Module initialization failed");
}
12 changes: 12 additions & 0 deletions webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <stdlib.h>
#include <cassert>
#include <sys/epoll.h>
#include <memory>

#include "config.h"
#include "./threadpool/threadpool.h"
#include "./http/http_conn.h"

Expand All @@ -28,7 +30,10 @@ class WebServer
void init(int port , string user, string passWord, string databaseName,
int log_write , int opt_linger, int trigmode, int sql_num,
int thread_num, int close_log, int actor_model);
void init(int argc, char *argv[]);
void run();

private:
void thread_pool();
void sql_pool();
void log_write();
Expand All @@ -42,6 +47,7 @@ class WebServer
bool dealwithsignal(bool& timeout, bool& stop_server);
void dealwithread(int sockfd);
void dealwithwrite(int sockfd);
bool init_module();

public:
//基础
Expand Down Expand Up @@ -78,5 +84,11 @@ class WebServer
//定时器相关
client_data *users_timer;
Utils utils;

//配置类
std::shared_ptr<Config> m_config;

//初始化模块
bool m_init_module;
};
#endif