-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathOdbDesignServerApp.cpp
More file actions
213 lines (185 loc) · 7.56 KB
/
Copy pathOdbDesignServerApp.cpp
File metadata and controls
213 lines (185 loc) · 7.56 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
#include "OdbDesignServerApp.h"
#include "Controllers/HelloWorldController.h"
#include "Controllers/FileUploadController.h"
#include "Controllers/FileModelController.h"
#include "Controllers/HealthCheckController.h"
#include "Controllers/DesignsController.h"
#include "Services/OdbDesignServiceImpl.h"
#include "Config/GrpcServiceConfig.h"
#include "macros.h"
#include <App/BasicRequestAuthentication.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <iostream>
#include <utility>
#include <memory>
#include <string>
#include <App/DesignCache.h>
#include "OdbServerAppBase.h"
#include <crow/middlewares/cors.h>
#include <grpcpp/security/server_credentials.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/resource_quota.h>
#include <filesystem>
#include <cstdlib>
using namespace Odb::Lib::App;
namespace Odb::App::Server
{
OdbDesignServerApp::OdbDesignServerApp(int argc, char* argv[])
: OdbServerAppBase(argc, argv)
{
}
void OdbDesignServerApp::add_controllers()
{
m_vecControllers.push_back(std::make_shared<HelloWorldController>(*this));
m_vecControllers.push_back(std::make_shared<FileUploadController>(*this));
m_vecControllers.push_back(std::make_shared<FileModelController>(*this));
m_vecControllers.push_back(std::make_shared<HealthCheckController>(*this));
m_vecControllers.push_back(std::make_shared<DesignsController>(*this));
}
void OdbDesignServerApp::RunGrpcServer(const std::string& server_address, std::shared_ptr<Odb::Lib::App::DesignCache> cache)
{
if (!cache)
{
std::cerr << "ERROR: DesignCache is null, cannot start gRPC server" << std::endl;
return;
}
// Load gRPC service configuration
// Check GRPC_CONFIG_PATH environment variable first, then try executable directory, then current directory
std::string configPath;
const char* envConfig = std::getenv("GRPC_CONFIG_PATH");
if (envConfig != nullptr && envConfig[0] != '\0')
{
std::filesystem::path p(envConfig);
std::error_code ec;
p = std::filesystem::weakly_canonical(p, ec);
std::filesystem::path exeDir = std::filesystem::weakly_canonical(args().executableDirectory(), ec);
std::filesystem::path currentDir = std::filesystem::current_path(ec);
bool isValidPath = false;
// Check if p is contained within exeDir or currentDir
if (std::filesystem::proximate(p, exeDir, ec).string().find("..") == std::string::npos ||
std::filesystem::proximate(p, currentDir, ec).string().find("..") == std::string::npos)
{
isValidPath = true;
}
if (!ec && p.extension() == ".json" && isValidPath && std::filesystem::is_regular_file(p, ec))
{
configPath = p.string();
}
else
{
std::cerr << "WARNING: GRPC_CONFIG_PATH must be a valid path to an existing .json file within allowed directories. Falling back to default locations." << std::endl;
envConfig = nullptr; // trigger fallback
}
}
if (envConfig == nullptr || envConfig[0] == '\0')
{
// Try executable directory first (where config.json should be)
std::filesystem::path exeDir = args().executableDirectory();
std::filesystem::path configInExeDir = exeDir / "config.json";
// Try current working directory as fallback
std::filesystem::path configInCwd = "config.json";
// Check executable directory first, then current directory
if (std::filesystem::exists(configInExeDir))
{
configPath = configInExeDir.string();
}
else if (std::filesystem::exists(configInCwd))
{
configPath = configInCwd.string();
}
else
{
// Default to executable directory (will show "not found" message)
configPath = configInExeDir.string();
}
}
auto loadResult = OdbDesignServer::Config::GrpcServiceConfig::LoadFromFile(configPath);
std::cout << loadResult.message << std::endl;
OdbDesignServer::Services::OdbDesignServiceImpl service(cache, loadResult.config);
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
grpc::ServerBuilder builder;
// Apply message size limits from configuration
// Convert MB to bytes (multiply by 1024*1024)
int maxReceiveBytes = loadResult.config->max_receive_message_size_mb * 1024 * 1024;
int maxSendBytes = loadResult.config->max_send_message_size_mb * 1024 * 1024;
builder.SetMaxReceiveMessageSize(maxReceiveBytes);
builder.SetMaxSendMessageSize(maxSendBytes);
std::cout << "gRPC max message sizes: receive=" << loadResult.config->max_receive_message_size_mb
<< "MB, send=" << loadResult.config->max_send_message_size_mb << "MB" << std::endl;
// Apply compression configuration
if (loadResult.config->compression_enabled)
{
builder.SetDefaultCompressionAlgorithm(GRPC_COMPRESS_GZIP);
std::cout << "gRPC compression enabled (gzip)" << std::endl;
}
else
{
std::cout << "gRPC compression disabled" << std::endl;
}
// Apply thread pool limits via ResourceQuota
grpc::ResourceQuota quota;
quota.SetMaxThreads(loadResult.config->max_threads);
builder.SetResourceQuota(quota);
builder.SetSyncServerOption(grpc::ServerBuilder::SyncServerOption::MIN_POLLERS,
loadResult.config->min_pollers);
std::cout << "gRPC thread pool: max_threads=" << loadResult.config->max_threads
<< ", min_pollers=" << loadResult.config->min_pollers << std::endl;
// Apply keepalive channel arguments
builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIME_MS,
loadResult.config->keepalive_time_s * 1000);
builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIMEOUT_MS,
loadResult.config->keepalive_timeout_s * 1000);
builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS,
loadResult.config->keepalive_permit_without_calls ? 1 : 0);
builder.AddChannelArgument(GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS,
(loadResult.config->keepalive_time_s * 1000) / 2);
std::cout << "gRPC keepalive: time=" << loadResult.config->keepalive_time_s
<< "s, timeout=" << loadResult.config->keepalive_timeout_s
<< "s, permit_without_calls="
<< (loadResult.config->keepalive_permit_without_calls ? "true" : "false")
<< std::endl;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
// Build server and transfer ownership to base via shared_ptr
std::unique_ptr<grpc::Server> serverUnique = builder.BuildAndStart();
if (!serverUnique) {
std::cerr << "ERROR: Failed to start gRPC server on " << server_address << std::endl;
return;
}
std::cout << "gRPC server listening on " << server_address << std::endl;
// Share ownership so base class can Shutdown() it from another thread
std::shared_ptr<grpc::Server> serverShared(std::move(serverUnique));
set_grpc_server(serverShared);
// Wait for shutdown signal
serverShared->Wait();
std::cout << "gRPC server shut down." << std::endl;
// Release ownership after shutdown (base will also reset on its side)
set_grpc_server(nullptr);
}
bool OdbDesignServerApp::preServerRun()
{
// CORS
auto& cors = crow_app().get_middleware<crow::CORSHandler>();
if (Utils::IsProduction())
{
cors.global()
.headers("*")
.origin("*");
}
else
{
cors.global()
.headers("*")
.origin("*");
}
// add authentication
bool disableAuth = args().disableAuthentication();
auto basicRequestAuth = std::make_unique<BasicRequestAuthentication>(BasicRequestAuthentication(disableAuth));
request_auth(std::move(basicRequestAuth));
return true;
}
}