Refine REST support; add demo for REST client and server.
parent
e04565a468
commit
c557430b8e
@ -0,0 +1,5 @@
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(rest_book_client ${SRCS})
|
||||
|
||||
target_link_libraries(rest_book_client webcc)
|
@ -0,0 +1,2 @@
|
||||
#include "book_client.h"
|
||||
|
@ -0,0 +1,6 @@
|
||||
#ifndef BOOK_CLIENT_H_
|
||||
#define BOOK_CLIENT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#endif // BOOK_CLIENT_H_
|
@ -0,0 +1,88 @@
|
||||
#include <iostream>
|
||||
#include "boost/lexical_cast.hpp"
|
||||
|
||||
#include "webcc/http_client.h"
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_response.h"
|
||||
|
||||
class BookListClient {
|
||||
public:
|
||||
BookListClient() {
|
||||
host_ = "localhost";
|
||||
port_ = "8080";
|
||||
}
|
||||
|
||||
bool ListBooks() {
|
||||
webcc::HttpRequest http_request;
|
||||
|
||||
http_request.set_method(webcc::kHttpGet);
|
||||
http_request.set_url("/books");
|
||||
http_request.SetHost(host_, port_);
|
||||
|
||||
http_request.Build();
|
||||
|
||||
webcc::HttpResponse http_response;
|
||||
|
||||
webcc::HttpClient http_client;
|
||||
webcc::Error error = http_client.SendRequest(http_request, &http_response);
|
||||
|
||||
if (error != webcc::kNoError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "Book list: " << std::endl
|
||||
<< http_response.content() << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string host_;
|
||||
std::string port_;
|
||||
};
|
||||
|
||||
class BookDetailClient {
|
||||
public:
|
||||
BookDetailClient() {
|
||||
host_ = "localhost";
|
||||
port_ = "8080";
|
||||
}
|
||||
|
||||
bool GetBook(const std::string& id) {
|
||||
webcc::HttpRequest http_request;
|
||||
|
||||
http_request.set_method(webcc::kHttpGet);
|
||||
http_request.set_url("/books/" + id);
|
||||
http_request.SetHost(host_, port_);
|
||||
|
||||
http_request.Build();
|
||||
|
||||
webcc::HttpResponse http_response;
|
||||
|
||||
webcc::HttpClient http_client;
|
||||
webcc::Error error = http_client.SendRequest(http_request, &http_response);
|
||||
|
||||
if (error != webcc::kNoError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "Book: " << id << std::endl
|
||||
<< http_response.content() << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string host_;
|
||||
std::string port_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
BookListClient book_list_client;
|
||||
book_list_client.ListBooks();
|
||||
|
||||
BookDetailClient book_detail_client;
|
||||
book_detail_client.GetBook("1");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(rest_book_server ${SRCS})
|
||||
|
||||
target_link_libraries(rest_book_server webcc)
|
@ -0,0 +1,138 @@
|
||||
#include "book_services.h"
|
||||
|
||||
#include <list>
|
||||
#include "boost/lexical_cast.hpp"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Book {
|
||||
public:
|
||||
std::string id;
|
||||
std::string title;
|
||||
double price;
|
||||
|
||||
bool IsNull() const {
|
||||
return id.empty();
|
||||
}
|
||||
};
|
||||
|
||||
static const Book kNullBook{};
|
||||
|
||||
class BookStore {
|
||||
public:
|
||||
BookStore() {
|
||||
books_.push_back({ "1", "Title1", 11.1 });
|
||||
books_.push_back({ "2", "Title2", 22.2 });
|
||||
books_.push_back({ "3", "Title3", 33.3 });
|
||||
}
|
||||
|
||||
const std::list<Book>& books() const {
|
||||
return books_;
|
||||
}
|
||||
|
||||
const Book& GetBook(const std::string& id) const {
|
||||
auto it = std::find_if(books_.begin(),
|
||||
books_.end(),
|
||||
[&id](const Book& book) { return book.id == id; });
|
||||
|
||||
if (it == books_.end()) {
|
||||
return kNullBook;
|
||||
}
|
||||
|
||||
return *it;
|
||||
}
|
||||
|
||||
bool AddBook(const Book& new_book) {
|
||||
auto it = std::find_if(
|
||||
books_.begin(),
|
||||
books_.end(),
|
||||
[&new_book](const Book& book) { return book.id == new_book.id; });
|
||||
|
||||
if (it != books_.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
books_.push_back(new_book);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeleteBook(const std::string& id) {
|
||||
auto it = std::find_if(books_.begin(),
|
||||
books_.end(),
|
||||
[&id](const Book& book) { return book.id == id; });
|
||||
|
||||
if (it == books_.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
books_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<Book> books_;
|
||||
};
|
||||
|
||||
static BookStore g_book_store;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Naively create JSON object string for a book.
|
||||
// You should use real JSON library in your product code.
|
||||
static std::string CreateBookJson(const Book& book) {
|
||||
std::string json = "{ ";
|
||||
json += "\"id\": " + book.id + ", ";
|
||||
json += "\"title\": " + book.title + ", ";
|
||||
json += "\"price\": " + std::to_string(book.price);
|
||||
json += " }";
|
||||
return json;
|
||||
}
|
||||
|
||||
bool BookListService::Handle(const std::string& http_method,
|
||||
const std::vector<std::string>& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
std::string* response_content) {
|
||||
if (http_method == webcc::kHttpGet) {
|
||||
*response_content = "{ ";
|
||||
for (const Book& book : g_book_store.books()) {
|
||||
*response_content += CreateBookJson(book);
|
||||
*response_content += ",";
|
||||
}
|
||||
*response_content += " }";
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool BookDetailService::Handle(const std::string& http_method,
|
||||
const std::vector<std::string>& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
std::string* response_content) {
|
||||
if (url_sub_matches.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& book_id = url_sub_matches[0];
|
||||
|
||||
if (http_method == webcc::kHttpGet) {
|
||||
const Book& book = g_book_store.GetBook(book_id);
|
||||
|
||||
if (book.IsNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*response_content = CreateBookJson(book);
|
||||
|
||||
return true;
|
||||
|
||||
} else if (http_method == webcc::kHttpPost) {
|
||||
|
||||
} else if (http_method == webcc::kHttpDelete) {
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
#ifndef BOOK_SERVICES_H_
|
||||
#define BOOK_SERVICES_H_
|
||||
|
||||
#include "webcc/rest_service.h"
|
||||
|
||||
// NOTE:
|
||||
// XxxListService and XxxDetailService are similar to the XxxListView
|
||||
// and XxxDetailView in Django (a Python web framework).
|
||||
|
||||
// List Service handles the HTTP GET and returns the book list based on
|
||||
// query parameters specified in the URL.
|
||||
// The URL should be like:
|
||||
// - /books
|
||||
// - /books?name={BookName}
|
||||
// The query parameters could be regular expressions.
|
||||
class BookListService : public webcc::RestService {
|
||||
public:
|
||||
BookListService() = default;
|
||||
~BookListService() override = default;
|
||||
|
||||
bool Handle(const std::string& http_method,
|
||||
const std::vector<std::string>& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
std::string* response_content) override;
|
||||
};
|
||||
|
||||
// Detail Service handles the following HTTP methods:
|
||||
// - GET
|
||||
// - PUT
|
||||
// - PATCH
|
||||
// - DELETE
|
||||
// The URL should be like: /books/{BookID}.
|
||||
class BookDetailService : public webcc::RestService {
|
||||
public:
|
||||
BookDetailService() = default;
|
||||
~BookDetailService() override = default;
|
||||
|
||||
bool Handle(const std::string& http_method,
|
||||
const std::vector<std::string>& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
std::string* response_content) override;
|
||||
};
|
||||
|
||||
#endif // BOOK_SERVICE_H_
|
@ -0,0 +1,38 @@
|
||||
#include <iostream>
|
||||
#include "webcc/rest_server.h"
|
||||
#include "book_services.h"
|
||||
|
||||
static void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 2) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned short port = std::atoi(argv[1]);
|
||||
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
webcc::RestServer server(port, workers);
|
||||
|
||||
server.RegisterService(std::make_shared<BookListService>(),
|
||||
"/books");
|
||||
|
||||
server.RegisterService(std::make_shared<BookDetailService>(),
|
||||
"/books/(\\d+)");
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue