https://github.com/venediktov/crud
Restful web-service library written in C++11 based on Boost.ASIO and CRUD handlers
https://github.com/venediktov/crud
asio asynchronous boost boost-asio boost-beast boost-library cpp11 cpp14 cpp17 crud crud-api crud-operation handlers lightweight performance restful restful-webservices web-api web-service
Last synced: about 1 year ago
JSON representation
Restful web-service library written in C++11 based on Boost.ASIO and CRUD handlers
- Host: GitHub
- URL: https://github.com/venediktov/crud
- Owner: venediktov
- Created: 2015-10-01T05:30:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-02-15T13:12:56.000Z (over 2 years ago)
- Last Synced: 2025-03-18T04:11:22.509Z (about 1 year ago)
- Topics: asio, asynchronous, boost, boost-asio, boost-beast, boost-library, cpp11, cpp14, cpp17, crud, crud-api, crud-operation, handlers, lightweight, performance, restful, restful-webservices, web-api, web-service
- Language: C++
- Homepage:
- Size: 62.5 KB
- Stars: 33
- Watchers: 4
- Forks: 5
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CRUD
High performance Restful web-service library written in C++11 based on boost.ASIO and CRUD handlers
This library supports persistent connections to achieve highest throughput and utilizes optinally regex for enpoints
[](https://travis-ci.org/venediktov/CRUD)
### Installing from GitHub
```bash
git clone https://github.com/venediktov/CRUD.git
```
### Building on Linux
```bash
$ mkdir Release
$ cd Release
$ cmake -DCMAKE_BUILD_TYPE=Release -DCRUD_WITH_EXAMPLES=1 .. -G "Unix Makefiles"
$ make -j $(nproc) install
```
### Building on Mac OS
```bash
$ mkdir Release
$ cd Release
$ cmake -DCMAKE_BUILD_TYPE=Release -DCRUD_WITH_EXAMPLES=1 .. -G "Unix Makefiles"
$ make -j $(sysctl -n hw.physicalcpu) install
```
### Running examples
```bash
$ cd Release/examples
$ ./webserver 0.0.0.0 8080 . &
$ ./simple_restful_service 0.0.0.0 8081 . &
$ ./regex_restful_service 0.0.0.0 8082 . &
$ ./persisted_regex_restful_service 0.0.0.0 8083 . &
```
### Testing output and benchmarks
```bash
curl localhost:8080
curl localhost:8081/venue_handler/RTB
curl localhost:8082/venue_handler/ANY/123
curl localhost:8083/venue_handler/ANY/123
ab -k -n 100000 -c 30 http://localhost:8081/RTB
ab -k -n 100000 -c 30 http://localhost:8082/ANY/123
ab -k -n 100000 -c 30 http://localhost:8083/ANY/123
```
### Stop all background examples
```bash
pkill -9 "persisted|restful|webserver"
```
## Utilizing API
### REST handler with regex
```C++
using regex_restful_dispatcher_t = http::crud::crud_dispatcher;
regex_restful_dispatcher_t regex_handler(".") ; //root is irrelavant for REST only used for web-server
regex_handler.crud_match(boost::regex("/venue_handler/(\w+)") )
.post([](http::server::reply & r, const http::crud::crud_match & match) {
r << "{}" << http::server::reply::flush("json") ;
std::cout << "POST group_1_match=[ << match[1] << "], request_data=" << match.data << std::endl;
});
```
### simple REST handler
```C++
// SIMPLE NO REGEX MATCH FOR POST "/venue_handler/RTB"
using simple_restful_dispatcher_t = http::crud::crud_dispatcher;
simple_restful_dispatcher_t simple_handler(".") ; //root is irrelavant for REST only used for web-server
simple_handler.crud_match(std::string("/venue_handler/RTB") )
.post([](http::server::reply & r, const http::crud::crud_match & match) {
r << "{}" << http::server::reply::flush("json") ;
std::cout << "POST request_data=" << match.data << std::endl;
});
```
### All in one go
```C++
// CREAT/READ/UPDATE/DELETE "/venue_handler/XEMDP/123"
handler.crud_match(boost::regex("/venue_handler/(\\w+)/(\\d+)") )
.put([](http::server::reply & r, const http::crud::crud_match & match)
std::cout << "CREATE request=" << match[0] << "/" << match[1] << std::endl;
r = http::server::reply::stock_reply(http::server::reply::no_content);
})
.get([](http::server::reply & r, const http::crud::crud_match & match) {
r << "name: " << match[1] << ", instance number: " << match[2]
<< http::server::reply::flush("text") ;
std::cout << "READ request=" << match[0] << std::endl;
})
.post([](http::server::reply & r, const http::crud::crud_match & match) {
r << "name: " << match[1] << ", instance number: " << match[2]
<< http::server::reply::flush("text") ;
std::cout << "UPDATE request=" << match[0] << std::endl;
std::cout << "UPDATE request_data=" << match.data << std::endl;
})
.del([](http::server::reply & r, const http::crud::crud_match & match)
std::cout << "DELETE request=" << match[0] << "/" << match[1] << std::endl;
r = http::server::reply::stock_reply(http::server::reply::no_content);
}) ;
```
### Setting up connection types
#### For keep-alive persistent connections specifiy second template argument with presistent type for connection
```C++
http::server::server server{host,port,handler};
```
#### By default server is using non-persistent connection from the library
```C++
http::server::server server{host,port,handler};
```
#### Adding CRUD as GitHub submodule to your project
Create a file .gitmodules in the root of your project with contents
```bash
[submodule "CRUD"]
path = CRUD
url = https://github.com/venediktov/CRUD.git
```
Or execute following command in your repo
```bash
git submodule add https://github.com/venediktov/CRUD
```
If you add CRUD as submodule you will have to use ```git clone --recursive``` for your repo in order to get us as dependency
You can look at GitHub subtrees instead ```git subtree add --prefix CRUD git@github.com:venediktov/CRUD.git master --squash```
## Support on Beerpay
Hey dude! Help me out for a couple of :beers:!
[](https://beerpay.io/venediktov/CRUD) [](https://beerpay.io/venediktov/CRUD?focus=wish)