Skip to content

Commit 3e4423f

Browse files
committed
added config parser
1 parent 3e5fd44 commit 3e4423f

File tree

9 files changed

+83
-14
lines changed

9 files changed

+83
-14
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ set(CMAKE_CXX_STANDARD 14)
77
include_directories(include/)
88

99
file(GLOB SOURCES src/*.cc src/*.c include/*)
10-
add_executable(http-server ${SOURCES})
10+
add_executable(http-server ${SOURCES} include/Config.hh src/Config.cc)

conf.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
listen 8080
2+
document_root ./../http-test-suite/

include/Config.hh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
#include <string>
3+
#include <unordered_map>
4+
5+
6+
class Config
7+
{
8+
public:
9+
Config(std::string const & fileName);
10+
11+
int getInt(std::string const & key);
12+
std::string getStr(std::string const & key);
13+
14+
private:
15+
std::unordered_map<std::string, std::string> confMap;
16+
};

include/HttpServer.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Http {
1010

1111
class HttpServer {
1212
public:
13-
static void init(uint32_t port);
13+
static void init(uint32_t port, std::string const & dRoot);
1414
static HttpServer &instance();
1515

1616
HttpServer(HttpServer const &) = delete;
@@ -20,7 +20,7 @@ public:
2020

2121
void destroy(uint32_t id);
2222

23-
static uint32_t port_;
23+
static uint32_t port;
2424
static std::string documentRoot;
2525

2626
private:

include/core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ enum {
3636

3737
void write_to_data(data_t * dest, char const * src, uint32_t len);
3838

39-
void init(on_accept_cb_t, on_read_cb_t, on_destroy_cb_t);
39+
void init(int port, on_accept_cb_t, on_read_cb_t, on_destroy_cb_t);
4040
void need_destroy(uint32_t conn_idx);
4141
void set_need_read(uint32_t conn_idx, int val);

src/Config.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <fstream>
2+
#include <sstream>
3+
#include "Config.hh"
4+
5+
Config::Config(const std::string &fileName) {
6+
std::fstream fs;
7+
fs.open(fileName);
8+
9+
if (!fs.is_open())
10+
throw std::runtime_error("Can't open conf file");
11+
12+
std::string line;
13+
while (std::getline(fs, line)) {
14+
std::stringstream lineStream(line);
15+
std::string key, value;
16+
lineStream >> key >> value;
17+
18+
if (key.size() && value.size())
19+
confMap[key] = value;
20+
}
21+
}
22+
23+
int Config::getInt(std::string const &key) {
24+
auto val = getStr(key);
25+
26+
int res;
27+
std::stringstream ss(val);
28+
ss >> res;
29+
30+
return res;
31+
}
32+
33+
std::string Config::getStr(std::string const &key) {
34+
auto val = confMap.find(key);
35+
if (val == confMap.end())
36+
throw std::logic_error("Key not exist");
37+
38+
return val->second;
39+
}
40+
41+

src/HttpServer.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ long fileSize(std::string const & filename)
1414

1515
namespace Http {
1616

17-
std::string HttpServer::documentRoot = "/home/algys/sources/http-server/http-test-suite";
17+
std::string HttpServer::documentRoot = ".";
18+
uint32_t HttpServer::port = 8080;
1819

1920
void HttpServer::onRead(void *udata) {
2021
if (!udata)
@@ -105,9 +106,11 @@ void HttpServer::destroy(uint32_t id) {
105106
need_destroy(id);
106107
}
107108

108-
void HttpServer::init(uint32_t port) {
109-
// HttpServer::port_ = port;
110-
::init(&HttpServer::onAccept, &HttpServer::onRead, &HttpServer::onDestroy);
109+
void HttpServer::init(uint32_t port, std::string const & dRoot) {
110+
HttpServer::port = port;
111+
HttpServer::documentRoot = dRoot;
112+
113+
::init(port, &HttpServer::onAccept, &HttpServer::onRead, &HttpServer::onDestroy);
111114
new HttpServer();
112115
}
113116

src/app.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#include <HttpServer.hh>
2+
#include <Config.hh>
23

34
extern "C" {
45

5-
int app_init() {
6-
Http::HttpServer::init(8080);
6+
int app_init(int argc, char ** argv) {
7+
if (argc < 2)
8+
throw std::runtime_error("Too few args");
9+
10+
auto conf = Config(argv[1]);
11+
12+
Http::HttpServer::init((uint32_t) conf.getInt("listen"), conf.getStr("document_root"));
713
}
814

915
}

src/core.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ static void _init() {
5858
events = (struct epoll_event *) malloc(sizeof(struct epoll_event) * MAX_EVENTS * 10);
5959
}
6060

61-
void init(on_accept_cb_t accept_cb, on_read_cb_t read_cb, on_destroy_cb_t destroy_cb) {
61+
void init(int port, on_accept_cb_t accept_cb, on_read_cb_t read_cb, on_destroy_cb_t destroy_cb) {
62+
listen_port = port;
6263
on_accept_cb = accept_cb;
6364
on_read_cb = read_cb;
6465
on_destroy_cb = destroy_cb;
@@ -323,10 +324,10 @@ int loop() {
323324
return 0;
324325
}
325326

326-
int main() {
327-
extern void app_init(void);
327+
int main(int argc, char ** argv) {
328+
extern void app_init(int, char **);
328329

329-
app_init();
330+
app_init(argc, argv);
330331

331332
loop();
332333
}

0 commit comments

Comments
 (0)