Rework book server and client.

This commit is contained in:
Chunting Gu
2019-09-06 17:40:46 +08:00
parent f1ad97b227
commit 2a25340ce4
24 changed files with 1120 additions and 301 deletions
+14
View File
@@ -0,0 +1,14 @@
set(SRCS
book.cc
book.h
book_db.cc
book_db.h
book_json.cc
book_json.h
views.cc
views.h
main.cc
)
add_executable(book_server ${SRCS})
target_link_libraries(book_server ${EXAMPLE_LIBS} jsoncpp)
+11
View File
@@ -0,0 +1,11 @@
#include "book.h"
#include <iostream>
const Book kNullBook{};
std::ostream& operator<<(std::ostream& os, const Book& book) {
os << "{ " << book.id << ", " << book.title << ", " << book.price << ", "
<< book.photo << " }";
return os;
}
+24
View File
@@ -0,0 +1,24 @@
#ifndef BOOK_H_
#define BOOK_H_
#include <iosfwd>
#include <string>
#include "boost/filesystem/path.hpp"
struct Book {
std::string id;
std::string title;
double price;
std::string photo; // Name only
bool IsNull() const {
return id.empty();
}
};
std::ostream& operator<<(std::ostream& os, const Book& book);
extern const Book kNullBook;
#endif // BOOK_H_
+66
View File
@@ -0,0 +1,66 @@
#include "book_db.h"
#include <algorithm>
const Book& BookDB::Get(const std::string& id) const {
auto it = Find(id);
return (it == books_.end() ? kNullBook : *it);
}
std::string BookDB::Add(const Book& book) {
std::string id = NewID();
books_.push_back({ id, book.title, book.price });
return id;
}
bool BookDB::Set(const Book& book) {
auto it = Find(book.id);
if (it != books_.end()) {
it->title = book.title;
it->price = book.price;
return true;
}
return false;
}
std::string BookDB::GetPhoto(const std::string& id) const {
auto it = Find(id);
return it != books_.end() ? it->photo : "";
}
bool BookDB::SetPhoto(const std::string& id, const std::string& photo) {
auto it = Find(id);
if (it != books_.end()) {
it->photo = photo;
return true;
}
return false;
}
bool BookDB::Delete(const std::string& id) {
auto it = Find(id);
if (it != books_.end()) {
books_.erase(it);
return true;
}
return false;
}
std::list<Book>::const_iterator BookDB::Find(const std::string& id) const {
return std::find_if(books_.begin(), books_.end(),
[&id](const Book& book) { return book.id == id; });
}
std::list<Book>::iterator BookDB::Find(const std::string& id) {
return std::find_if(books_.begin(), books_.end(),
[&id](Book& book) { return book.id == id; });
}
std::string BookDB::NewID() const {
static int s_id_counter = 0;
++s_id_counter;
return std::to_string(s_id_counter);
}
+44
View File
@@ -0,0 +1,44 @@
#ifndef BOOK_DB_H_
#define BOOK_DB_H_
#include <list>
#include <string>
#include "book.h"
// Book database simulator.
// There should be some database in a real product.
class BookDB {
public:
const std::list<Book>& books() const {
return books_;
}
const Book& Get(const std::string& id) const;
// Add a book, return the ID.
// NOTE: The ID of the input book will be ignored so should be empty.
std::string Add(const Book& book);
bool Set(const Book& book);
std::string GetPhoto(const std::string& id) const;
bool SetPhoto(const std::string& id, const std::string& photo);
bool Delete(const std::string& id);
private:
std::list<Book>::const_iterator Find(const std::string& id) const;
std::list<Book>::iterator Find(const std::string& id);
// Allocate a new book ID.
std::string NewID() const;
private:
std::list<Book> books_;
};
#endif // BOOK_DB_H_
+57
View File
@@ -0,0 +1,57 @@
#include "book_json.h"
#include <iostream>
#include <sstream>
#include "json/json.h"
#include "book.h"
std::string JsonToString(const Json::Value& json) {
Json::StreamWriterBuilder builder;
return Json::writeString(builder, json);
}
Json::Value StringToJson(const std::string& str) {
Json::Value json;
Json::CharReaderBuilder builder;
std::stringstream stream(str);
std::string errs;
if (!Json::parseFromStream(builder, stream, &json, &errs)) {
std::cerr << errs << std::endl;
}
return json;
}
Json::Value BookToJson(const Book& book) {
Json::Value json;
json["id"] = book.id;
json["title"] = book.title;
json["price"] = book.price;
return json;
}
Book JsonToBook(const Json::Value& json) {
return {
json["id"].asString(),
json["title"].asString(),
json["price"].asDouble(),
};
}
std::string BookToJsonString(const Book& book) {
return JsonToString(BookToJson(book));
}
bool JsonStringToBook(const std::string& json_str, Book* book) {
Json::Value json = StringToJson(json_str);
if (!json) {
return false;
}
*book = JsonToBook(json);
return true;
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef BOOK_JSON_H_
#define BOOK_JSON_H_
#include <string>
#include "json/json-forwards.h"
struct Book;
std::string JsonToString(const Json::Value& json);
Json::Value StringToJson(const std::string& str);
Json::Value BookToJson(const Book& book);
Book JsonToBook(const Json::Value& json);
std::string BookToJsonString(const Book& book);
bool JsonStringToBook(const std::string& json_str, Book* book);
#endif // BOOK_JSON_H_
+68
View File
@@ -0,0 +1,68 @@
#include <iostream>
#include "boost/filesystem/operations.hpp"
#include "webcc/logger.h"
#include "webcc/server.h"
#include "views.h"
// Memory leak detection with VLD.
#if (defined(_WIN32) || defined(_WIN64))
#if defined(_DEBUG) && defined(WEBCC_ENABLE_VLD)
#pragma message ("< include vld.h >")
#include "vld/vld.h"
#pragma comment(lib, "vld")
#endif
#endif
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "usage: book_server <port> <upload_dir>" << std::endl;
std::cout << "e.g.," << std::endl;
std::cout << " $ book_server 8080 D:/upload" << std::endl;
return 1;
}
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
std::uint16_t port = static_cast<std::uint16_t>(std::atoi(argv[1]));
bfs::path upload_dir = argv[2];
if (!bfs::is_directory(upload_dir) || !bfs::exists(upload_dir)) {
std::cerr << "Invalid upload dir!" << std::endl;
return 1;
}
// Add a sub-dir for book photos.
bfs::path photo_dir = upload_dir / "books";
if (!bfs::exists(photo_dir)) {
bfs::create_directory(photo_dir);
}
std::cout << "Book photos will be saved to: " << photo_dir << std::endl;
try {
webcc::Server server(port); // No doc root
server.Route("/books",
std::make_shared<BookListView>(),
{ "GET", "POST" });
server.Route(webcc::R("/books/(\\d+)"),
std::make_shared<BookDetailView>(photo_dir),
{ "GET", "PUT", "DELETE" });
server.Route(webcc::R("/books/(\\d+)/photo"),
std::make_shared<BookPhotoView>(photo_dir),
{ "GET", "PUT", "DELETE" });
server.Run(2);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
+223
View File
@@ -0,0 +1,223 @@
#include "views.h"
#include "json/json.h"
#include "boost/filesystem/operations.hpp"
#include "webcc/response_builder.h"
#include "book.h"
#include "book_db.h"
#include "book_json.h"
// -----------------------------------------------------------------------------
static BookDB g_book_db;
// -----------------------------------------------------------------------------
webcc::ResponsePtr BookListView::Handle(webcc::RequestPtr request) {
if (request->method() == "GET") {
return Get(request);
}
if (request->method() == "POST") {
return Post(request);
}
return {};
}
webcc::ResponsePtr BookListView::Get(webcc::RequestPtr request) {
Json::Value json(Json::arrayValue);
for (const Book& book : g_book_db.books()) {
json.append(BookToJson(book));
}
// Return all books as a JSON array.
return webcc::ResponseBuilder{}.OK().Body(JsonToString(json)).Json().Utf8()();
}
webcc::ResponsePtr BookListView::Post(webcc::RequestPtr request) {
Book book;
if (JsonStringToBook(request->data(), &book)) {
std::string id = g_book_db.Add(book);
Json::Value json;
json["id"] = id;
return webcc::ResponseBuilder{}.Created().Body(JsonToString(json)).Json().Utf8()();
} else {
// Invalid JSON
return webcc::ResponseBuilder{}.BadRequest()();
}
}
// -----------------------------------------------------------------------------
BookDetailView::BookDetailView(bfs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
webcc::ResponsePtr BookDetailView::Handle(webcc::RequestPtr request) {
if (request->method() == "GET") {
return Get(request);
}
if (request->method() == "PUT") {
return Put(request);
}
if (request->method() == "DELETE") {
return Delete(request);
}
return {};
}
webcc::ResponsePtr BookDetailView::Get(webcc::RequestPtr request) {
if (request->args().size() != 1) {
// NotFound means the resource specified by the URL cannot be found.
// BadRequest could be another choice.
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull()) {
return webcc::ResponseBuilder{}.NotFound()();
}
return webcc::ResponseBuilder{}.OK().Body(BookToJsonString(book)).Json().Utf8()();
}
webcc::ResponsePtr BookDetailView::Put(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
Book book;
if (!JsonStringToBook(request->data(), &book)) {
return webcc::ResponseBuilder{}.BadRequest()();
}
book.id = id;
g_book_db.Set(book);
return webcc::ResponseBuilder{}.OK()();
}
webcc::ResponsePtr BookDetailView::Delete(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
std::string photo_name = g_book_db.GetPhoto(id);
// Delete the book from DB.
if (!g_book_db.Delete(id)) {
return webcc::ResponseBuilder{}.NotFound()();
}
// Delete the photo from file system.
if (!photo_name.empty()) {
boost::system::error_code ec;
bfs::remove(photo_dir_ / photo_name, ec);
}
return webcc::ResponseBuilder{}.OK()();
}
// -----------------------------------------------------------------------------
BookPhotoView::BookPhotoView(bfs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
webcc::ResponsePtr BookPhotoView::Handle(webcc::RequestPtr request) {
if (request->method() == "GET") {
return Get(request);
}
if (request->method() == "PUT") {
return Put(request);
}
if (request->method() == "DELETE") {
return Delete(request);
}
return {};
}
// TODO: Check content type to see if it's JPEG.
webcc::ResponsePtr BookPhotoView::Get(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull() || book.photo.empty()) {
return webcc::ResponseBuilder{}.NotFound()();
}
bfs::path photo_path = photo_dir_ / book.photo;
if (!bfs::exists(photo_path)) {
return webcc::ResponseBuilder{}.NotFound()();
}
// File() might throw Error::kFileError.
// TODO: Avoid exception handling.
try {
return webcc::ResponseBuilder{}.OK().File(photo_path)();
} catch (const webcc::Error&) {
return webcc::ResponseBuilder{}.NotFound()();
}
}
webcc::ResponsePtr BookPhotoView::Put(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull()) {
return webcc::ResponseBuilder{}.NotFound()();
}
// Use ID as the name of the photo.
// You can also use a UUID or any unique string as the name.
auto photo_name = id + ".jpg";
if (!request->file_body()->Move(photo_dir_ / photo_name)) {
return webcc::ResponseBuilder{}.InternalServerError()();
}
// Set photo name to DB.
if (!g_book_db.SetPhoto(id, photo_name)) {
return webcc::ResponseBuilder{}.InternalServerError()();
}
return webcc::ResponseBuilder{}.OK()();
}
webcc::ResponsePtr BookPhotoView::Delete(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull() || book.photo.empty()) {
return webcc::ResponseBuilder{}.NotFound()();
}
// Error handling is simplified.
boost::system::error_code ec;
bfs::remove(photo_dir_ / book.photo, ec);
return webcc::ResponseBuilder{}.OK()();
}
+76
View File
@@ -0,0 +1,76 @@
#ifndef VIEWS_H_
#define VIEWS_H_
#include "webcc/view.h"
#include "boost/filesystem/path.hpp"
namespace bfs = boost::filesystem;
// -----------------------------------------------------------------------------
// URL: /books
class BookListView : public webcc::View {
public:
webcc::ResponsePtr Handle(webcc::RequestPtr request) override;
private:
// Get a list of books based on query parameters.
webcc::ResponsePtr Get(webcc::RequestPtr request);
// Create a new book.
webcc::ResponsePtr Post(webcc::RequestPtr request);
};
// -----------------------------------------------------------------------------
// URL: /books/{id}
class BookDetailView : public webcc::View {
public:
explicit BookDetailView(bfs::path photo_dir);
webcc::ResponsePtr Handle(webcc::RequestPtr request) override;
private:
// Get the detailed information of a book.
webcc::ResponsePtr Get(webcc::RequestPtr request);
// Update a book.
webcc::ResponsePtr Put(webcc::RequestPtr request);
// Delete a book.
webcc::ResponsePtr Delete(webcc::RequestPtr request);
private:
bfs::path photo_dir_;
};
// -----------------------------------------------------------------------------
// URL: /books/{id}/photo
class BookPhotoView : public webcc::View {
public:
explicit BookPhotoView(bfs::path photo_dir);
webcc::ResponsePtr Handle(webcc::RequestPtr request) override;
// Stream the request data, an image, of PUT into a temp file.
bool Stream(const std::string& method) override {
return method == "PUT";
}
private:
// Get the photo of the book.
webcc::ResponsePtr Get(webcc::RequestPtr request);
// Set the photo of the book.
webcc::ResponsePtr Put(webcc::RequestPtr request);
// Delete the photo of the book.
webcc::ResponsePtr Delete(webcc::RequestPtr request);
private:
bfs::path photo_dir_;
};
#endif // VIEWS_H_