A framework with the sole intention of making C++ Web API development extremely productive and easier to do. Save time by using an easy to use chainable routing interface. Work directly with MongoDB and Redis with custom built high-level drivers. Support is focused on Mac, Debian and Windows OSes and will shift primarily to Docker Containers in the future.
At first, the original intent was to create a mono framework with all key features (ie: Redis, Mongo, RabbitMQ etc). However, after some consideration about the project. I decided it was best to dial it down to focusing solely on just the router itself with this project. And creating integrations and modules for other frameworks.
Surprisingly not much is required to sign off WSF as an actual v1. After some recent review. It seems I need the following.
- Param based routing
app.route("GET", "/create/:id", app::function);
// Which is then accessible via
auto id = app.params["id"];
- Middleware
app
.use(app::function)
.route("GET", "/create/:id", app::function);
Setting up is straight forward, here's an example main function:
#include "utility/CLI.utility.hpp"
#include "utility/Router.utility.hpp"
#include "routes/Server.route.hpp"
#include "routes/Mongo.example.route.hpp"
#include "routes/Redis.example.route.hpp"
int main(int argc, char** argv) {
if (wsf::cli::parse(argc, argv) == false) {
std::cout << "There seems to be something wrong with your command line flags..." << "\n";
return 2;
}
wsf::utility::Router app;
return app.init()
// Mongo Example Routes
.route("GET", "/mongo/insert/one", wsf::route::mongo::insert::one)
.route("GET", "/mongo/insert/many", wsf::route::mongo::insert::many)
.route("GET", "/mongo/update/one", wsf::route::mongo::update::one)
.route("GET", "/mongo/update/many", wsf::route::mongo::update::many)
.route("GET", "/mongo/find/one", wsf::route::mongo::find::one)
.route("GET", "/mongo/find/many", wsf::route::mongo::find::many)
.route("GET", "/mongo/remove/one", wsf::route::mongo::remove::one)
.route("GET", "/mongo/remove/many", wsf::route::mongo::remove::many)
// Redis Example Routes
.route("GET", "/redis/set", wsf::route::redis::set)
.route("GET", "/redis/get", wsf::route::redis::get)
.route("GET", "/redis/del", wsf::route::redis::del)
.route("GET", "/redis/push", wsf::route::redis::push)
.route("GET", "/redis/list", wsf::route::redis::list)
// Default routes
.get("/", wsf::route::info)
.notFound()
.start();
}
Routes feel a lot like Express and anything similar to it. Routes are chainable or can be colon separated. In the future this same functionality can be applied for middleware and prefixing routes.
In order to handle routes. You can use the Response object and interface to interact with data. All you need to do is update the _json
response:
wsf::utility::Response info(wsf::utility::Router& app) {
wsf::utility::Response res;
res._json = wsf::service::metadata(res._json);
res._json["query"] = app.query;
return res;
}
Accessing data in responses is similar to any other framework:
web::json::value = app.query; // Request query string
web::json::value = app.body; // Request json payload
std::string = app.method; // Request method
std::string = app.path; // Request uri path
You can just go the easy way with Docker. But if you need to do more in depth tinkering. You might want to compile directly.
With docker all you need to do is:
docker build -t wsf .
And you'll be able to SSH into the container and compile with cmake accordingly.
-
CMake and Make (>=3.7)
-
Before installing the C++ drivers, you need to install the C drivers first.
-
Once the C drivers are installed you can install the C++ drivers.
-
Use hiredisx to interact with Redis hiredisx.
-
The hiredis C drivers are required for hiredisx hiredis.
CMake will always be the best way to interface with WSF. Primary support will be for Mac OS, Debian and Windows Operating Systems.
1. Create the build directory
You can build directly in the source file if you don't plan on committing to this repository.
mkdir build
cd build
2. Run cmake and make:
If you want to try the sample application, just make sure to turn BINARY=ON
with cmake.
cmake .. -DEXAMPLE=ON
make
3. Install libraries or run the server binary
If you turned the example on, then you should have a server binary that can run with just:
./example
Install the WSF library to your machine just run only this instead:
make install
4. (Optional) Add flags for drivers
Run CMake with the following optional flags:
cmake -DMONGO=ON # To enable mongodb
cmake -DREDIS=ON # To enable hiredisx
cmake -DWSS=ON # To enable websocket++
5. NOTE: sometimes you might need to specify library paths, if you do need to, you can do like so:
# Custom OpenSSL paths
cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DOPENSSL_LIBRARIES=/usr/local/opt/openssl/lib ..
# Custom HiRedis/HiRedisX path
cmake -DHIREDIS_INCLUDE_DIR=/usr/local/include/hiredis ..
cmake -DHIREDISX_INCLUDE_DIR=/usr/local/include/hiredisx ..
# Custom Websocket++ path
cmake -DWSSPP_INCLUDE_DIR=/usr/local/include/websocketpp ..
The binary also comes with custom command line options:
$ server --help
The C++ Web Service Framework for modern web services:
--help print help message
-h [ --host ] arg specify host to listen on (default is http://0.0.0.0)
-p [ --port ] arg specify port to listen on (default is 3000)
-c [ --cors ] arg specify CORS (default is * which is sometimes unsafe)
--mongo arg MongoDB url (default is mongodb://localhost:27017)
--mongodb arg MongoDB DB name (default is wsf)
--redis arg Redis url (default is localhost)
--redisPort arg Redis port (default is 6379)
--redisTimeout arg Redis timeout (default is 3 in s)
Set custom cmake flags to enable testing:
# Example flags that can be "ON" or "OFF"
cmake .. -DALL_TESTING=ON -DSTANDARD_TESTING=ON -DMONGO_TESTING=ON -DREDIS_TESTING=ON
Then just run:
make
make test
Feel free to open a PR or issue if you have any concerns in regards to the codebase.