-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
153 lines (137 loc) · 5.91 KB
/
main.cpp
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
#include <drogon/HttpAppFramework.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/program_options.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include "controllers/games_controller.h"
#include "controllers/rooms_controller.h"
#include "controllers/sessions_controller.h"
#include "controllers/state_controller.h"
#include "controllers/statistics_controller.h"
#include "model/games/games_storage.h"
#include "model/logic/game_logic_manager.h"
#include "model/rooms/rooms_storage.h"
#include "model/sessions/sessions_storage.h"
#include "model/statistics/statistics_manager.h"
namespace cavoke::server {
void run(const model::GamesStorageConfig &games_storage_config) {
// TODO: logging
// init models
std::cout << "Initialize models..." << std::endl;
auto games_storage =
std::make_shared<model::GamesStorage>(games_storage_config);
auto game_logic_manager =
std::make_shared<model::GameLogicManager>(games_storage);
auto game_state_storage = std::make_shared<model::GameStateStorage>();
auto sessions_storage = std::make_shared<model::SessionsStorage>(
game_logic_manager, games_storage, game_state_storage);
auto statistics_manager = std::make_shared<model::StatisticsManager>(
sessions_storage, games_storage);
auto rooms_storage =
std::make_shared<model::RoomsStorage>(sessions_storage);
// init controllers
std::cout << "Initialize controllers..." << std::endl;
auto games_controller =
std::make_shared<controllers::GamesController>(games_storage);
auto state_controller = std::make_shared<controllers::StateController>(
games_storage, game_logic_manager, game_state_storage,
sessions_storage);
auto sessions_controller =
std::make_shared<controllers::SessionsController>(
games_storage, game_logic_manager, game_state_storage,
sessions_storage);
auto statistics_controller =
std::make_shared<controllers::StatisticsController>(games_storage,
statistics_manager);
auto rooms_controller = std::make_shared<controllers::RoomsController>(
rooms_storage, games_storage, sessions_storage);
auto &app = drogon::app();
// register controllers
app.registerController(games_controller);
app.registerController(state_controller);
app.registerController(sessions_controller);
app.registerController(statistics_controller);
app.registerController(rooms_controller);
// print db and listeners on start
app.registerBeginningAdvice([]() {
auto dbclient = drogon::app().getDbClient();
dbclient->connectionInfo();
std::cout << "Database connection: "
<< (dbclient == nullptr
? "FAILED"
: hide_password(dbclient->connectionInfo()))
<< std::endl;
auto listeners = drogon::app().getListeners();
std::string ips =
listeners.empty()
? "<NO LISTENERS>"
: boost::join(
listeners | boost::adaptors::transformed(
[](const trantor::InetAddress &e) {
return e.toIpPort();
}),
", ");
std::cout << "Listening at " << ips << "... " << std::endl;
});
// Add CORS handler
auto corsValue = nlohmann::to_nlohmann(drogon::app().getCustomConfig())
.value("cors", "*");
app.registerPostHandlingAdvice(
[corsValue](const drogon::HttpRequestPtr &req,
const drogon::HttpResponsePtr &resp) {
// cors header for every response
resp->addHeader("Access-Control-Allow-Origin", corsValue);
});
// start server
app.run();
}
} // namespace cavoke::server
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
po::options_description desc("Allowed options");
auto add_desc_options = desc.add_options();
add_desc_options("help,h", "Print help");
add_desc_options("config-file,c", po::value<std::string>(),
"File with game storage configuration");
add_desc_options("port,p", po::value<uint16_t>()->default_value(8080),
"TCP/IP port number for connection");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << "\n";
return 0;
}
cavoke::server::model::GamesStorageConfig games_storage_config;
if (vm.count("config-file")) {
// load from file
try {
std::string file = vm["config-file"].as<std::string>();
drogon::app().loadConfigFile(file);
games_storage_config =
nlohmann::to_nlohmann(drogon::app().getCustomConfig())
.at("storage")
.get<cavoke::server::model::GamesStorageConfig>();
std::cout << "Server configuration loaded from: '" << file << "'\n";
} catch (const std::exception &err) {
std::cerr
<< "Failed to load games storage configuration from file: "
<< err.what() << '\n';
return 1;
}
} else {
// use default one
std::cerr
<< "No server configuration file specified. Using a default "
"one.\nWARNING: No database requests will succeed.\nPlease use "
"`-c` option to specify config file.\n";
// default configuration
games_storage_config = {"../../local_server/games", "logic",
"client.zip", "config.json"};
}
if (vm.count("port")) {
uint16_t port = vm.at("port").as<uint16_t>();
drogon::app().addListener("0.0.0.0", port);
}
cavoke::server::run(games_storage_config);
return 0;
}