Skip to content

lewisgcm/mvcpp

Repository files navigation

MVC++ Fast C++ HTTP Networking Library

Build Status Coverage Status License Type

C++ library for HTTP based network applications including additional MVC helpers and routing. This library was designed for simplicity, speed and modularity. Various benchmarks have been included in the benchmarking/* directory and examples can be viewed in the examples/* directory.

Getting Started

Building this project requires cmake 3, boost-system 1.54, google test and a C++11 compiler.

Linux - debian based

Dependencies can be installed using the following commands on a debian based Linux system:

sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test;
sudo apt-get update;
sudo apt-get install make g++-4.9 libcppunit-dev libboost-system-dev cmake libgtest-dev;

OSX

On OSX dependencies can be installed using homebrew:

brew install cmake
brew install boost

GoogleTest (gtest) is also required. This can be installed using the following commands:

git clone https://github.com/google/googletest.git
cd googletest; cmake .
make
make install

Building OSX/Ubuntu

CMake is used as the build system for this project, follow the steps below to compile:

git clone --recursive https://github.com/lewisgcm/mvcpp
cd mvcpp; cmake .
make UnitTest  #Run unit tests
make RequestBenchmark #Compile request benchmark
make RouterExample #Compile router example

Usage

Basic usage:

Routing::Router router({
    Routing::Route( Http::GET,  "/", []( Http::Request& request, Http::Response& response ) -> void {
        response << "Hello World";
    }),
    Routing::Route( Http::GET,  "/api/test", []( Http::Request& request, Http::Response& response ) -> void {
        response << "Testing!!!";
    }),
    Routing::Route( Http::GET,  "/api/test/{id}", []( Http::Request& request, Http::Response& response ) -> void {
        response << "Testing!!!" << request.getQuery().getParam<int>( "id" );
    })
});

Http::Server server( 8080, "0.0.0.0" );
server.run(
    router,
    []( Http::Response response, std::exception_ptr exception ) -> void {
        response.setStatusCode( Http::INTERNAL_SERVER_ERROR );
        response << "error"; 
    }
);

Alternativley a Simple file server could be implemented as follows:

Http::Server server( 8080, "0.0.0.0" );
server.run(
    []( Http::Request request, Http::Response response ) -> void {
        response << "<h1>Hello World</h1>";
    },
    []( Http::Response response, std::exception_ptr exception ) -> void {
        response.setStatusCode( Http::INTERNAL_SERVER_ERROR );
        response << "A bad error!";
    }
);

Advanced features have been added for JSON based APIs where request bodies can be auto-maigcally casted into objects from json.

Http::Server server( 8080, "0.0.0.0" );
server.run(
    []( Http::Request request, Http::Response response ) -> void {
        User user = request.getBody<User>();
        user.setName( "MyNewName" );
        response << static_cast<json>(user) ;
    },
    []( Http::Response response, std::exception_ptr exception ) -> void {
        response.setStatusCode( Http::INTERNAL_SERVER_ERROR );
        response << "A bad error!";
    }
);

About

Fast C++ HTTP MVC Networking Library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages