-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProxyConfig.cpp
More file actions
271 lines (212 loc) · 6.13 KB
/
Copy pathProxyConfig.cpp
File metadata and controls
271 lines (212 loc) · 6.13 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
*
* @file ProxyConfig.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/commands/proxy/ProxyConfig.hpp>
#include <nlohmann/json.hpp>
#include <filesystem>
#include <fstream>
#include <optional>
#include <string>
namespace fs = std::filesystem;
using json = nlohmann::json;
namespace vix::commands::proxy
{
namespace
{
std::string trim_copy(std::string value)
{
while (!value.empty() &&
(value.back() == '\n' ||
value.back() == '\r' ||
value.back() == '\t' ||
value.back() == ' '))
{
value.pop_back();
}
std::size_t index = 0;
while (index < value.size() &&
(value[index] == '\n' ||
value[index] == '\r' ||
value[index] == '\t' ||
value[index] == ' '))
{
++index;
}
return value.substr(index);
}
json read_json_or_empty(const fs::path &path)
{
if (!fs::exists(path))
return json::object();
std::ifstream in(path);
if (!in)
return json::object();
json value;
try
{
in >> value;
}
catch (...)
{
return json::object();
}
if (!value.is_object())
return json::object();
return value;
}
std::optional<std::string> read_string(
const json &object,
const std::string &key)
{
if (!object.is_object() ||
!object.contains(key) ||
!object[key].is_string())
{
return std::nullopt;
}
const std::string value = trim_copy(object[key].get<std::string>());
if (value.empty())
return std::nullopt;
return value;
}
std::optional<int> read_int(
const json &object,
const std::string &key)
{
if (!object.is_object() ||
!object.contains(key) ||
!object[key].is_number_integer())
{
return std::nullopt;
}
return object[key].get<int>();
}
std::optional<bool> read_bool(
const json &object,
const std::string &key)
{
if (!object.is_object() ||
!object.contains(key) ||
!object[key].is_boolean())
{
return std::nullopt;
}
return object[key].get<bool>();
}
json read_proxy_object(const json &root)
{
if (!root.is_object())
return json::object();
if (!root.contains("production") ||
!root["production"].is_object())
{
return json::object();
}
const auto &production = root["production"];
if (!production.contains("proxy") ||
!production["proxy"].is_object())
{
return json::object();
}
return production["proxy"];
}
std::string normalize_path(std::string value)
{
value = trim_copy(value);
if (value.empty())
return "/ws";
if (value.front() != '/')
value.insert(value.begin(), '/');
return value;
}
fs::path default_certificate_path(const std::string &domain)
{
return fs::path("/etc/letsencrypt/live") / domain / "fullchain.pem";
}
fs::path default_certificate_key_path(const std::string &domain)
{
return fs::path("/etc/letsencrypt/live") / domain / "privkey.pem";
}
}
std::optional<std::string> read_project_name()
{
const json root = read_json_or_empty(fs::current_path() / "vix.json");
if (auto name = read_string(root, "name"))
return name;
for (const auto &entry : fs::directory_iterator(fs::current_path()))
{
if (!entry.is_regular_file())
continue;
if (entry.path().extension() == ".vix")
return entry.path().stem().string();
}
const std::string fallback = trim_copy(fs::current_path().filename().string());
if (!fallback.empty())
return fallback;
return std::nullopt;
}
std::filesystem::path nginx_sites_available_path(
const NginxProxyConfig &cfg)
{
return fs::path("/etc/nginx/sites-available") / cfg.appName;
}
std::filesystem::path nginx_sites_enabled_path(
const NginxProxyConfig &cfg)
{
return fs::path("/etc/nginx/sites-enabled") / cfg.appName;
}
NginxProxyConfig load_nginx_proxy_config()
{
NginxProxyConfig cfg;
cfg.appName = read_project_name().value_or("vix-app");
const json root = read_json_or_empty(fs::current_path() / "vix.json");
const json proxy = read_proxy_object(root);
if (auto domain = read_string(proxy, "domain"))
cfg.domain = *domain;
if (proxy.contains("http") && proxy["http"].is_object())
{
const auto &http = proxy["http"];
if (auto port = read_int(http, "port"))
cfg.httpPort = *port;
}
if (proxy.contains("websocket") && proxy["websocket"].is_object())
{
const auto &websocket = proxy["websocket"];
if (auto enabled = read_bool(websocket, "enabled"))
cfg.websocketEnabled = *enabled;
if (auto path = read_string(websocket, "path"))
cfg.websocketPath = normalize_path(*path);
if (auto port = read_int(websocket, "port"))
cfg.websocketPort = *port;
}
if (proxy.contains("tls") && proxy["tls"].is_object())
{
const auto &tls = proxy["tls"];
if (auto enabled = read_bool(tls, "enabled"))
cfg.tlsEnabled = *enabled;
if (auto cert = read_string(tls, "certificate"))
cfg.certificatePath = fs::path(*cert);
if (auto key = read_string(tls, "certificate_key"))
cfg.certificateKeyPath = fs::path(*key);
}
if (cfg.tlsEnabled && !cfg.domain.empty())
{
if (cfg.certificatePath.empty())
cfg.certificatePath = default_certificate_path(cfg.domain);
if (cfg.certificateKeyPath.empty())
cfg.certificateKeyPath = default_certificate_key_path(cfg.domain);
}
cfg.sitesAvailablePath = nginx_sites_available_path(cfg);
cfg.sitesEnabledPath = nginx_sites_enabled_path(cfg);
return cfg;
}
}