-
Notifications
You must be signed in to change notification settings - Fork 0
Cleanup, Tests, and Documentation #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2c2c36d
Refactor library, add tests and documentation.
google-labs-jules[bot] d0640b7
Refactor library, add tests/docs, fix toolchain.
google-labs-jules[bot] 2830916
Refactor library, add tests/docs, fix toolchain.
google-labs-jules[bot] dc70361
Refactor library, add tests/docs, fix toolchain.
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,95 @@ | ||
| # rest-api-helper | ||
| A simple helper aimed at making creation of REST APIs a bit nicer | ||
|
|
||
| A simple helper library for C++ aimed at making the creation of REST APIs easier and cleaner. It is built on top of [cpp-httplib](https://github.com/yhirose/cpp-httplib) and [nlohmann/json](https://github.com/nlohmann/json). | ||
|
|
||
| ## Features | ||
|
|
||
| - **Route Management**: Define routes and endpoints with a fluent API. | ||
| - **Auto-Documentation**: Automatically generates a JSON documentation endpoint for your API. | ||
| - **Parameter Descriptions**: Add descriptions for path parameters and request/response details. | ||
| - **Bazel Support**: Ready to be integrated into Bazel projects. | ||
|
|
||
| ## Installation | ||
|
|
||
| This library is designed to be used with Bazel with Bzlmod enabled. | ||
|
|
||
| ### 1. Add dependency to `MODULE.bazel` | ||
|
|
||
| Add the following to your `MODULE.bazel`: | ||
|
|
||
| ```python | ||
| # Assuming you have this in a registry or local override | ||
| bazel_dep(name = "rest_api_helper", version = "0.0.1") | ||
| ``` | ||
|
|
||
| ### 2. Add dependency to your `BUILD` file | ||
|
|
||
| In your `BUILD` or `BUILD.bazel` file, add the library to your target's `deps`: | ||
|
|
||
| ```python | ||
| cc_binary( | ||
| name = "my_app", | ||
| srcs = ["main.cpp"], | ||
| deps = [ | ||
| "@rest_api_helper//src:rest_api_helper", | ||
| ], | ||
| ) | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Here is a simple example of how to use the library: | ||
|
|
||
| ```cpp | ||
| #include <httplib.h> | ||
| #include "src/rest_api.hpp" | ||
|
|
||
| int main() | ||
| { | ||
| httplib::Server server; | ||
| // Initialize the API helper with a base route "/api" | ||
| yuki::web::RestAPI api(server, "/api"); | ||
|
|
||
| // Enable the documentation endpoint at "/api/docs" | ||
| api.add_docs_endpoint("docs"); | ||
|
|
||
| // Add a route "/api/hello" | ||
| auto& hello_route = api.add_route("hello", "A simple hello route"); | ||
|
|
||
| // Add a GET endpoint to the route | ||
| hello_route.add_endpoint( | ||
| yuki::web::HTTPMethod::HTTP_GET, | ||
| [](const httplib::Request&, httplib::Response& res) { | ||
| res.set_content("Hello, World!", "text/plain"); | ||
| }, | ||
| "Returns a hello message" | ||
| ); | ||
|
|
||
| // Start the server | ||
| server.listen("0.0.0.0", 8080); | ||
|
|
||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| See `examples/basic_usage.cpp` for a more complete example. | ||
|
|
||
| ## Building and Testing | ||
|
|
||
| To build the library: | ||
|
|
||
| ```bash | ||
| bazel build //src:rest_api_helper | ||
| ``` | ||
|
|
||
| To run the tests: | ||
|
|
||
| ```bash | ||
| bazel test //tests:unit_tests | ||
| ``` | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - [cpp-httplib](https://github.com/yhirose/cpp-httplib) | ||
| - [nlohmann/json](https://github.com/nlohmann/json) | ||
| - [googletest](https://github.com/google/googletest) (for testing) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| load("//build_utils:defs.bzl", "strict_cc_binary") | ||
|
|
||
| strict_cc_binary( | ||
| name = "basic_usage", | ||
| srcs = ["basic_usage.cpp"], | ||
| deps = ["//src:rest_api_helper"], | ||
| visibility = ["//visibility:private"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| #include <httplib.h> | ||
|
|
||
| #include "src/hello.hpp" | ||
| #include "src/rest_api.hpp" | ||
|
|
||
| int main() | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,13 @@ | ||
| load("//build_utils:defs.bzl", "strict_cc_binary", "strict_cc_library") | ||
| load("//build_utils:defs.bzl", "strict_cc_library") | ||
|
|
||
| strict_cc_library( | ||
| name = "rest_api_helper", | ||
| srcs = ["hello.cpp"], | ||
| hdrs = ["hello.hpp"], | ||
| srcs = ["rest_api.cpp"], | ||
| hdrs = ["rest_api.hpp"], | ||
| visibility = ["//visibility:public"], | ||
|
|
||
| deps = [ | ||
| "@cpp-httplib", | ||
| "@nlohmann_json//:json", | ||
| ], | ||
| ) | ||
|
|
||
| strict_cc_binary( | ||
| name = "example_app", | ||
| srcs = ["main.cpp"], | ||
| deps = [":rest_api_helper"], | ||
| visibility = ["//visibility:private"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.