Skip to content

Blue-SeaBird/JetPlusPlus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JetPlusPlus

Awesome

C++ REST API Framework

Created and developed at Blue SeaBird by: @Peeentaa

Table of Contents

  1. Introduction
  2. Features
  3. Installation
  4. Getting Started
  5. Routes and Endpoints
  6. Containers

Introduction

JetPlusPlus is a C++ REST API framework designed to simplify the development of robust and scalable web services. This documentation provides an overview of the features and usage of JetPlusPlus, along with examples to guide you through the process.

Features

  • Modular Routing: Easily define routes with callback functions, making it simple to handle various endpoints in your application.
  • Container Support: JetPlusPlus supports proxy containers to restrict access to specified hosts, enhancing security for your API.
  • JSON Response Handling: The framework provides built-in support for handling JSON responses, making it convenient to work with JSON data in your API.

Installation

To use JetPlusPlus in your C++ project, follow these steps:

  1. Clone the JetPlusPlus repository.
git clone https://github.com/Blue-SeaBird/JetPlusPlus.git
  1. Build the project with cmake
mkdir build
cd build
cmake ..
make
  1. Include the necessary headers in your project.
# CMakeLists.txt

# Add the path to JetPlusPlus headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/jetplusplus)

# Add any other necessary configuration for your project
  1. Build and link your project with JetPlusPlus.
# CMakeLists.txt

# Link your project with JetPlusPlus library
target_link_libraries(your_project_name PRIVATE lib/libJetPlusPlusLib.dylib)

Getting Started

C++ CMake

Create a main function in your C++ application and define your API endpoints using the JetPlusPlus router.

#include "jetplusplus/router/router.hpp"
#include "jetplusplus/server/server.hpp"

int main() {
    jetpp::Router router;
    router.get("/your-endpoint",[](jetpp::Request &req, jetpp::Response &res)
     { 
        //your logic here
     });
    
    jetpp::Server server(router);
    server.start(8080);
    return 0;
}

Routes and Endpoints

GET

Simple GET route:

router.get("/users", [](jetpp::Request &req, jetpp::Response &res)
    {
        res.send("Hello World"); 
    });

With custom status:

router.get("/users", [](jetpp::Request &req, jetpp::Response &res)
    {
        res.status(200).send("Hello World"); 
    });

With json response:

router.get("/users", [](jetpp::Request &req, jetpp::Response &res)
    {
        jetpp::JsonValue users;
        jetpp::JsonValue user;
        user.setObject({{"name", jetpp::JsonValue("John Doe")}});

        std::vector<jetpp::JsonValue> usersVector;
        users.setArray(userVector)
        res.json(users);
    });

With dynamic route:

router.get("/users/:userid", [](jetpp::Request &req, jetpp::Response &res)
    {
        std::string userid=req.params.userid;
        res.status(200).send(userid);
    });

With query params:

router.get("/users", [](jetpp::Request &req, jetpp::Response &res)
    {
        std::string userid=req.query.userid;
        res.status(200).send(userid);
    });
### Expect: 123 (STATUSCODE 200)
GET http://localhost:8080/users?userid=123

POST

Simple POST route:

router.post("/players", [](jetpp::Request &req, jetpp::Response &res)
    {
        res.send(200);
    });

Retrieve the body:

router.post("/players", [](jetpp::Request &req, jetpp::Response &res)
    {
        jetpp::JsonValue body=req.body;
        res.send(200);
    });

PUT

Simple PUT route:

router.put("/players", [](jetpp::Request &req, jetpp::Response &res)
    {
        res.send(200);
    });

PATCH

Simple PATCH route:

router.patch("/players", [](jetpp::Request &req, jetpp::Response &res)
    {
        res.send(200);
    });

DELETE

Simple DELETE route:

router.Delete("/players", [](jetpp::Request &req, jetpp::Response &res)
    {
        res.send(200);
    });

OPTIONS

Simple OPTIONS route:

router.post("/players", [](jetpp::Request &req, jetpp::Response &res)
    {
        res.send(200);
    });

Containers

Creating a Container

To create a container, use the jetpp::Container class and make a shared pointer:

std::shared_ptr<jetpp::Container> myContainer = std::make_shared<jetpp::Container>();

Adding Access Hosts

You can specify hosts that are allowed to access your API. Use the addAccessHost method:

myContainer->addAccessHost("127.0.0.1");

This example allows access only from the host with the IP address "127.0.0.1". You can add multiple hosts as needed.

Associating Containers with Routes

Associate a container with a specific route to apply access restrictions. Pass the container as the last argument when defining a route:

router.get("/restricted-endpoint", [](jetpp::Request &req, jetpp::Response &res)
{
    // Your logic here
}, myContainer);

Now, the "/restricted-endpoint" route will only be accessible from hosts specified in the associated container.

Code example with container

#include "jetplusplus/router/router.hpp"
#include "jetplusplus/server/server.hpp"
#include "jetplusplus/container/container.hpp"

int main()
{
    jetpp::Router router;

    // ... Your existing route definitions ...

    // Define a container and add an access host
    std::shared_ptr<jetpp::Container> userProxyContainer = std::make_shared<jetpp::Container>();
    userProxyContainer->addAccessHost("127.0.0.1");

    // Associate the container with a route
    router.get("/users/:user", [](jetpp::Request &req, jetpp::Response &res)
               { 
                    std::string message="Success for users and :user= "+req.params["user"];
                    res.send(message); }, userProxyContainer);

    // ... Your existing route definitions ...

    jetpp::Server server(router);
    server.start(8000);

    return 0;
}