Skip to content

evangipson/minimal-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minimal API

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, and DELETE HTTP 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

Getting Started

  1. Download the repo
  2. Navigate to the repo root
  3. Run the server either by:
    • docker compose up to launch the API in Docker
    • cargo run to launch the API locally
  4. 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

Examples

Basic GET

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!")
}

GET with query parameters

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}!")
}

GET with dynamic path segments

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}'!")
}

Basic POST

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")
}

Basic PUT

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")
}

Basic DELETE

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")
}

Creating Endpoints

  1. Create a file in the routes definition folder
  2. Write a function that returns a String
  3. Add any HTTP macro attribute to the function
  4. Optionally, add any parameters to represent query strings or body data
  5. Add the function to the vector returned by get_endpoints() in the routes definition file, without providing any parameters if they were added
  6. The server listen() function will automatically pick up the new endpoint

Environment Configuration

TODO:

  • Guard query string parameter (and body content) input casting with some sort of validation in the attributes library

About

A thread-safe minimal API that serves JSON written in rust

Topics

Resources

Stars

Watchers

Forks