Rust tutorial
This commit is contained in:
parent
131f2b022e
commit
bee67445a5
17
application-server/rust/.cargo/config.toml
Normal file
17
application-server/rust/.cargo/config.toml
Normal file
@ -0,0 +1,17 @@
|
||||
[target.x86_64-pc-windows-msvc]
|
||||
rustflags = ["-C", "target-feature=+crt-static"]
|
||||
|
||||
[target.aarch64-pc-windows-msvc]
|
||||
rustflags = ["-C", "target-feature=+crt-static"]
|
||||
|
||||
[target.x86_64-apple-darwin]
|
||||
rustflags = ["-C", "link-args=-ObjC"]
|
||||
|
||||
[target.aarch64-apple-darwin]
|
||||
rustflags = ["-C", "link-args=-ObjC"]
|
||||
|
||||
[target.aarch64-apple-ios]
|
||||
rustflags = ["-C", "link-args=-ObjC"]
|
||||
|
||||
[target.aarch64-apple-ios-sim]
|
||||
rustflags = ["-C", "link-args=-ObjC"]
|
||||
3
application-server/rust/.env
Normal file
3
application-server/rust/.env
Normal file
@ -0,0 +1,3 @@
|
||||
SERVER_PORT=6080
|
||||
LIVEKIT_API_KEY=devkey
|
||||
LIVEKIT_API_SECRET=secret
|
||||
14
application-server/rust/.gitignore
vendored
Normal file
14
application-server/rust/.gitignore
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
16
application-server/rust/Cargo.toml
Normal file
16
application-server/rust/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.5"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tower-http = { version = "0.5.2", features = ["cors"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.117"
|
||||
livekit = "0.3.2"
|
||||
livekit-api = "0.3.2"
|
||||
dotenv = "0.15.0"
|
||||
env_logger = "0.11.3"
|
||||
log = "0.4.21"
|
||||
18
application-server/rust/README.md
Normal file
18
application-server/rust/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
#### Prerequisites
|
||||
|
||||
To run this application you will need **Rust**:
|
||||
|
||||
- [Rust](https://www.rust-lang.org/tools/install){:target="\_blank"}
|
||||
|
||||
#### Download repository
|
||||
|
||||
```bash
|
||||
git clone https://github.com/OpenVidu/openvidu-livekit-tutorials.git
|
||||
cd openvidu-livekit-tutorials/application-server/rust
|
||||
```
|
||||
|
||||
#### Run application
|
||||
|
||||
```bash
|
||||
cargo run
|
||||
```
|
||||
70
application-server/rust/src/main.rs
Normal file
70
application-server/rust/src/main.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use axum::{
|
||||
extract::Json, http::header::CONTENT_TYPE, http::Method, http::StatusCode, routing::post,
|
||||
Router,
|
||||
};
|
||||
use dotenv::dotenv;
|
||||
use env_logger::Env;
|
||||
use livekit_api::access_token;
|
||||
use log::info;
|
||||
use serde_json::Value;
|
||||
use std::env;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); // Init logger
|
||||
dotenv().ok(); // Load environment variables from .env
|
||||
|
||||
// Check that required environment variables are set
|
||||
let server_port = env::var("SERVER_PORT").unwrap_or("6080".to_string());
|
||||
env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
|
||||
env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
.allow_methods([Method::POST])
|
||||
.allow_origin(Any)
|
||||
.allow_headers([CONTENT_TYPE]);
|
||||
|
||||
let app = Router::new().route("/token", post(get_token)).layer(cors);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:".to_string() + &server_port)
|
||||
.await
|
||||
.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn get_token(payload: Option<Json<Value>>) -> (StatusCode, Json<String>) {
|
||||
if let Some(payload) = payload {
|
||||
let livekit_api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
|
||||
let livekit_api_secret =
|
||||
env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");
|
||||
|
||||
let room_name = payload.get("roomName").expect("roomName is required");
|
||||
let participant_name = payload
|
||||
.get("participantName")
|
||||
.expect("participantName is required");
|
||||
|
||||
let token = access_token::AccessToken::with_api_key(&livekit_api_key, &livekit_api_secret)
|
||||
.with_identity(&participant_name.to_string())
|
||||
.with_name(&participant_name.to_string())
|
||||
.with_grants(access_token::VideoGrants {
|
||||
room_join: true,
|
||||
room: room_name.to_string(),
|
||||
..Default::default()
|
||||
})
|
||||
.to_jwt()
|
||||
.unwrap();
|
||||
|
||||
info!(
|
||||
"Sending token for room {} to participant {}",
|
||||
room_name, participant_name
|
||||
);
|
||||
|
||||
return (StatusCode::CREATED, Json(token));
|
||||
} else {
|
||||
return (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json("roomName and participantName are required".to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user