GithubHelp home page GithubHelp logo

guoyu07 / mvcpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lewisgcm/mvcpp

0.0 0.0 0.0 167 KB

Fast C++ HTTP MVC Networking Library

License: GNU Lesser General Public License v3.0

CMake 4.18% C++ 95.82%

mvcpp's Introduction

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!";
    }
);

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.