Refine folder structure; add rapidjson as submodule.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
if(UNIX)
|
||||
add_definitions(-std=c++11)
|
||||
endif()
|
||||
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(rest_book_server ${SRCS})
|
||||
|
||||
target_link_libraries(rest_book_server webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(rest_book_server "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -0,0 +1,161 @@
|
||||
#include "book_services.h"
|
||||
|
||||
#include <list>
|
||||
|
||||
#include "boost/lexical_cast.hpp"
|
||||
#include "boost/thread.hpp"
|
||||
#include "boost/date_time/posix_time/posix_time.hpp"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// In-memory test data.
|
||||
// There should be some database in a real product.
|
||||
|
||||
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() {
|
||||
// Prepare test data.
|
||||
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 = FindBook(id);
|
||||
return (it == books_.end() ? kNullBook : *it);
|
||||
}
|
||||
|
||||
bool AddBook(const Book& new_book) {
|
||||
if (FindBook(new_book.id) == books_.end()) {
|
||||
books_.push_back(new_book);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DeleteBook(const std::string& id) {
|
||||
auto it = FindBook(id);
|
||||
|
||||
if (it != books_.end()) {
|
||||
books_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<Book>::const_iterator FindBook(const std::string& id) const {
|
||||
return std::find_if(books_.begin(),
|
||||
books_.end(),
|
||||
[&id](const Book& book) { return book.id == id; });
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<Book> books_;
|
||||
};
|
||||
|
||||
static BookStore g_book_store;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Naively create JSON object for a book.
|
||||
// You should use real JSON library in real product.
|
||||
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;
|
||||
}
|
||||
|
||||
// Naively create JSON array object for a list of books.
|
||||
// You should use real JSON library in real product.
|
||||
static std::string CreateBookListJson(const std::list<Book>& books) {
|
||||
std::string json = "[ ";
|
||||
|
||||
for (const Book& book : books) {
|
||||
json += CreateBookJson(book);
|
||||
json += ",";
|
||||
}
|
||||
|
||||
// Remove last ','.
|
||||
if (!books.empty()) {
|
||||
json[json.size() - 1] = ' ';
|
||||
}
|
||||
|
||||
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 = CreateBookListJson(g_book_store.books());
|
||||
|
||||
// Sleep for testing timeout control.
|
||||
//boost::this_thread::sleep(boost::posix_time::seconds(2));
|
||||
|
||||
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);
|
||||
|
||||
// Sleep for testing timeout control.
|
||||
//boost::this_thread::sleep(boost::posix_time::seconds(2));
|
||||
|
||||
return true;
|
||||
|
||||
} else if (http_method == webcc::kHttpPost) {
|
||||
|
||||
} else if (http_method == webcc::kHttpDelete) {
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#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,43 @@
|
||||
#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+)");
|
||||
|
||||
// For test purpose.
|
||||
// Timeout like 60s makes more sense in a real product.
|
||||
// Leave it as default (0) for no timeout control.
|
||||
server.set_timeout_seconds(1);
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user