Merge branch 'master' of https://github.com/sprinfall/webcc
This commit is contained in:
@@ -2,99 +2,13 @@
|
||||
|
||||
A lightweight C++ REST and SOAP client and server library based on *Boost.Asio*.
|
||||
|
||||
Please turn to our [Wiki](https://github.com/sprinfall/webcc/wiki) for more tutorials and guides.
|
||||
Please turn to our [Wiki](https://github.com/sprinfall/webcc/wiki) for more tutorials and guides, or just follow the links below:
|
||||
|
||||
## Quick Start
|
||||
|
||||
### REST Server
|
||||
|
||||
Suppose you want to create a book server, and provide the following operations with RESTful API:
|
||||
|
||||
- Query books based on some criterias.
|
||||
- Add a new book.
|
||||
- Get the detailed information of a book.
|
||||
- Update the information of a book.
|
||||
- Delete a book.
|
||||
|
||||
The first two operations can be implemented by deriving from `webcc::RestListService`:
|
||||
|
||||
```cpp
|
||||
class BookListService : public webcc::RestListService {
|
||||
protected:
|
||||
// Get a list of books based on query parameters.
|
||||
void Get(const webcc::UrlQuery& query,
|
||||
webcc::RestResponse* response) final;
|
||||
|
||||
// Create a new book.
|
||||
// The new book's data is attached as request content in JSON format.
|
||||
void Post(const std::string& request_content,
|
||||
webcc::RestResponse* response) final;
|
||||
};
|
||||
```
|
||||
|
||||
The others, derive from `webcc::RestDetailService`:
|
||||
|
||||
```cpp
|
||||
// The URL is like '/books/{BookID}', and the 'url_sub_matches' parameter
|
||||
// contains the matched book ID.
|
||||
class BookDetailService : public webcc::RestDetailService {
|
||||
protected:
|
||||
// Get the detailed information of a book.
|
||||
void Get(const webcc::UrlSubMatches& url_sub_matches,
|
||||
const webcc::UrlQuery& query,
|
||||
webcc::RestResponse* response) final;
|
||||
|
||||
// Update a book.
|
||||
void Put(const webcc::UrlSubMatches& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
webcc::RestResponse* response) final;
|
||||
|
||||
// Delete a book.
|
||||
void Delete(const webcc::UrlSubMatches& url_sub_matches,
|
||||
webcc::RestResponse* response) final;
|
||||
};
|
||||
```
|
||||
|
||||
As you can see, all you have to do is to override the proper virtual functions which are named after HTTP methods.
|
||||
|
||||
The detailed implementation is out of the scope of this document, but here is an example:
|
||||
|
||||
```cpp
|
||||
void BookDetailService::Get(const webcc::UrlSubMatches& url_sub_matches,
|
||||
const webcc::UrlQuery& query,
|
||||
webcc::RestResponse* response) {
|
||||
if (url_sub_matches.size() != 1) {
|
||||
// Invalid URL.
|
||||
response->status = webcc::HttpStatus::kBadRequest;
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string& book_id = url_sub_matches[0];
|
||||
|
||||
// Get the book by ID from, e.g., database.
|
||||
// ...
|
||||
|
||||
if (<NotFound>) {
|
||||
response->status = webcc::HttpStatus::kNotFound;
|
||||
} else {
|
||||
response->content = <JsonStringOfTheBook>;
|
||||
response->status = webcc::HttpStatus::kOK;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Last step, bind the services and run the server:
|
||||
|
||||
```cpp
|
||||
webcc::RestServer server(8080, 2);
|
||||
|
||||
server.Bind(std::make_shared<BookListService>(), "/books", false);
|
||||
server.Bind(std::make_shared<BookDetailService>(), "/books/(\\d+)", true);
|
||||
|
||||
server.Run();
|
||||
```
|
||||
|
||||
Please see `example/rest_book_server` for the complete example.
|
||||
- [Integrate Into Your Project](https://github.com/sprinfall/webcc/wiki/Integrate-Into-Your-Project)
|
||||
- [Logging](https://github.com/sprinfall/webcc/wiki/Logging)
|
||||
- [SOAP Client Tutorial](https://github.com/sprinfall/webcc/wiki/SOAP-Client-Tutorial)
|
||||
- [SOAP Server Tutorial](https://github.com/sprinfall/webcc/wiki/SOAP-Server-Tutorial)
|
||||
- [REST Server Tutorial](https://github.com/sprinfall/webcc/wiki/REST-Server-Tutorial)
|
||||
|
||||
## Build Instructions
|
||||
|
||||
|
||||
Reference in New Issue
Block a user