Skip to content

QuackHack-McBlindy/tinyapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinyapi

Sponsors Buy Me a Coffee

tinyapi is a crate designed for bare metal and no_std Rust async projects.
Its sole purpose is to make it stupid easy to create an embedded API.

tinyapi depends on embassy-net for network, embassy-executor for async task handling.
It's also assumed you already have network/WiFi configured properly and that an alloc is present.

User defines:

  • endpoint
  • function to execute or file to serve
  • response

The embassy-executor web_server_task can then be started in your main loop by passing your network stack.

Installation

Add tinyapi as a dependency in Cargo.toml.

[dependencies]
tinyapi = "0.1.12"

Example usage

//! Example for ESP32‑S3 with `esp‑alloc` and `embassy‑net`.
#![no_std]
#![no_main]

extern crate alloc;
use alloc::format;
use defmt::info;
use embassy_executor::Spawner;
use esp_alloc as _; // global allocator
use tinyapi::{register_route, Response, web_server_task};

#[embassy_executor::main]
async fn main(spawner: Spawner) {
    // ... WiFi setup, get `stack: &'static Stack<'static>` ...

    // Register endpoints
    // Serve an embedded index.html (place file in crate root)
    register_route("/", |_req| {
        Response::html(include_str!("index.html"))
    })

    // Inline HTML response
    register_route("/hello_world", |_req| {
        Response::html("<h1>Hello from ESP32!</h1>")
    })

    // Path parameter example
    register_route("/led/{state}", |req| {
        let state = req.param("state").unwrap_or("?");
        info!("Setting LED to {}", state);
        Response::text(&format!("LED is now {}", state))
    })

    // Start server
    spawner.spawn(web_server_task(stack)).unwrap();

    loop { /* other tasks */ }
}



Sponsors Buy Me a Coffee

🦆🧑‍🦯 says ⮞ Hi! I'm QuackHack-McBlindy!
Like my work?
Buy me a coffee, or become a sponsor.
Thanks for supporting open source/hungry developers ♥️🦆!

♥️Wallet: pungkula.x
Buy Me A Coffee


License

This project is licensed under the terms of the MIT license.
See the LICENSE file in the repository for full details.

About

tinyapi is designed to make embedded APIs simple.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors