You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
C++
51 lines
1.1 KiB
C++
#include "webcc/logger.h"
|
|
#include "webcc/response_builder.h"
|
|
#include "webcc/server.h"
|
|
|
|
class HelloView : public webcc::View {
|
|
public:
|
|
webcc::ResponsePtr Handle(webcc::RequestPtr request) override {
|
|
if (request->method() == "GET") {
|
|
return webcc::ResponseBuilder{}.OK().Body("Hello, World!")();
|
|
}
|
|
|
|
return {};
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
|
|
|
try {
|
|
webcc::Server server{ boost::asio::ip::tcp::v4(), 8080 };
|
|
|
|
server.Route("/", std::make_shared<HelloView>());
|
|
|
|
// Run the server in a separate thread.
|
|
std::thread t([&server]() { server.Run(); });
|
|
|
|
// Let the server run for several seconds.
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
|
|
|
// Stop the server.
|
|
server.Stop();
|
|
|
|
// Wait for the server to finish.
|
|
t.join();
|
|
|
|
// Run the server again.
|
|
std::thread t2([&server]() { server.Run(); });
|
|
|
|
// Wait for the server to finish.
|
|
t2.join();
|
|
|
|
} catch (const std::exception&) {
|
|
// NOTE:
|
|
// Catch std::exception instead of webcc::Error.
|
|
// webcc::Error is for client only.
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|