Fifthtry

Routing, Request And Response

Say we want a /date/ page, which shows the current date calculated by server.

forward.rs

First step is to add the routing, which we do in forward.rs.

This is how forward.rs looked like hello world example in last section:

use crate::prelude::*;
use http::method::Method;

pub fn magic(in_: &In) -> Result<realm::Response> {
    let mut input = realm::request_config::RequestConfig::new(&in_.ctx.request)?;
    match (in_.ctx.request.uri().path(), in_.ctx.request.method()) {
        t if realm::is_realm_url(t) => realm::handle(in_, t, &mut input),
        ("/", &Method::GET) => crate::routes::index::get(in_),
    }
}

Lets add handler for /date/:

use crate::prelude::*;
use http::method::Method;

pub fn magic(in_: &In) -> Result<realm::Response> {
    let mut input = realm::request_config::RequestConfig::new(&in_.ctx.request)?;
    match (in_.ctx.request.uri().path(), in_.ctx.request.method()) {
        t if realm::is_realm_url(t) => realm::handle(in_, t, &mut input),
        ("/", &Method::GET) => crate::routes::index::get(in_),
        // adding date here
        ("/date/", &Method::GET) => crate::routes::date::get(in_),
    }
}

We have decided our date request would be handled by a function named get() in routes::date module. Also we have decided we are only going to handle GET requests.

routes::date module

The first this we have to do is to decide what is the dynamic data, the data that should be supplied by backend to frontend.

In realm we do that by defining a struct, usually called Page:

// routes/date.rs
use crate::prelude::*;

#[realm_page(id = "Pages.Date")]
struct Page {
    date: String,
}

Note we are deciding here that the frontend Elm code for this page will live in an Elm module named Pages.Date.

Further we are declaring that we will be sending date from frontend to backend, and its type will be String.

Now we need our get() method.

use crate::prelude::*;

#[realm_page(id = "Pages.Date")]
struct Page {
    date: String,
}

pub fn get(in_: &realm::In<()>) -> Result<realm::Response> {
    Page {
        date: std::time::SystemTime::now().to_string()
    }
    .with_title("Date")
}

We are getting the date using std::time::SystemTime, and then creating our Page struct.

Further we have decide the “title” of our page is going to be “Date”.

Realm will take the page struct, and convert it to json, and pass the data to frontend.

Also ensure that date is added to your routes/mod.rs:

pub mod index;
pub mod date;

Go to next section to see the frontend code.

Table Of Content

What is Realm?

A Bit On Motivation

Routing is Hard

What does Realm do?

Backend Data And Type Safety

Tutorial

Quick Start Realm Tutorial

In Depth Tutorial (not ready)

Nix
Shell
Doit
Hello Rust
Hello Elm
Hello Static Files
Hello Server Side Rendering
Pre-Commit Hooks

Routing, Request And Response

Frontend, Data, Navigation, And APIs

How To Guides

File Upload

Backend: S3 File Upload
Authenticated File Serving
Frontend: Uploading Files From Elm

How to use storybook?

How to implement “loading..”?

Docs

Realm.In

Realm.Storybook.Story

realm::In

realm::Context

realm::Result

realm.magicSlice

realm::RequestConfig

Environment Variables

Internals - Only for Realm Developers, not Users

“Realm DATA”
iFrame Controller
Shutdown Routine
Testing Internals

Change Log

Get Realm Starter Working

Transparent Offline Feature

How to make http requests in Realm?

Development

Replay Testing

Tutorial: ToDo App

Realm Testing

Enhance Realm Starter

Double Load Issue

Deploy To Heroku Button

End failure

Realm-Starter Github Template

Proposal: Tracker And Visit

Proposal: Activity Store

Proposal: Bundling

Proposal: Retry On Network Error

Storybook: Editable JSON

Storybook: Notes

Storybook: Reference

Backlog

Readings

Change Log

How to Publish

Testing

Code Snippets

Skip rustfmt For Some Section

Close Modal Dialog When Clicked Outside

Ignoring Lints In Python

Ignoring Lints (clippy and rustc warnings) In Rust

Handle DateTime in Rust & Elm

Handle CiText value read in Rust

Transport Enum Type to and fro Rust/Elm through JSON