-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
289 lines (250 loc) · 8.79 KB
/
main.cc
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "controllers/AuthController.h"
#include "controllers/RoleController.h"
#include "controllers/UserController.h"
#include "models/Users.h"
#include "tables/PermissionTable.h"
#include "tables/RolePermissionTable.h"
#include "tables/RoleTable.h"
#include "tables/UserPermissionTable.h"
#include "tables/UserTable.h"
#include <drogon/HttpAppFramework.h>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
using namespace std;
using namespace drogon;
using namespace drogon::orm;
using namespace drogon_model::drogon_user_service;
void createTables(const string &connectionString) {
// User table
UserTable userTable;
userTable.create(connectionString);
// Role table
RoleTable roleTable;
roleTable.create(connectionString);
// Permission table
PermissionTable permissionTable;
permissionTable.create(connectionString);
// User permission table
UserPermissionTable userPermissionTable;
userPermissionTable.create(connectionString);
// Role permission table
RolePermissionTable rolePermissionTable;
rolePermissionTable.create(connectionString);
}
void registerRoutes() {
// Register root route
drogon::app().registerHandler(
"/", [](const HttpRequestPtr &req,
function<void(const HttpResponsePtr &)> &&callback) {
// Return response
Json::Value response;
response["status"] = "up";
HttpResponsePtr resp = HttpResponse::newHttpJsonResponse(response);
callback(resp);
});
// Register generate jwt token
auto authController = make_shared<AuthController>();
drogon::app().registerHandler(
"/api/v1/generate-jwt",
[authController](const HttpRequestPtr &req,
function<void(const HttpResponsePtr &)> &&callback) {
if (req->method() == Options) {
authController->getHeaders(req, std::move(callback));
} else if (req->method() == Post) {
authController->asyncHandleHttpRequest(req, std::move(callback));
}
},
{Options, Post});
// Register user routes
auto userController = make_shared<UserController>();
drogon::app().registerHandler(
"/api/v1/user",
[userController](const HttpRequestPtr &req,
function<void(const HttpResponsePtr &)> &&callback) {
if (req->method() == Options) {
userController->getHeaders(req, std::move(callback));
} else if (req->method() == Get) {
userController->getUsers(req, std::move(callback));
} else if (req->method() == Post) {
userController->createUser(req, std::move(callback));
}
},
{Options, Get, Post, "AuthFilter"});
app().registerHandler(
"/api/v1/user/{id}",
[userController](const HttpRequestPtr &req,
function<void(const HttpResponsePtr &)> &&callback,
const string &id) {
if (req->method() == Options) {
userController->getByIdHeaders(req, std::move(callback), id);
} else if (req->method() == Get) {
userController->getUserById(req, std::move(callback), id);
} else if (req->method() == Put) {
userController->updateUserById(req, std::move(callback), id);
;
} else if (req->method() == Delete) {
userController->deleteUserById(req, std::move(callback), id);
;
}
},
{Options, Get, Put, Delete, "AuthFilter"});
// Register role routes
auto roleController = make_shared<RoleController>();
app().registerHandler(
"/api/v1/role",
[roleController](const HttpRequestPtr &req,
function<void(const HttpResponsePtr &)> &&callback) {
if (req->method() == Options) {
roleController->getHeaders(req, std::move(callback));
} else if (req->method() == Get) {
roleController->getRoles(req, std::move(callback));
} else if (req->method() == Post) {
roleController->createRole(req, std::move(callback));
}
},
{Options, Get, Post, "AuthFilter"});
app().registerHandler(
"/api/v1/role/{id}",
[roleController](const HttpRequestPtr &req,
function<void(const HttpResponsePtr &)> &&callback,
const string &id) {
if (req->method() == Options) {
roleController->getByIdHeaders(req, std::move(callback), id);
} else if (req->method() == Get) {
roleController->getRoleById(req, std::move(callback), id);
} else if (req->method() == Put) {
roleController->updateRoleById(req, std::move(callback), id);
;
} else if (req->method() == Delete) {
roleController->deleteRoleById(req, std::move(callback), id);
;
}
},
{Options, Get, Put, Delete, "AuthFilter"});
}
void dropTables() {}
void runServer() {
// Set log level
drogon::app().setLogLevel(trantor::Logger::kTrace);
int32_t port = 8000;
// Set HTTP listener address and port
drogon::app().addListener("0.0.0.0", port);
// Load config file
try {
drogon::app().loadConfigFile("../config.json");
} catch (const exception &e) {
cerr << "Exception caught: " << typeid(e).name() << " - " << e.what()
<< endl;
}
// Register routes
registerRoutes();
// Run server
LOG_INFO << "Server running on 127.0.0.1:" << port;
drogon::app().run();
}
map<string, string> loadEnvVariables(const string &filename) {
map<string, string> envVariables;
ifstream file(filename);
if (!file.is_open()) {
throw runtime_error("Failed to open .env file.");
}
string line;
while (getline(file, line)) {
stringstream lineStream(line);
string key, value;
if (getline(lineStream, key, '=') && getline(lineStream, value)) {
envVariables[key] = value;
}
}
file.close();
return envVariables;
}
void generateConfigFile() {
// Load environment variables from .env file
map<string, string> envVariables = loadEnvVariables(".env");
// Create a JSON object
Json::Value config;
// Set the values for the variables from environment variables
string secretKey = envVariables["SECRET_KEY"];
string dbHost = envVariables["DB_HOST"];
string dbName = envVariables["DB_NAME"];
string user = envVariables["DB_USER"];
string password = envVariables["DB_PASSWORD"];
config["secret_key"] = secretKey;
config["db_clients"] = Json::Value(Json::arrayValue);
Json::Value &dbClients = config["db_clients"];
// Create a database client object
Json::Value dbClient;
dbClient["name"] = "default";
dbClient["rdbms"] = "postgresql";
dbClient["host"] = dbHost;
dbClient["port"] = 5432;
dbClient["dbname"] = dbName;
dbClient["user"] = user;
dbClient["passwd"] = password;
dbClient["is_fast"] = false;
dbClient["connection_number"] = 1;
dbClient["filename"] = "";
// Add the database client object to the array
dbClients.append(dbClient);
// Generate the JSON string
Json::StreamWriterBuilder writer;
writer["indentation"] = " "; // 4 spaces for indentation
string jsonString = Json::writeString(writer, config);
// Write the JSON string to a file
ofstream outputFile("config.json");
outputFile << jsonString;
outputFile.close();
cout << "Configuration file generated successfully." << endl;
}
int main(int argc, char *argv[]) {
// Generate config file from .env
generateConfigFile();
// Check if the correct number of command-line arguments is provided
if (argc != 2) {
cerr << "Usage: "
"--action=run-server|create-tables|drop-tables"
<< endl;
return 1;
}
// Parse the command-line argument
string action = argv[1];
// Extract the action from the argument
size_t equalsPos = action.find('=');
if (equalsPos == string::npos || equalsPos == action.length() - 1) {
cerr << "Invalid argument format" << endl;
return 1;
}
string key = action.substr(0, equalsPos);
string value = action.substr(equalsPos + 1);
// Database config to database cli interface
map<string, string> envVariables = loadEnvVariables(".env");
Json::Value config;
string secretKey = envVariables["SECRET_KEY"];
string dbHost = envVariables["DB_HOST"];
string dbName = envVariables["DB_NAME"];
string user = envVariables["DB_USER"];
string password = envVariables["DB_PASSWORD"];
string port = "5432";
string connectionString = "postgresql://"+ user +":"+ password +"@"+ dbHost +":"+ port +"/"+ dbName;
// Check the action and perform the corresponding operation
if (key == "--action") {
if (value == "run-server") {
runServer();
} else if (value == "create-tables") {
createTables(connectionString);
} else if (value == "drop-tables") {
dropTables();
} else {
cerr << "Invalid action" << endl;
return 1;
}
} else {
cerr << "Invalid argument" << endl;
return 1;
}
return 0;
}