Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 150 additions & 160 deletions unmnemonic_devices_vrs/Cargo.lock

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions unmnemonic_devices_vrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ path = "src/main.rs"
name = "unmnemonic_devices_vrs"

[dependencies]
axum = { version = "0.6", features = ["tracing"] }
axum-template = { version = "0.15", features = ["handlebars"] }
axum = { version = "0.7", features = ["tracing"] }
axum-template = { version = "2", features = ["handlebars"] }
bcrypt = "0.15"
chrono = { version = "0.4.22", default-features = false, features = ["clock"] }
handlebars = { version = "4", features = ["dir_source"] }
http = "0.2.9"
handlebars = { version = "5", features = ["dir_source"] }
http = "1"
mime = "0.3.16"
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1.0", features = ["full"] }
toml = "0.7.3"
tower-http = { version = "0.4", features = ["fs", "trace"] }
tower-http = { version = "0.5", features = ["fs", "trace"] }
tracing = "0.1.35"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
select = "0.6"
Expand All @@ -37,10 +37,8 @@ sqlx = { version = "0.7.2", features = [
] }
urlencoding = "2.1.2"
base64 = "0.21.4"
axum-macros = "0.3.8"
axum-extra = "0.8.0"
handlebars-concat = "0.1.2"
serde-querystring-axum = "0.2.0"
axum-macros = "0.4"
axum-extra = "0.9"
url = { version = "2.4.1", features = ["serde"] }

[dependencies.uuid]
Expand Down
12 changes: 8 additions & 4 deletions unmnemonic_devices_vrs/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use crate::Prompts;
use crate::WrappedPrompts;
use crate::WrappedPromptsSerialisation;
use axum::response::Response;
use axum::{extract::State, http::StatusCode, middleware::Next};
use axum::{
extract::{Request, State},
http::StatusCode,
middleware::Next,
};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use sqlx::Row;
Expand Down Expand Up @@ -75,10 +79,10 @@ struct CallParams {
call_sid: Option<String>,
}

pub async fn store_call_path_middleware<B>(
pub async fn store_call_path_middleware(
State(state): State<AppState>,
req: axum::http::Request<B>,
next: Next<B>,
req: Request,
next: Next,
) -> Result<Response, StatusCode> {
if req.method() == axum::http::Method::GET {
if let Some(query) = req.uri().query() {
Expand Down
15 changes: 10 additions & 5 deletions unmnemonic_devices_vrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use axum::{
Router,
};
use axum_template::engine::Engine;
use handlebars::Handlebars;
use handlebars_concat::HandlebarsConcat;
use handlebars::{DirectorySourceOptions, Handlebars};
use helpers::{get_all_prompts, store_call_path_middleware};
use serde::Deserialize;
use sqlx::PgPool;
Expand Down Expand Up @@ -75,9 +74,15 @@ pub struct Prompts {

pub async fn app(services: InjectableServices) -> Router {
let mut hbs = Handlebars::new();
hbs.register_templates_directory(".hbs", "src/templates")
.expect("Failed to register templates directory");
hbs.register_helper("concat", Box::new(HandlebarsConcat));
hbs.register_templates_directory(
"src/templates",
DirectorySourceOptions {
tpl_extension: ".hbs".to_string(),
hidden: false,
temporary: false,
},
)
.expect("Failed to register templates directory");

let prompts_string =
fs::read_to_string("src/prompts.toml").expect("Failed to read the prompts file");
Expand Down
29 changes: 15 additions & 14 deletions unmnemonic_devices_vrs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axum::Server;
use sqlx::PgPool;
use std::env;
use std::net::{SocketAddr, TcpListener};
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use unmnemonic_devices_vrs::config::{ConfigProvider, EnvVarProvider};
use unmnemonic_devices_vrs::{app, InjectableServices};
Expand All @@ -15,7 +15,9 @@ async fn main() {
let db = PgPool::connect(database_url.as_str()).await.unwrap();

let listener_address = "127.0.0.1:3000";
let listener = TcpListener::bind(listener_address.parse::<SocketAddr>().unwrap()).unwrap();
let listener = TcpListener::bind(listener_address.parse::<SocketAddr>().unwrap())
.await
.expect("Failed to bind port 3000");

println!("unmnemonic devices VRS listening on {}", listener_address);

Expand All @@ -30,16 +32,15 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();

Server::from_tcp(listener)
.expect("Failed to listen")
.serve(
app(InjectableServices {
db,
twilio_address: config.twilio_url.to_string(),
})
.await
.into_make_service(),
)
axum::serve(
listener,
app(InjectableServices {
db,
twilio_address: config.twilio_url.to_string(),
})
.await
.expect("Failed to start server")
.into_make_service(),
)
.await
.expect("Failed to start server")
}
4 changes: 2 additions & 2 deletions unmnemonic_devices_vrs/src/render_xml.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{WrappedPrompts, WrappedPromptsSerialisation};
use axum::{
body::{Bytes, Full},
body::Body,
response::{IntoResponse, Response},
};
use axum_template::TemplateEngine;
Expand All @@ -14,7 +14,7 @@ pub struct Xml<T>(pub T);

impl<T> IntoResponse for Xml<T>
where
T: Into<Full<Bytes>>,
T: Into<Body>,
{
fn into_response(self) -> Response {
(
Expand Down
3 changes: 1 addition & 2 deletions unmnemonic_devices_vrs/src/routes/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use axum::{
use axum_template::Key;
use base64::{engine::general_purpose, Engine as _};
use serde::{Deserialize, Serialize};
use serde_querystring_axum::QueryString;

use crate::{helpers::TwilioParams, render_xml::RenderXml, twilio_form::TwilioForm, AppState};

Expand Down Expand Up @@ -88,7 +87,7 @@ pub struct RootData {
pub async fn get_root(
Key(key): Key,
State(state): State<AppState>,
params: QueryString<RootParams>,
params: Query<RootParams>,
) -> impl IntoResponse {
sqlx::query!(
r#"
Expand Down
5 changes: 2 additions & 3 deletions unmnemonic_devices_vrs/src/routes/teams.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use axum::{
extract::{Path, State},
extract::{Path, Query, State},
response::{IntoResponse, Redirect},
Form,
};
use axum_template::Key;
use base64::{engine::general_purpose, Engine as _};
use serde::Serialize;
use serde_querystring_axum::QueryString;
use sqlx::types::Uuid;
use std::env;

Expand Down Expand Up @@ -166,7 +165,7 @@ pub async fn get_team(
Key(key): Key,
Path(id): Path<Uuid>,
State(state): State<AppState>,
params: QueryString<TwilioParams>,
params: Query<TwilioParams>,
) -> impl IntoResponse {
sqlx::query!(
r#"
Expand Down
16 changes: 9 additions & 7 deletions unmnemonic_devices_vrs/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub mod helpers {
use axum::Server;
use base64::{engine::general_purpose, Engine as _};
use sqlx::PgPool;
use std::env;
use std::net::TcpListener;
use tokio::net::TcpListener;
use unmnemonic_devices_vrs::config::{ConfigProvider, EnvVarProvider};
use unmnemonic_devices_vrs::{app, InjectableServices};
use wiremock::matchers::any;
Expand All @@ -14,14 +13,17 @@ pub mod helpers {
}

pub async fn spawn_app(services: InjectableServices) -> TestApp {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let address = format!("http://127.0.0.1:{}", port);

let server = Server::from_tcp(listener)
.expect("Failed to listen")
.serve(app(services).await.into_make_service());
let _ = tokio::spawn(server);
tokio::spawn(async move {
axum::serve(listener, app(services).await.into_make_service())
.await
.unwrap();
});

TestApp { address }
}
Expand Down
2 changes: 1 addition & 1 deletion unmnemonic_devices_vrs/tests/voice/recordings_prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async fn get_character_prompts_404s_for_unknown_character(db: PgPool) {
.await
.expect("Failed to execute request.");

assert_eq!(response.status(), http::StatusCode::NOT_FOUND);
assert_eq!(response.status(), 404);
}

#[sqlx::test(fixtures("schema", "teams"))]
Expand Down