Add hello world server example
This commit is contained in:
@@ -39,6 +39,9 @@ if(WEBCC_ENABLE_SSL)
|
||||
target_link_libraries(github_client ${EXAMPLE_LIBS} jsoncpp)
|
||||
endif()
|
||||
|
||||
add_executable(hello_world_server hello_world_server.cc)
|
||||
target_link_libraries(hello_world_server ${EXAMPLE_LIBS})
|
||||
|
||||
add_executable(rest_book_server rest_book_server.cc ${REST_BOOK_SRCS})
|
||||
target_link_libraries(rest_book_server ${EXAMPLE_LIBS} jsoncpp)
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#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() {
|
||||
try {
|
||||
webcc::Server server(8080);
|
||||
|
||||
server.Route("/", std::make_shared<HelloView>());
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (const std::exception&) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -214,6 +214,7 @@ int main(int argc, char* argv[]) {
|
||||
session.set_timeout(timeout);
|
||||
|
||||
// If the request has body, default to this content type.
|
||||
// Optional.
|
||||
session.set_media_type("application/json");
|
||||
session.set_charset("utf-8");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user