File size: 948 Bytes
98ca630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * @file config.cpp
 */

#include "core/config.hpp"
#include <cstdlib>
#include <charconv>

namespace app::core {

std::expected<Config, std::string> Config::from_env() {
    Config cfg;

    if (const char* name = std::getenv("APP_NAME")) {
        cfg.app_name = name;
    }
    if (const char* url = std::getenv("DATABASE_URL")) {
        cfg.database_url = url;
    }
    if (const char* key = std::getenv("SECRET_KEY")) {
        cfg.secret_key = key;
    }
    if (const char* port_str = std::getenv("PORT")) {
        auto result = std::from_chars(port_str, port_str + std::strlen(port_str), cfg.port);
        if (result.ec != std::errc{}) {
            return std::unexpected<std::string>("Invalid PORT environment variable");
        }
    }
    if (const char* debug = std::getenv("DEBUG")) {
        cfg.debug = std::string_view(debug) == "true" || std::string_view(debug) == "1";
    }

    return cfg;
}

} // namespace app::core