Server API rework.
This commit is contained in:
@@ -50,3 +50,6 @@ target_link_libraries(file_upload_client ${EXAMPLE_LIBS})
|
||||
|
||||
add_executable(file_upload_server file_upload_server.cc)
|
||||
target_link_libraries(file_upload_server ${EXAMPLE_LIBS})
|
||||
|
||||
add_executable(static_server static_server.cc)
|
||||
target_link_libraries(static_server ${EXAMPLE_LIBS})
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
#include <string>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/response_builder.h"
|
||||
#include "webcc/server.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class FileUploadService : public webcc::Service {
|
||||
class FileUploadView : public webcc::View {
|
||||
public:
|
||||
webcc::ResponsePtr Handle(webcc::RequestPtr request,
|
||||
const webcc::UrlArgs& args) override {
|
||||
webcc::ResponsePtr Handle(webcc::RequestPtr request) override {
|
||||
if (request->method() == "POST") {
|
||||
std::cout << "files: " << request->form_parts().size() << std::endl;
|
||||
|
||||
@@ -47,10 +47,9 @@ int main(int argc, char* argv[]) {
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
// TODO: doc root
|
||||
webcc::Server server(port, workers);
|
||||
|
||||
server.Bind(std::make_shared<FileUploadService>(), "/upload", false);
|
||||
server.Route("/upload", std::make_shared<FileUploadView>(), { "POST" });
|
||||
|
||||
server.Run();
|
||||
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "boost/core/ignore_unused.hpp"
|
||||
|
||||
#include "json/json.h"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
@@ -36,18 +34,29 @@ static void Sleep(int seconds) {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookListService : public webcc::ListService {
|
||||
class BookListView : public webcc::View {
|
||||
public:
|
||||
explicit BookListService(int sleep_seconds)
|
||||
: sleep_seconds_(sleep_seconds) {
|
||||
explicit BookListView(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
|
||||
}
|
||||
|
||||
webcc::ResponsePtr Handle(webcc::RequestPtr request) override {
|
||||
if (request->method() == "GET") {
|
||||
return Get(request->query());
|
||||
}
|
||||
|
||||
if (request->method() == "POST") {
|
||||
return Post(request);
|
||||
}
|
||||
|
||||
return{};
|
||||
}
|
||||
|
||||
protected:
|
||||
// Get a list of books based on query parameters.
|
||||
webcc::ResponsePtr Get(const webcc::UrlQuery& query) override;
|
||||
webcc::ResponsePtr Get(const webcc::UrlQuery& query);
|
||||
|
||||
// Create a new book.
|
||||
webcc::ResponsePtr Post(webcc::RequestPtr request) override;
|
||||
webcc::ResponsePtr Post(webcc::RequestPtr request);
|
||||
|
||||
private:
|
||||
// Sleep some seconds before send back the response.
|
||||
@@ -59,23 +68,38 @@ private:
|
||||
|
||||
// The URL is like '/books/{BookID}', and the 'args' parameter
|
||||
// contains the matched book ID.
|
||||
class BookDetailService : public webcc::DetailService {
|
||||
class BookDetailView : public webcc::View {
|
||||
public:
|
||||
explicit BookDetailService(int sleep_seconds)
|
||||
: sleep_seconds_(sleep_seconds) {
|
||||
explicit BookDetailView(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
|
||||
}
|
||||
|
||||
webcc::ResponsePtr Handle(webcc::RequestPtr request) override {
|
||||
if (request->method() == "GET") {
|
||||
return Get(request->args(), request->query());
|
||||
}
|
||||
|
||||
if (request->method() == "PUT") {
|
||||
return Put(request, request->args());
|
||||
}
|
||||
|
||||
if (request->method() == "DELETE") {
|
||||
return Delete(request->args());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
protected:
|
||||
// Get the detailed information of a book.
|
||||
webcc::ResponsePtr Get(const webcc::UrlArgs& args,
|
||||
const webcc::UrlQuery& query) override;
|
||||
const webcc::UrlQuery& query);
|
||||
|
||||
// Update a book.
|
||||
webcc::ResponsePtr Put(webcc::RequestPtr request,
|
||||
const webcc::UrlArgs& args) override;
|
||||
const webcc::UrlArgs& args);
|
||||
|
||||
// Delete a book.
|
||||
webcc::ResponsePtr Delete(const webcc::UrlArgs& args) override;
|
||||
webcc::ResponsePtr Delete(const webcc::UrlArgs& args);
|
||||
|
||||
private:
|
||||
// Sleep some seconds before send back the response.
|
||||
@@ -86,9 +110,7 @@ private:
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Return all books as a JSON array.
|
||||
webcc::ResponsePtr BookListService::Get(const webcc::UrlQuery& query) {
|
||||
boost::ignore_unused(query);
|
||||
|
||||
webcc::ResponsePtr BookListView::Get(const webcc::UrlQuery& /*query*/) {
|
||||
Sleep(sleep_seconds_);
|
||||
|
||||
Json::Value json(Json::arrayValue);
|
||||
@@ -101,7 +123,7 @@ webcc::ResponsePtr BookListService::Get(const webcc::UrlQuery& query) {
|
||||
return webcc::ResponseBuilder{}.OK().Data(JsonToString(json)).Json()();
|
||||
}
|
||||
|
||||
webcc::ResponsePtr BookListService::Post(webcc::RequestPtr request) {
|
||||
webcc::ResponsePtr BookListView::Post(webcc::RequestPtr request) {
|
||||
Sleep(sleep_seconds_);
|
||||
|
||||
Book book;
|
||||
@@ -121,8 +143,8 @@ webcc::ResponsePtr BookListService::Post(webcc::RequestPtr request) {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
webcc::ResponsePtr BookDetailService::Get(const webcc::UrlArgs& args,
|
||||
const webcc::UrlQuery& query) {
|
||||
webcc::ResponsePtr BookDetailView::Get(const webcc::UrlArgs& args,
|
||||
const webcc::UrlQuery& query) {
|
||||
Sleep(sleep_seconds_);
|
||||
|
||||
if (args.size() != 1) {
|
||||
@@ -142,8 +164,8 @@ webcc::ResponsePtr BookDetailService::Get(const webcc::UrlArgs& args,
|
||||
return webcc::ResponseBuilder{}.OK().Data(BookToJsonString(book)).Json()();
|
||||
}
|
||||
|
||||
webcc::ResponsePtr BookDetailService::Put(webcc::RequestPtr request,
|
||||
const webcc::UrlArgs& args) {
|
||||
webcc::ResponsePtr BookDetailView::Put(webcc::RequestPtr request,
|
||||
const webcc::UrlArgs& args) {
|
||||
Sleep(sleep_seconds_);
|
||||
|
||||
if (args.size() != 1) {
|
||||
@@ -163,7 +185,7 @@ webcc::ResponsePtr BookDetailService::Put(webcc::RequestPtr request,
|
||||
return webcc::ResponseBuilder{}.OK()();
|
||||
}
|
||||
|
||||
webcc::ResponsePtr BookDetailService::Delete(const webcc::UrlArgs& args) {
|
||||
webcc::ResponsePtr BookDetailView::Delete(const webcc::UrlArgs& args) {
|
||||
Sleep(sleep_seconds_);
|
||||
|
||||
if (args.size() != 1) {
|
||||
@@ -209,14 +231,15 @@ int main(int argc, char* argv[]) {
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
// TODO: doc root
|
||||
webcc::Server server(port, workers);
|
||||
|
||||
server.Bind(std::make_shared<BookListService>(sleep_seconds),
|
||||
"/books", false);
|
||||
server.Route("/books",
|
||||
std::make_shared<BookListView>(sleep_seconds),
|
||||
{ "GET", "POST" });
|
||||
|
||||
server.Bind(std::make_shared<BookDetailService>(sleep_seconds),
|
||||
"/books/(\\d+)", true);
|
||||
server.Route(webcc::R("/books/(\\d+)"),
|
||||
std::make_shared<BookDetailView>(sleep_seconds),
|
||||
{ "GET", "PUT", "DELETE" });
|
||||
|
||||
server.Run();
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// A general HTTP server serving static files.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/server.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port> <doc_root>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080 D:/www" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 3) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
std::uint16_t port = static_cast<std::uint16_t>(std::atoi(argv[1]));
|
||||
std::string doc_root = argv[2];
|
||||
|
||||
try {
|
||||
webcc::Server server(port, 1, doc_root);
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user