4 releases

0.1.3 Mar 18, 2024
0.1.2 Mar 18, 2024
0.1.1 Feb 29, 2024
0.1.0 Feb 29, 2024

#6 in #ryde

Download history 264/week @ 2024-02-25 29/week @ 2024-03-03 5/week @ 2024-03-10 231/week @ 2024-03-17 4/week @ 2024-03-24 14/week @ 2024-03-31

257 downloads per month
Used in 2 crates (via ryde_db)

MIT license

26KB
610 lines

ryde

ryde is a single person, single file web development library for rust

Install

cargo new your-project
cd your-project
cargo add ryde
mkdir static

Quickstart

Open up your-project/src/main.rs in your favorite editor

// src/main.rs
use ryde::*;

route!(
    (get, "/", index),
    (get, "/*files", files_handler) // serves the static files from the root ::1:3000/test.css, ::1:3000/app.js
);

fn main() {
    serve!("::1:3000");
}

async fn index() -> Response {
    document()
        .head((title("ryde with rust"), render_static_files!()))
        .body(
            h1("ryde with rust")
                .css(css!(
                    "font-size: 1.5rem",
                    "line-height: 2rem"
                ))
        )
        .render()
}

// embeds all files in the static/ folder
// and serves them
serve_static_files!("static", files_handler);

A longer example

// src/main.rs
use ryde::*;

db!(
    "create table if not exists todos (
        id integer primary key,
        content text not null,
        created_at integer not null default(unixepoch())
    )",
    (insert_todo, "insert into todos (content) values (?)"),
    (todos, "select * from todos order by created_at desc limit 30")
);

route!(
    (get, "/", todos_index),
    (post, "/todos", todos_create),
    (get, "/*files", files_handler)
);

serve_static_files!("static", files_handler);

fn main() {
    serve!("::1:3000")
}

async fn todos_index() -> Result<Response> {
    let todos = todos().await?;
    Ok(render_todos_index(todos))
}

async fn todos_create(Form(todo): Form<InsertTodo>) -> Result<Response> {
    let _todo = insert_todo(todo.content).await?;
    Ok(redirect_to(Route::TodosIndex))
}

fn render_todos_index(todos: Vec<Todos>) -> Response {
    render(div((
        h1("todos").css(css!(
            "font-size: 1.5rem",
            "line-height: 2rem",
        )),
        ul(todos.iter().map(|todo| li(todo.content.clone())).collect::<Vec<_>>()),
        todo_form()
    )).css(css!(
        "color: rgb(3 7 18)",
        "dark:color: rgb(245 158 11)")))
}

fn todo_form() -> Element {
    form((
        input().type_("text").name("content"),
        input().type_("submit").name("save")
    ))
    .method("POST")
    .action(Route::TodosCreate)
}

fn render(element: Element) -> Response {
    document()
        .head(render_static_files!())
        .body(
            div(element)
                .css(css!(
                    "background: rgb(243 244 246)",
                    "dark:background: rgb(3 7 18)"
                ))
        )
        .render()
}

More examples

Clone the repo and check out the rest of examples!

Dependencies

~24MB
~465K SLoC