C++ REST API Framework
Created and developed at Blue SeaBird by: @janhaslik
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.
- 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.
To use JetPlusPlus in your C++ project, follow these steps:
- Clone the JetPlusPlus repository.
git clone https://github.com/Blue-SeaBird/JetPlusPlus.git
- Build the project with cmake
mkdir build
cd build
cmake ..
make
- 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
- 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)
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;
}
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
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);
});
Simple PUT route:
router.put("/players", [](jetpp::Request &req, jetpp::Response &res)
{
res.send(200);
});
Simple PATCH route:
router.patch("/players", [](jetpp::Request &req, jetpp::Response &res)
{
res.send(200);
});
Simple DELETE route:
router.Delete("/players", [](jetpp::Request &req, jetpp::Response &res)
{
res.send(200);
});
Simple OPTIONS route:
router.post("/players", [](jetpp::Request &req, jetpp::Response &res)
{
res.send(200);
});
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>();
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.
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.
#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;
}