A thread-safe minimal API written in rust that serves JSON for HTTP requests.
Minimal API currently supports:
- Thread-safe workers to listen for requests, and serve out responses
GET,POST,PUT, andDELETEHTTP requests- Query string keys, body content, and dynamic path segments as function parameters
- A series of more complex routes to represent more realistic, complex scenarios
- Download the repo
- Navigate to the repo root
- Run the server either by:
docker compose upto launch the API in Dockercargo runto launch the API locally
- Use your browser to hit the API and get a JSON response from an endpoint
- Check the server config file of the environment you chose for the address
- By default, there are index ("/"), "/name", and "/version" endpoints
The following example sets up a GET endpoint for the index route (/) that returns "Hello!":
use http_attributes::http_get;
#[http_get("/")]
pub fn say_hello() -> String {
format!("Hello!")
}The following example sets up a GET endpoint for the /who path that returns a message with the value of the name query parameter:
use http_attributes::http_get;
#[http_get("/who")]
pub fn say_hello(name: String) -> String {
format!("Hello, {name}!")
}The following example sets up a GET endpoint for the /user path that returns a message with the value of the id path segment:
use http_attributes::http_get;
#[http_get("/user/{id}")]
pub fn get_user(id: String) -> String {
format!("Found user by id '{id}'!")
}The following example sets up a POST endpoint for the /submit path that returns the POST data:
use http_attributes::http_post;
#[http_post("/submit")]
pub fn get_post_data(content: String) -> String {
format!("Received '{content}' from POST")
}The following example sets up a PUT endpoint for the /update path that returns the PUT data:
use http_attributes::http_put;
#[http_put("/update")]
pub fn get_put_data(content: String) -> String {
format!("Received '{content}' from PUT")
}The following example sets up a DELETE endpoint for the /remove path that returns a query parameter value sent to the DELETE route:
use http_attributes::http_delete;
#[http_delete("/remove")]
pub fn get_delete_id(id: String) -> String {
format!("Received '{id}' from DELETE")
}- Create a file in the routes definition folder
- Write a function that returns a
String - Add any HTTP macro attribute to the function
- Optionally, add any parameters to represent query strings or body data
- Add the function to the vector returned by
get_endpoints()in the routes definition file, without providing any parameters if they were added - The server listen() function will automatically pick up the new endpoint
- Modify the
./cargo/config.tomlfile to change the ip address or port for local development. - Modify the
./cargo/config.docker.tomland./compose.ymlfiles to change the ip address or port for docker development.
- Guard query string parameter (and body content) input casting with some sort of validation in the attributes library