Refine folder structure; add rapidjson as submodule.

This commit is contained in:
Adam Gu
2018-04-10 17:34:00 +08:00
parent 9abf8d99d5
commit 5bc988b093
61 changed files with 43 additions and 28 deletions
+10
View File
@@ -0,0 +1,10 @@
if(UNIX)
add_definitions(-std=c++11)
endif()
file(GLOB SRCS *.cc *.h)
add_executable(rest_book_client ${SRCS})
target_link_libraries(rest_book_client webcc ${Boost_LIBRARIES})
target_link_libraries(rest_book_client "${CMAKE_THREAD_LIBS_INIT}")
+87
View File
@@ -0,0 +1,87 @@
#include <iostream>
#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;
}
+10
View File
@@ -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}")
+161
View File
@@ -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;
}
+48
View File
@@ -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_
+43
View File
@@ -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;
}
+11
View File
@@ -0,0 +1,11 @@
if(UNIX)
add_definitions(-std=c++11)
endif()
file(GLOB SRCS *.cc *.h)
add_executable(soap_calc_client ${SRCS})
target_link_libraries(soap_calc_client webcc pugixml ${Boost_LIBRARIES})
target_link_libraries(soap_calc_client "${CMAKE_THREAD_LIBS_INIT}")
+75
View File
@@ -0,0 +1,75 @@
#include "calc_client.h"
#include <iostream>
#include "boost/lexical_cast.hpp"
CalcClient::CalcClient() {
Init();
}
bool CalcClient::Add(double x, double y, double* result) {
return Calc("add", "x", "y", x, y, result);
}
bool CalcClient::Subtract(double x, double y, double* result) {
return Calc("subtract", "x", "y", x, y, result);
}
bool CalcClient::Multiply(double x, double y, double* result) {
return Calc("multiply", "x", "y", x, y, result);
}
bool CalcClient::Divide(double x, double y, double* result) {
return Calc("divide", "numerator", "denominator", x, y, result);
}
// Set to 0 to test our own calculator server created with webcc.
#define ACCESS_PARASOFT 0
void CalcClient::Init() {
#if ACCESS_PARASOFT
url_ = "/glue/calculator";
host_ = "ws1.parasoft.com";
port_ = ""; // Default to "80".
service_ns_ = { "cal", "http://www.parasoft.com/wsdl/calculator/" };
result_name_ = "Result";
#else
url_ = "/calculator";
host_ = "localhost";
port_ = "8080";
service_ns_ = { "ser", "http://www.example.com/calculator/" };
result_name_ = "Result";
#endif
}
bool CalcClient::Calc(const std::string& operation,
const std::string& x_name,
const std::string& y_name,
double x,
double y,
double* result) {
// Prepare parameters.
std::vector<webcc::Parameter> parameters{
{ x_name, x },
{ y_name, y }
};
// Make the call.
std::string result_str;
webcc::Error error = Call(operation, std::move(parameters), &result_str);
// Error handling if any.
if (error != webcc::kNoError) {
std::cerr << "Error: " << error;
std::cerr << ", " << webcc::GetErrorMessage(error) << std::endl;
return false;
}
// Convert the result from string to double.
try {
*result = boost::lexical_cast<double>(result_str);
} catch (boost::bad_lexical_cast&) {
return false;
}
return true;
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef CALC_CLIENT_H_
#define CALC_CLIENT_H_
#include <string>
#include "webcc/soap_client.h"
class CalcClient : public webcc::SoapClient {
public:
CalcClient();
bool Add(double x, double y, double* result);
bool Subtract(double x, double y, double* result);
bool Multiply(double x, double y, double* result);
bool Divide(double x, double y, double* result);
protected:
void Init();
// A more concrete wrapper to make a call.
bool Calc(const std::string& operation,
const std::string& x_name,
const std::string& y_name,
double x,
double y,
double* result);
};
#endif // CALC_CLIENT_H_
+250
View File
@@ -0,0 +1,250 @@
<?xml version="1.0" encoding="US-ASCII"?>
<!--generated by GLUE Standard 4.1.2 on Fri Nov 21 13:50:48 PST 2003-->
<wsdl:definitions name="Calculator"
targetNamespace="http://www.parasoft.com/wsdl/calculator/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tme="http://www.themindelectric.com/"
xmlns:tns="http://www.parasoft.com/wsdl/calculator/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema
elementFormDefault="qualified"
targetNamespace="http://www.parasoft.com/wsdl/calculator/">
<xsd:element
name="add">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="x" type="xsd:float"/>
<xsd:element name="y"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="addResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="divide">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="numerator" type="xsd:float"/>
<xsd:element
name="denominator"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="divideResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="multiply">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="x" type="xsd:float"/>
<xsd:element name="y"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="multiplyResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="subtract">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="x" type="xsd:float"/>
<xsd:element name="y"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="subtractResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message
name="add0In">
<wsdl:part element="tns:add"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="add0Out">
<wsdl:part element="tns:addResponse"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="divide1In">
<wsdl:part element="tns:divide"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="divide1Out">
<wsdl:part element="tns:divideResponse"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="multiply2In">
<wsdl:part element="tns:multiply"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="multiply2Out">
<wsdl:part element="tns:multiplyResponse"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="subtract3In">
<wsdl:part element="tns:subtract"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="subtract3Out">
<wsdl:part element="tns:subtractResponse"
name="parameters"/>
</wsdl:message>
<wsdl:portType
name="ICalculator">
<wsdl:operation name="add"
parameterOrder="x y">
<wsdl:input message="tns:add0In"
name="add0In"/>
<wsdl:output message="tns:add0Out"
name="add0Out"/>
</wsdl:operation>
<wsdl:operation name="divide"
parameterOrder="numerator denominator">
<wsdl:input
message="tns:divide1In" name="divide1In"/>
<wsdl:output
message="tns:divide1Out"
name="divide1Out"/>
</wsdl:operation>
<wsdl:operation
name="multiply" parameterOrder="x y">
<wsdl:input
message="tns:multiply2In" name="multiply2In"/>
<wsdl:output
message="tns:multiply2Out"
name="multiply2Out"/>
</wsdl:operation>
<wsdl:operation
name="subtract" parameterOrder="x y">
<wsdl:input
message="tns:subtract3In" name="subtract3In"/>
<wsdl:output
message="tns:subtract3Out"
name="subtract3Out"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding
name="ICalculator" type="tns:ICalculator">
<soap:binding
style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation
name="add">
<soap:operation soapAction="add"
style="document"/>
<wsdl:input name="add0In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="add0Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation
name="divide">
<soap:operation soapAction="divide"
style="document"/>
<wsdl:input name="divide1In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="divide1Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation
name="multiply">
<soap:operation soapAction="multiply"
style="document"/>
<wsdl:input name="multiply2In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="multiply2Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation
name="subtract">
<soap:operation soapAction="subtract"
style="document"/>
<wsdl:input name="subtract3In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="subtract3Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service
name="Calculator">
<wsdl:documentation>instance of class webtool.soap.examples.calculator.Calculator</wsdl:documentation>
<wsdl:port
binding="tns:ICalculator" name="ICalculator">
<soap:address
location="http://ws1.parasoft.com/glue/calculator"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
+30
View File
@@ -0,0 +1,30 @@
#include <iostream>
#include "calc_client.h"
int main() {
CalcClient calc;
double x = 1.0;
double y = 2.0;
double result = 0.0;
if (calc.Add(x, y, &result)) {
printf("add: %.1f\n", result);
}
#if 0
if (calc.Subtract(x, y, &result)) {
printf("subtract: %.1f\n", result);
}
if (calc.Multiply(x, y, &result)) {
printf("multiply: %.1f\n", result);
}
if (calc.Divide(x, y, &result)) {
printf("divide: %.1f\n", result);
}
#endif
return 0;
}
+10
View File
@@ -0,0 +1,10 @@
if(UNIX)
add_definitions(-std=c++11)
endif()
file(GLOB SRCS *.cc *.h)
add_executable(soap_calc_server ${SRCS})
target_link_libraries(soap_calc_server webcc pugixml ${Boost_LIBRARIES})
target_link_libraries(soap_calc_server "${CMAKE_THREAD_LIBS_INIT}")
+36
View File
@@ -0,0 +1,36 @@
#include "calc_service.h"
#include "boost/lexical_cast.hpp"
#include "webcc/soap_request.h"
#include "webcc/soap_response.h"
bool CalcService::Handle(const webcc::SoapRequest& soap_request,
webcc::SoapResponse* soap_response) {
try {
if (soap_request.operation() == "add") {
double x = boost::lexical_cast<double>(soap_request.GetParameter("x"));
double y = boost::lexical_cast<double>(soap_request.GetParameter("y"));
double result = x + y;
soap_response->set_soapenv_ns(webcc::kSoapEnvNamespace);
soap_response->set_service_ns({
"cal",
"http://www.example.com/calculator/"
});
soap_response->set_operation(soap_request.operation());
soap_response->set_result_name("Result");
soap_response->set_result(std::to_string(result));
return true;
} else {
// NOT_IMPLEMENTED
}
} catch (boost::bad_lexical_cast&) {
// BAD_REQUEST
}
return false;
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef CALC_SERVICE_H_
#define CALC_SERVICE_H_
#include "webcc/soap_service.h"
class CalcService : public webcc::SoapService {
public:
CalcService() = default;
~CalcService() override = default;
bool Handle(const webcc::SoapRequest& soap_request,
webcc::SoapResponse* soap_response) override;
};
#endif // CALC_SERVICE_H_
+35
View File
@@ -0,0 +1,35 @@
#include <iostream>
#include "webcc/soap_server.h"
#include "calc_service.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::SoapServer server(port, workers);
server.RegisterService(std::make_shared<CalcService>(),
"/calculator");
server.Run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}