2 releases

0.0.2-alpha May 18, 2023
0.0.1-alpha May 7, 2023

#33 in #rocket-framework

43 downloads per month

MIT license

315KB
8K SLoC

hashira

CI-badge Latest Version Docs

A server side rendering framework built on top of yew.

Getting started

To create a project with hashira you need to install the CLI.

cargo install --force hashira-cli

Creating a new project

Using hashira new you can create a new project, the CLI will prompt you with the template to use, and the project name.

You can also use a shortcut to create a new project:

hashira new --{{template}} --name {{name}}

There are the templates available at the moment:

  • actix-web
  • axum
  • rocket
  • deno

More will be added in the future. Or if you want to create an adapter for your own, look at the code, most of the templates just use an adapter which starts the server, you can check the adapters in /packages/adapters.

This project still on alpha

hashira still on alpha that means:

  • Some features are incomplete
  • Some features may be removed

Features

SSR (Server Side Rendering)

Allow to render your yew components on the server passing down the properties from server side.

async fn render_page(ctx: RenderContext) -> Result<Response> {
    let products = get_products().await?;
    let res = ctx.render_with_props(ProductsPageProps { products }).await;
    Ok(res)
}

#[page_component("/products", render = "render_page")]
fn ProductsPage(props: &ProductsPageProps) -> yew::Html {
    yew::html! {
        // ...
    }
}

Server Actions

Execute code in your server from your components.

struct CreateProduct {
    name: String,
    price: i64,
    description: Option<String>
}

#[action("/api/products/create")]
async fn CreateProductAction(form: Form<CreateProduct>) -> Json<Product> {
    // server side logic
}

#[page_component("/products/add", render = "...")]
fn CreateProductPage() -> yew::Html {
    let action = use_action();

    if action.is_loading() {
        return yew::html! {
            "Creating product..."
        };
    }

    yew::html! {
        <Form<CreateProductAction> action={action.clone()}>
            <input name="name" required={true} />
            <input name="price" required={true} />
            <textarea name="description" rows={4}></textarea>
        </Form<CreateProductAction>>
    }
}

Extractors

Render functions and Server actions allow to inject any parameter that implements FromRequest.

#[action("/api/products/create")]
async fn CreateProductAction(
    form: Form<CreateProduct>,
    headers: HeadersMap,
    method: Method,
    Inject(pool): Inject<MySqlPool>) -> Json<Product> {
    // server side logic
}

Dependencies

~25–58MB
~1M SLoC