Add hello world server example
This commit is contained in:
@@ -104,6 +104,38 @@ You can then parse `r->data()` to JSON object with your favorite JSON library. M
|
||||
|
||||
## Server API Examples
|
||||
|
||||
### Hello, World!
|
||||
|
||||
```cpp
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
### Book Store
|
||||
|
||||
Suppose you want to create a book server and provide the following operations with RESTful API:
|
||||
|
||||
- Query books based on some criterias.
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
+6
-11
@@ -17,7 +17,8 @@ namespace webcc {
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server(std::uint16_t port, std::size_t workers, const Path& doc_root = {});
|
||||
explicit Server(std::uint16_t port, std::size_t workers = 1,
|
||||
const Path& doc_root = {});
|
||||
|
||||
virtual ~Server() = default;
|
||||
|
||||
@@ -26,25 +27,19 @@ public:
|
||||
|
||||
// Route a URL to a view.
|
||||
// The URL should start with "/". E.g., "/instances".
|
||||
bool Route(const std::string& url, ViewPtr view, const Strings& methods) {
|
||||
bool Route(const std::string& url, ViewPtr view,
|
||||
const Strings& methods = { "GET" }) {
|
||||
return request_handler_.Route(url, view, methods);
|
||||
}
|
||||
|
||||
bool Route(const std::string& url, ViewPtr view) {
|
||||
return Route(url, view, { methods::kGet });
|
||||
}
|
||||
|
||||
// Route a regular expression URL to a view.
|
||||
// The URL should start with "/" and be a regular expression.
|
||||
// E.g., "/instances/(\\d+)".
|
||||
bool Route(const RegexUrl& regex_url, ViewPtr view, const Strings& methods) {
|
||||
bool Route(const RegexUrl& regex_url, ViewPtr view,
|
||||
const Strings& methods = { "GET" }) {
|
||||
return request_handler_.Route(regex_url, view, methods);
|
||||
}
|
||||
|
||||
bool Route(const RegexUrl& regex_url, ViewPtr view) {
|
||||
return request_handler_.Route(regex_url, view, { methods::kGet });
|
||||
}
|
||||
|
||||
// Run the loop.
|
||||
void Run();
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include "webcc/view.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
//ResponsePtr DetailView::Handle(RequestPtr request, const UrlArgs& args) {
|
||||
// if (request->method() == methods::kGet) {
|
||||
// return Get(args, UrlQuery(request->url().query()));
|
||||
// }
|
||||
//
|
||||
// if (request->method() == methods::kPut) {
|
||||
// return Put(request, args);
|
||||
// }
|
||||
//
|
||||
// if (request->method() == methods::kPatch) {
|
||||
// return Patch(request, args);
|
||||
// }
|
||||
//
|
||||
// if (request->method() == methods::kDelete) {
|
||||
// return Delete(args);
|
||||
// }
|
||||
//
|
||||
// return ResponsePtr();
|
||||
//}
|
||||
//
|
||||
//ResponsePtr DetailView::Get(const UrlArgs& args, const UrlQuery& query) {
|
||||
// return ResponsePtr();
|
||||
//}
|
||||
//
|
||||
//ResponsePtr DetailView::Put(RequestPtr request, const UrlArgs& args) {
|
||||
// return ResponsePtr();
|
||||
//}
|
||||
//
|
||||
//ResponsePtr DetailView::Patch(RequestPtr request, const UrlArgs& args) {
|
||||
// return ResponsePtr();
|
||||
//}
|
||||
//
|
||||
//ResponsePtr DetailView::Delete(const UrlArgs& args) {
|
||||
// return ResponsePtr();
|
||||
//}
|
||||
|
||||
} // namespace webcc
|
||||
Reference in New Issue
Block a user