{"id":13424616,"url":"https://github.com/ozanarmagan/RESTCpp","last_synced_at":"2025-03-15T18:35:31.409Z","repository":{"id":222912480,"uuid":"452885400","full_name":"ozanarmagan/RESTCpp","owner":"ozanarmagan","description":"Cross Platform Multi threaded REST API / HTTP Server framework using thread-pooling implementation with modern C++","archived":false,"fork":false,"pushed_at":"2023-08-08T15:07:38.000Z","size":783,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-26T23:55:31.024Z","etag":null,"topics":["api","api-rest","backend","cpp","cpp11","cpp14","cpp17","rest","rest-api"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ozanarmagan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-01-28T00:01:16.000Z","updated_at":"2024-02-16T22:06:32.000Z","dependencies_parsed_at":"2024-02-17T00:23:22.523Z","dependency_job_id":"af9828d5-5865-4f10-a1ee-47db660550f9","html_url":"https://github.com/ozanarmagan/RESTCpp","commit_stats":null,"previous_names":["ozanarmagan/restcpp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanarmagan%2FRESTCpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanarmagan%2FRESTCpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanarmagan%2FRESTCpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ozanarmagan%2FRESTCpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ozanarmagan","download_url":"https://codeload.github.com/ozanarmagan/RESTCpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243775899,"owners_count":20346282,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-rest","backend","cpp","cpp11","cpp14","cpp17","rest","rest-api"],"created_at":"2024-07-31T00:00:57.048Z","updated_at":"2025-03-15T18:35:26.400Z","avatar_url":"https://github.com/ozanarmagan.png","language":"C++","funding_links":[],"categories":["Rest protocol"],"sub_categories":[],"readme":"\n# RESTC++\nRestC++ is a micro web framework developed with modern C++. Currently supports Windows and Linux systems.\n## Contents\n- [How to build](#how-to-build)\n- [Components of RESTC++](#components-of-restc)\n  - [Server](#server)\n\t  - [Multithreading](#multithreading)\n  - [Router](#router)\n\t  - [Static File Serving](#static-file-serving)\n\t  - [Dynamic Route](#dynamic-route)\n\t\t  - [Parsing URL Parameters](#parsing-url-parameters)\n  - [HTTPRequest](#httprequest)\n  - [HTTPResponse](#httpresponse)\n  - [FormData](#formdata)\n  - [Proxy](#proxy)\n  - [Cookie](#cookie)\n  - [Session](#session)\n- [Notes](#notes)\n## How to build\nYou can easily build by CMake or Makefile included to repo for both Windows and Linux\n## Components of RESTC++\n### Server\nThis is wrapper class for whole framework, to be able run the server.\nJust create an instance of `restcpp::Server` class and call `run` method.\n```\nrestcpp::Server server(80);\nserver.run();\n```\n#### Multithreading\nServer uses a custom implementation of thread pool design to create a multithreaded server to serve multiple client connections at the same time.Number of threads that are in thread pool are controlled by `THREAD_COUNT` constant which is in `Common.h` file (default is 3).\n### Router\nTo have a proper web server and/or REST API, you have to define `route`s.\nIdea behind routes is parsing URL path and calling a defined callback function to create a response to that request depends on URL path and HTTP method type.\nFor RESTC++, you can call appropriate function of `Server` class to create a dynamic route\n\n```\nServer\u0026 get(const std::string\u0026 URLPath, const std::function\u003cvoid(const HTTPRequest\u0026,HTTPResponse\u0026)\u003e\u0026 callBack);\nServer\u0026 post(const std::string\u0026 URLPath, const std::function\u003cvoid(const HTTPRequest\u0026,HTTPResponse\u0026)\u003e\u0026 callBack);\nServer\u0026 put(const std::string\u0026 URLPath, const std::function\u003cvoid(const HTTPRequest\u0026,HTTPResponse\u0026)\u003e\u0026 callBack);\nServer\u0026 patch(const std::string\u0026 URLPath, const std::function\u003cvoid(const HTTPRequest\u0026,HTTPResponse\u0026)\u003e\u0026 callBack);\nServer\u0026 del(const std::string\u0026 URLPath, const std::function\u003cvoid(const HTTPRequest\u0026,HTTPResponse\u0026)\u003e\u0026 callBack);\n```\n#### Static File Serving\nRESTC++ allows you to  directly serve files from a folder (and its subfolders automatically) to a URL path with file name  at the end of the URL.\nTo add dynamic route we will call `serveStatic` function class `Server`class.\n```\nrestcpp::Server server(6005);\nserver.serveStatic(\"/files/\",\"./WWW_ROOT/\");\n```\nWith this example we can directly reach files a under `WWW_ROOT` folder (and its subfolders) by `localhost:6005/files/` path.\n#### Dynamic route\nA dynamic route is a more complex routing type than static file serving.In dynamic routing, we generally don't directly send files over path.Instead, we parse fields from `HTTPRequest` object (like form data or file sent with request) or we parse.\nTo parse request and mutate `HTTPResponse` object, we need user defined callback functions.\nTo add dynamic route we need to use  route methods which attached to instances of `Server` class.\nThere two ways to accomplish this, first one is define function seperately and pass it as a parameter.\n```\nvoid  test(const restcpp::HTTPRequest\u0026  req, restcpp::HTTPResponse\u0026  res)\n{\n\tres.setBodyText(\"Greetings!\");\n}\nserver.get(\"/greeting/\", test);\n```\nSecond way, you can declare it as lambda function with `std::function`\n```\nserver.get(\"/greeting2/\",restcpp::METHOD::GET, \n\t[](const restcpp::HTTPRequest\u0026  req, restcpp::HTTPResponse\u0026  res) \n\t\t{ res.setBodyText(\"Greetings!\"); });\n```\nAnother example with `POST` method\n```\nserver.post(\"/\",[](const restcpp::HTTPRequest\u0026  req, restcpp::HTTPResponse\u0026  res) {\n\tres.setBodyText(\"You've sent a POST request!\");\n})\n```\n##### Parsing URL Parameters\nTo parse URL parameters and store them as variables in `HTTPRequest` object, we need to specify it with surrounding with curly brackets in URL Path parameter of routing function\nHere is an example.\n```\nserver.get(\"/greeting/{name}/{surname}\",  testParams);\n```\nNow you can access `name` and `surname` as variable in `HTTPRequest` parameter of callback function via `getParam` member function of `HTTPRequest` class.\n```\nvoid  testParams(const restcpp::HTTPRequest\u0026  req, restcpp::HTTPResponse\u0026  res)\n{\n\tres.setBodyText(\"Greetings, Mr./Mrs. \"  +  req.getParam(\"name\") +  \"  \"  +  req.getParam(\"surname\"));\n}\n```\n### HTTPRequest\nThis is part of framework which handles data that stored in HTTP requests, such as different headers and body types.\nSuch as:\n```\nQuery\nPath parameters\nGeneral headers (HTTP version,User-Agent,host,URL path,time etc.)\n```\nAlso it parses form body of request if exists, and parses into objects of `FormData` class.\n\nConstructor of `HTTPRequest` class takes raw string read from socket and parses it.Also it has setters and getters so you can modify it,but since callback functions of dynamic routes gets `const HTTPRequest\u0026` you can not do that in call them.\n### HTTPResponse \nClass of framework to used create and store responses for requests of clients.Most important of this class is it has different member functions for storing data in body:\n```\nsetBodyText //Set content type of response to text/plain\nsetBodyJSON //Set content type of response to application/json\nsetBodyHTML //Set content type of response to text/html\nsetBodyFile //Send file in response\n```\nWe can serialize `HTTPResponse` objects into proper `std::string` via `serializeResponse` function.\n### FormData\nThis class used to store every member of forms sent with request.\nIt can store either text or binary data.\n### Proxy\nWe can use RESTC++ as a HTTP proxy as well.All we need to use dynamic routes to proxy a specific path into an address.\nHere is an example.\n```\nvoid  proxyTest(const restcpp::HTTPRequest\u0026  req, restcpp::HTTPResponse\u0026  res)\n{\n\tres  = restcpp::Proxy(\"google.com\").getResponse();\n}\n```\n### Cookie\nRESTC++ supports HTTP cookies.We can use `Cookie` class to create HTTP cookies and add them to `HTTPResponse` objects by `addCookie` function.\n```\nvoid testSetCookie(const restcpp::HTTPRequest\u0026 req, restcpp::HTTPResponse\u0026 res)\n{\n\trestcpp::Cookie cookie('test','test');\n\tres.addCookie(cookie);\n}\n```\n### Session\nIn RESTC++ HTTP sessions wraps `Cookie`s with `Session` class and stores key-value pairs with `std::string` as a key and `std::any` as a value (which makes you can store object of any class or primitive type as a value).\n\nYou need to call `setData` function to store key-value pairs in instance of `Session` class.\n\n```\nrestcpp::Session session(restcpp::generateUUID());\nsession.setData(\"test\",\"test\");\n```\n\n\nTo store sessions on the server, you have instantiate a `SessionManager` object and add sessions to them. `addSession` function of `SessionManager` class returns `Session*` to the session if you successfully add the session to the manager or create session by providing a session ID (you can use `restcpp::generateUUID` function to crate unique ids for sessions).\n```\nrestcpp::sessionManager.addSession(session); // Add an existing session to the manager\nrestcpp::Session* newSession = sessionManager(restcpp::generateUUID()); // Creates a new session\n```\n\nTo set a new cookie over response call `toCookie` function of `Session` instance to get `Session` as a `Cookie` to set.\n```\nrestcpp::session session(\"test\");\nres.addCookie(session.toCookie());\n```\n\n### Notes\nNo note currently.Feel free to contribute this project or start any discussion about the project.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fozanarmagan%2FRESTCpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fozanarmagan%2FRESTCpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fozanarmagan%2FRESTCpp/lists"}