WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
web_framework_c_api.cpp
1#ifdef WEB_FRAMEWORK_DLL
2
3#include "web_framework_c_api.h"
4
5#include "Import/WebFramework.h"
6#include "Import/Config.h"
7#include "Log.h"
8
9#define LOG_EXCEPTION() if (Log::isValid()) { Log::error("Exception: {}", "C_API", e.what()); }
10#define CREATE_EXCEPTION() *exception = new std::runtime_error(e.what())
11#define LOG_AND_CREATE_EXCEPTION() LOG_EXCEPTION(); CREATE_EXCEPTION()
12
13void deleteWebFrameworkString(String string)
14{
15 delete static_cast<std::string*>(string);
16}
17
18void deleteWebFrameworkConfig(Config config)
19{
20 delete static_cast<framework::utility::Config*>(config);
21}
22
23void deleteWebFramework(WebFramework webFramework)
24{
25 delete static_cast<framework::WebFramework*>(webFramework);
26}
27
28void deleteWebFrameworkException(Exception exception)
29{
30 delete static_cast<std::runtime_error*>(exception);
31}
32
33const char* getDataFromString(String string)
34{
35 return static_cast<std::string*>(string)->data();
36}
37
39
40WebFramework createWebFrameworkFromPath(const char* configPath, Exception* exception)
41{
42 try
43 {
44 return new framework::WebFramework(std::filesystem::path(configPath));
45 }
46 catch (const std::exception& e)
47 {
48 LOG_AND_CREATE_EXCEPTION();
49 }
50
51 return nullptr;
52}
53
54WebFramework createWebFrameworkFromString(const char* serverConfiguration, const char* applicationDirectory, Exception* exception)
55{
56 try
57 {
58 return new framework::WebFramework(framework::utility::Config(serverConfiguration, applicationDirectory));
59 }
60 catch (const std::exception& e)
61 {
62 LOG_AND_CREATE_EXCEPTION();
63 }
64
65 return nullptr;
66}
67
68WebFramework createWebFrameworkFromConfig(Config config, Exception* exception)
69{
70 try
71 {
72 return new framework::WebFramework(*static_cast<framework::utility::Config*>(config));
73 }
74 catch (const std::exception& e)
75 {
76 LOG_AND_CREATE_EXCEPTION();
77 }
78
79 return nullptr;
80}
81
82Config createConfigFromPath(const char* configPath, Exception* exception)
83{
84 try
85 {
86 return new framework::utility::Config(configPath);
87 }
88 catch (const std::exception& e)
89 {
90 LOG_AND_CREATE_EXCEPTION();
91 }
92
93 return nullptr;
94}
95
96Config createConfigFromString(const char* serverConfiguration, const char* applicationDirectory, Exception* exception)
97{
98 try
99 {
100 return new framework::utility::Config(serverConfiguration, applicationDirectory);
101 }
102 catch (const std::exception& e)
103 {
104 LOG_AND_CREATE_EXCEPTION();
105 }
106
107 return nullptr;
108}
109
111
112void startWebFrameworkServerCXX(WebFramework server, bool wait, void* onStartServer, Exception* exception)
113{
114 try
115 {
116 std::function<void()>* lambda = static_cast<std::function<void()>*>(onStartServer);
117
118 static_cast<framework::WebFramework*>(server)->start(wait, *lambda);
119 }
120 catch (const std::exception& e)
121 {
122 LOG_AND_CREATE_EXCEPTION();
123 }
124}
125
126void startWebFrameworkServer(WebFramework server, bool wait, void (*onStartServer)(), Exception* exception)
127{
128 try
129 {
130 static_cast<framework::WebFramework*>(server)->start(wait, onStartServer);
131 }
132 catch (const std::exception& e)
133 {
134 LOG_AND_CREATE_EXCEPTION();
135 }
136}
137
138void stopWebFrameworkServer(WebFramework server, bool wait, Exception* exception)
139{
140 try
141 {
142 static_cast<framework::WebFramework*>(server)->stop(wait);
143 }
144 catch (const std::exception& e)
145 {
146 LOG_AND_CREATE_EXCEPTION();
147 }
148}
149
150bool isServerRunning(WebFramework server, Exception* exception)
151{
152 try
153 {
154 return static_cast<framework::WebFramework*>(server)->isServerRunning();
155 }
156 catch (const std::exception& e)
157 {
158 LOG_AND_CREATE_EXCEPTION();
159 }
160
161 return false;
162}
163
164const char* getWebFrameworkVersion()
165{
167}
168
169void overrideConfigurationString(Config config, const char* key, const char* value, bool recursive, Exception* exception)
170{
171 try
172 {
173 static_cast<framework::utility::Config*>(config)->overrideConfiguration(key, std::string(value), recursive);
174 }
175 catch (const std::exception& e)
176 {
177 LOG_AND_CREATE_EXCEPTION();
178 }
179}
180
181void overrideConfigurationInteger(Config config, const char* key, int64_t value, bool recursive, Exception* exception)
182{
183 try
184 {
185 static_cast<framework::utility::Config*>(config)->overrideConfiguration(key, value, recursive);
186 }
187 catch (const std::exception& e)
188 {
189 LOG_AND_CREATE_EXCEPTION();
190 }
191}
192
193void overrideConfigurationBoolean(Config config, const char* key, bool value, bool recursive, Exception* exception)
194{
195 try
196 {
197 static_cast<framework::utility::Config*>(config)->overrideConfiguration(key, static_cast<json::utility::jsonObject::variantType>(value), recursive);
198 }
199 catch (const std::exception& e)
200 {
201 LOG_AND_CREATE_EXCEPTION();
202 }
203}
204
205void overrideConfigurationStringArray(Config config, const char* key, const char** value, bool recursive, int64_t size, Exception* exception)
206{
207 try
208 {
209 std::vector<json::utility::jsonObject> data;
210
211 data.reserve(size);
212
213 for (int64_t i = 0; i < size; i++)
214 {
215 json::utility::appendArray(std::string(value[i]), data);
216 }
217
218 static_cast<framework::utility::Config*>(config)->overrideConfiguration(key, data, recursive);
219 }
220 catch (const std::exception& e)
221 {
222 LOG_AND_CREATE_EXCEPTION();
223 }
224}
225
226void overrideConfigurationIntegerArray(Config config, const char* key, const int64_t* value, bool recursive, int64_t size, Exception* exception)
227{
228 try
229 {
230 std::vector<json::utility::jsonObject> data;
231
232 data.reserve(size);
233
234 for (int64_t i = 0; i < size; i++)
235 {
236 json::utility::appendArray(value[i], data);
237 }
238
239 static_cast<framework::utility::Config*>(config)->overrideConfiguration(key, data, recursive);
240 }
241 catch (const std::exception& e)
242 {
243 LOG_AND_CREATE_EXCEPTION();
244 }
245}
246
247String getConfigurationString(Config config, const char* key, bool recursive, Exception* exception)
248{
249 try
250 {
251 return new std::string(static_cast<framework::utility::Config*>(config)->getConfigurationString(key, recursive));
252 }
253 catch (const std::exception& e)
254 {
255 LOG_AND_CREATE_EXCEPTION();
256 }
257
258 return nullptr;
259}
260
261int64_t getConfigurationInteger(Config config, const char* key, bool recursive, Exception* exception)
262{
263 try
264 {
265 return static_cast<framework::utility::Config*>(config)->getConfigurationInteger(key, recursive);
266 }
267 catch (const std::exception& e)
268 {
269 LOG_AND_CREATE_EXCEPTION();
270 }
271
272 return -1;
273}
274
275bool getConfigurationBoolean(Config config, const char* key, bool recursive, Exception* exception)
276{
277 try
278 {
279 return static_cast<framework::utility::Config*>(config)->getConfigurationBoolean(key, recursive);
280 }
281 catch (const std::exception& e)
282 {
283 LOG_AND_CREATE_EXCEPTION();
284 }
285
286 return false;
287}
288
289void overrideBasePath(Config config, const char* basePath, Exception* exception)
290{
291 try
292 {
293 reinterpret_cast<framework::utility::Config*>(config)->overrideBasePath(basePath);
294 }
295 catch (const std::exception& e)
296 {
297 LOG_AND_CREATE_EXCEPTION();
298 }
299}
300
301void* getConfiguration(Config config, Exception* exception)
302{
303 try
304 {
305 std::ostringstream result;
306
307 result << **static_cast<framework::utility::Config*>(config);
308
309 return new std::string(result.str());
310 }
311 catch (const std::exception& e)
312 {
313 LOG_AND_CREATE_EXCEPTION();
314 }
315
316 return nullptr;
317}
318
319const char* getRawConfiguration(Config config, Exception* exception)
320{
321 try
322 {
323 return (*(*static_cast<framework::utility::Config*>(config))).getRawData().data();
324 }
325 catch (const std::exception& e)
326 {
327 LOG_AND_CREATE_EXCEPTION();
328 }
329
330 return nullptr;
331}
332
333void* getBasePath(Config config, Exception* exception)
334{
335 try
336 {
337 return new std::string(static_cast<framework::utility::Config*>(config)->getBasePath().string());
338 }
339 catch (const std::exception& e)
340 {
341 LOG_AND_CREATE_EXCEPTION();
342 }
343
344 return nullptr;
345}
346
348
349const char* getErrorMessage(Exception exception)
350{
351 return static_cast<std::runtime_error*>(exception)->what();
352}
353
354#endif
Main class of framework.
void start(bool wait=false, const std::function< void()> &onStartServer=[]() {})
Start server.
static std::string_view getWebFrameworkVersion()
Get current WebFramework version.
Config file representation.
Definition Config.h:15