Cover Page
Backend Page
Rust with axum
handlers.rs
Change to your chatterd folder and edit the file src/handlers.rs:
server$ cd ~/reactive/chatterd
server$ vi src/handlers.rs
Add one new property to the Chatt struct:
pub struct Chatt {
// . . .
audio: Option<String>,
}
To the back-end database, audio is just a string.
To handle audio data uploads, make a copy of your postchatt() function inside your handlers.rs
file and name the copy postaudio(). Replace the parameters in the call to chatterDB.execute() with:
"INSERT INTO chatts (username, message, id, audio) VALUES ($1, $2, gen_random_uuid(), $3)",
&[&chatt.username, &chatt.message, &chatt.audio],
which extracts the audio entry from the JSON object and insert it into the chatts table,
along with the rest of its associated chatt.
Next, make a copy of your getchatts() function inside your handlers.rs file and name the copy
getaudio(). Replace the SELECT statement with: "SELECT username, message, id, time, audio FROM chatts".
This will retrieve all data, including our new audio string from the PostgreSQL database.
Still in getaudio(), replace the chattArr.push(/*...*/); line with:
chattArr.push(vec![
row.get(0),
row.get(1),
Some(row.get::<usize, Uuid>(2).to_string()),
Some(row.get::<usize, DateTime<Local>>(3).to_string()),
row.get(4),
]);
In addition to the original columns, we added reading the audio column and included it
in the chatt data returned to the front end.
We’re done with handlers.rs. Save and exit the file.
main.rs
Edit the file src/main.rs:
server$ vi src/main.rs
Find the router = Router::new() instantiation statement and add the following new routes for
the new APIs /getaudio and /postaudio, before the .layer(/*...*/) line:
.route("/getaudio", get(handlers::getaudio))
.route("/postaudio", post(handlers::postaudio))
We’re done with main.rs. Save and exit the file.
Build and test run
To build your server:
server$ cargo build --release
![]()
Rust is a compiled language, like C/C++ and unlike Python, which is an
interpreted language. This means you must run cargo build each and every time
you made changes to your code, for the changes to show up in your executable.
To run your server:
server$ sudo ./chatterd
# Hit ^C to end the test
The cover backend spec provides instructions for Testing audio upload.
| Prepared by Sugih Jamin | Last updated August 11th, 2025 |