The path to the application directory is passed to the initialization… · cppAndNetworkAndDB/httpserver-1@db3d837 · GitHub
Skip to content

Commit db3d837

Browse files
committed
The path to the application directory is passed to the initialization function
1 parent cdc448f commit db3d837

5 files changed

Lines changed: 50 additions & 40 deletions

File tree

src/server/Server.cpp

Lines changed: 15 additions & 13 deletions

src/server/ServerApplicationSettings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace HttpServer
3131

3232
std::function<int(Transfer::app_request *, Transfer::app_response *)> application_call;
3333
std::function<void(void *, size_t)> application_clear;
34-
std::function<bool()> application_init;
35-
std::function<void()> application_final;
34+
std::function<bool(const char *)> application_init;
35+
std::function<void(const char *)> application_final;
3636
};
3737
};

src/server/ServerSettings.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ namespace HttpServer
3838
{
3939
if (app->application_final)
4040
{
41-
app->application_final();
41+
const std::string root = app->root_dir;
42+
app->application_final(root.data() );
4243
}
4344
}
4445
catch (std::exception &exc)
4546
{
46-
std::cout << "Warning: the error of the application's finalize '" << app->server_module << "':" << exc.what() << std::endl;
47+
std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl;
4748
}
4849

4950
delete app;

src/server/config/ConfigParser.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,33 @@ namespace HttpServer
244244

245245
std::function<void(void *, size_t)> app_clear = reinterpret_cast<void(*)(void *, size_t)>(addr);
246246

247-
std::function<bool()> app_init = std::function<bool()>();
247+
std::function<bool(const char *)> app_init = std::function<bool(const char *)>();
248248

249249
if (module.find("application_init", &addr) )
250250
{
251-
app_init = reinterpret_cast<bool(*)()>(addr);
251+
app_init = reinterpret_cast<bool(*)(const char *)>(addr);
252252
}
253253

254-
std::function<void()> app_final = std::function<void()>();
254+
std::function<void(const char *)> app_final = std::function<void(const char *)>();
255255

256256
if (module.find("application_final", &addr) )
257257
{
258-
app_final = reinterpret_cast<void(*)()>(addr);
258+
app_final = reinterpret_cast<void(*)(const char *)>(addr);
259+
}
260+
261+
std::string root_dir = it_root_dir->second;
262+
263+
#ifdef WIN32
264+
if ('\\' == root_dir.back() )
265+
{
266+
root_dir.pop_back();
267+
}
268+
#endif
269+
270+
// Remove back slash from root_dir
271+
if ('/' == root_dir.back() )
272+
{
273+
root_dir.pop_back();
259274
}
260275

261276
bool success = true;
@@ -264,17 +279,21 @@ namespace HttpServer
264279
{
265280
if (app_init)
266281
{
267-
success = app_init();
282+
const std::string root = root_dir;
283+
success = app_init(root.data() );
268284
}
269285
}
270-
catch (...)
286+
catch (std::exception &exc)
271287
{
288+
std::cout << "Warning: an exception was thrown when the application '" << it_module->second << "' was initialized: " << exc.what() << std::endl;
289+
272290
success = false;
273291
}
274292

275293
if (false == success)
276294
{
277295
std::cout << "Warning: error when initializing application '" << it_module->second << "';" << std::endl;
296+
278297
return false;
279298
}
280299

@@ -308,21 +327,6 @@ namespace HttpServer
308327
modules.emplace_back(std::move(module) );
309328
}
310329

311-
std::string root_dir = it_root_dir->second;
312-
313-
#ifdef WIN32
314-
if ('\\' == root_dir.back() )
315-
{
316-
root_dir.pop_back();
317-
}
318-
#endif
319-
320-
// Remove back slash from root_dir
321-
if ('/' == root_dir.back() )
322-
{
323-
root_dir.pop_back();
324-
}
325-
326330
// Create application settings struct
327331
ServerApplicationSettings *settings = new ServerApplicationSettings {
328332
std::move(ports),

src/server/protocol/ServerHttp1.cpp

Lines changed: 5 additions & 2 deletions

0 commit comments

Comments
 (0)