db-lab1/bserv/config.hpp

50 lines
1.3 KiB
C++
Raw Normal View History

2021-07-13 19:53:10 +08:00
#ifndef _CONFIG_HPP
#define _CONFIG_HPP
#include <iostream>
#include <string>
#include <cstddef>
#include <optional>
2021-08-08 08:56:35 +08:00
#include <thread>
2021-07-13 19:53:10 +08:00
namespace bserv {
const std::string NAME = "bserv";
const unsigned short PORT = 8080;
2021-08-08 08:56:35 +08:00
const int NUM_THREADS =
std::thread::hardware_concurrency() > 0 ? std::thread::hardware_concurrency() : 1;
2021-07-13 19:53:10 +08:00
2021-08-08 08:56:35 +08:00
const std::size_t PAYLOAD_LIMIT = 8 * 1024 * 1024;
2021-07-13 19:53:10 +08:00
const int EXPIRY_TIME = 30; // seconds
2021-08-08 08:56:35 +08:00
const std::size_t LOG_ROTATION_SIZE = 8 * 1024 * 1024;
2021-07-13 19:53:10 +08:00
const std::string LOG_PATH = "./log/" + NAME;
const int NUM_DB_CONN = 10;
const std::string DB_CONN_STR = "dbname=bserv";
#define decl_field(type, name, default_value) \
private: \
std::optional<type> name##_; \
public: \
void set_##name(std::optional<type>&& name) { name##_ = std::move(name); } \
type get_##name() const { return name##_.has_value() ? name##_.value() : default_value; }
struct server_config {
decl_field(std::string, name, NAME)
decl_field(unsigned short, port, PORT)
decl_field(int, num_threads, NUM_THREADS)
decl_field(std::size_t, log_rotation_size, LOG_ROTATION_SIZE)
decl_field(std::string, log_path, LOG_PATH)
decl_field(int, num_db_conn, NUM_DB_CONN)
decl_field(std::string, db_conn_str, DB_CONN_STR)
public:
server_config() = default;
};
#undef decl_field
} // bserv
#endif // _CONFIG_HPP