Git Product home page Git Product logo

wsf's Introduction

The C++ Web Service Framework

License Build Status codecov C++ Standard CMake

Summary

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.

The New Vision

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.

Project Development

Surprisingly not much is required to sign off WSF as an actual v1. After some recent review. It seems I need the following.

  1. Param based routing
app.route("GET", "/create/:id", app::function);
// Which is then accessible via
auto id = app.params["id"];
  1. Middleware
app
    .use(app::function)
    .route("GET", "/create/:id", app::function);

How it works

An intuitive and simple router

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

Requirements for Compilation

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.

Required for build

  1. C++ REST SDK

  2. Boost Core

  3. OpenSSL

  4. CMake and Make (>=3.7)

Optional Requirements

  1. MongoDB
  1. Redis
  • Use hiredisx to interact with Redis hiredisx.

  • The hiredis C drivers are required for hiredisx hiredis.

Building with CMake

CMake will always be the best way to interface with WSF. Primary support will be for Mac OS, Debian and Windows Operating Systems.

Standard Build

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 ..

Custom binary flags

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)

Testing

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

Need help?

Feel free to open a PR or issue if you have any concerns in regards to the codebase.

wsf's People

Contributors

chriscates avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.