MCPcopy Create free account
hub / github.com/chronoxor/CppServer / main

Function main

examples/https_server.cpp:190–261  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

188};
189
190int main(int argc, char** argv)
191{
192 // HTTPS server port
193 int port = 8443;
194 if (argc > 1)
195 port = std::atoi(argv[1]);
196 // HTTPS server content path
197 std::string www = "../www/api";
198 if (argc > 2)
199 www = argv[2];
200
201 std::cout << "HTTPS server port: " << port << std::endl;
202 std::cout << "HTTPS server static content path: " << www << std::endl;
203 std::cout << "HTTPS server website: " << "https://localhost:" << port << "/api/index.html" << std::endl;
204
205 std::cout << std::endl;
206
207 // Create a new Asio service
208 auto service = std::make_shared<AsioService>();
209
210 // Start the Asio service
211 std::cout << "Asio service starting...";
212 service->Start();
213 std::cout << "Done!" << std::endl;
214
215 // Create and prepare a new SSL server context
216 auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv13);
217 context->set_password_callback([](size_t max_length, asio::ssl::context::password_purpose purpose) -> std::string { return "qwerty"; });
218 context->use_certificate_chain_file("../tools/certificates/server.pem");
219 context->use_private_key_file("../tools/certificates/server.pem", asio::ssl::context::pem);
220 context->use_tmp_dh_file("../tools/certificates/dh4096.pem");
221
222 // Create a new HTTPS server
223 auto server = std::make_shared<HTTPSCacheServer>(service, context, port);
224 server->AddStaticContent(www, "/api");
225
226 // Start the server
227 std::cout << "Server starting...";
228 server->Start();
229 std::cout << "Done!" << std::endl;
230
231 std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
232
233 // Perform text input
234 std::string line;
235 while (getline(std::cin, line))
236 {
237 if (line.empty())
238 break;
239
240 // Restart the server
241 if (line == "!")
242 {
243 std::cout << "Server restarting...";
244 server->Restart();
245 std::cout << "Done!" << std::endl;
246 continue;
247 }

Callers

nothing calls this directly

Calls 5

StartMethod · 0.45
AddStaticContentMethod · 0.45
emptyMethod · 0.45
RestartMethod · 0.45
StopMethod · 0.45

Tested by

no test coverage detected