Skip to content
Merged
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
378 changes: 340 additions & 38 deletions frameworks/Rust/rama/Cargo.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions frameworks/Rust/rama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ default = []
simd-json = ["dep:simd-json", "dep:mime", "dep:serde_path_to_error"]

[dependencies]
rama = { version = "0.2.0-alpha.11", default-features = false, features = [
"http-full",
] }
rama = { version = "0.2.0", default-features = false, features = ["http-full"] }
deadpool = { version = "0.12", features = ["rt_tokio_1", "serde", "managed"] }
deadpool-postgres = { version = "0.14", features = ["rt_tokio_1", "serde"] }
dotenv = "0.15"
Expand Down
5 changes: 3 additions & 2 deletions frameworks/Rust/rama/src/common/simd_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

use bytes::{BufMut, BytesMut};
use rama::http::{
HeaderMap, HeaderValue, IntoResponse, Request, Response, StatusCode, header,
HeaderMap, HeaderValue, Request, Response, StatusCode, header,
service::web::extract::{
Bytes, FromRequest,
body::{BytesRejection, InvalidJsonContentType, JsonRejection},
},
service::web::response::IntoResponse,
};
use serde::{Serialize, de::DeserializeOwned};
use simd_json;
Expand Down Expand Up @@ -83,7 +84,7 @@ fn json_content_type(headers: &HeaderMap) -> bool {

let is_json_content_type = mime.type_() == "application"
&& (mime.subtype() == "json"
|| mime.suffix().map_or(false, |name| name == "json"));
|| mime.suffix().is_some_and(|name| name == "json"));

is_json_content_type
}
Expand Down
26 changes: 1 addition & 25 deletions frameworks/Rust/rama/src/common/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bytes::Bytes;
use rama::http::{HeaderValue, IntoResponse, Response, StatusCode, header};
use rama::http::StatusCode;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
Expand All @@ -26,26 +25,3 @@ where
{
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
}

#[derive(Clone, Copy, Debug)]
pub struct Utf8Html<T>(pub T);

impl<T> IntoResponse for Utf8Html<T>
where
T: Into<Bytes>,
{
fn into_response(self) -> Response {
let mut res = (StatusCode::OK, self.0.into()).into_response();
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res
}
}

impl<T> From<T> for Utf8Html<T> {
fn from(inner: T) -> Self {
Self(inner)
}
}
7 changes: 5 additions & 2 deletions frameworks/Rust/rama/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ static GLOBAL: MiMalloc = MiMalloc;
#[cfg(feature = "simd-json")]
use common::simd_json::Json;
#[cfg(not(feature = "simd-json"))]
use rama::http::response::Json;
use rama::http::{IntoResponse, StatusCode, service::web::Router};
use rama::http::service::web::response::Json;
use rama::http::{
StatusCode,
service::web::{Router, response::IntoResponse},
};

/// Return a plaintext static string.
#[inline(always)]
Expand Down
14 changes: 9 additions & 5 deletions frameworks/Rust/rama/src/main_mongo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ use mongodb::{
options::{ClientOptions, Compressor},
};
#[cfg(not(feature = "simd-json"))]
use rama::http::response::Json;
use rama::http::service::web::response::Json;
use rama::http::{
IntoResponse, StatusCode,
service::web::{Router, extract::Query},
StatusCode,
service::web::{
Router,
extract::Query,
response::{Html, IntoResponse},
},
};
use rand::{SeedableRng, rng, rngs::SmallRng};
use yarte::Template;
Expand All @@ -30,7 +34,7 @@ static GLOBAL: MiMalloc = MiMalloc;

use common::{
get_env,
utils::{Params, Utf8Html, parse_params},
utils::{Params, parse_params},
};
use mongo::database::{
DatabaseConnection, fetch_fortunes, find_world_by_id, find_worlds, update_worlds,
Expand Down Expand Up @@ -101,7 +105,7 @@ async fn fortunes(DatabaseConnection(db): DatabaseConnection) -> impl IntoRespon
})
.collect();

Utf8Html(
Html(
FortunesTemplate {
fortunes: &fortune_infos,
}
Expand Down
6 changes: 3 additions & 3 deletions frameworks/Rust/rama/src/main_mongo_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use common::{
utils::{Params, parse_params},
};
use rama::http::{
IntoResponse, StatusCode,
service::web::{Router, extract::Query},
StatusCode,
service::web::{Router, extract::Query, response::IntoResponse},
};
use std::time::Duration;

Expand All @@ -25,7 +25,7 @@ static GLOBAL: MiMalloc = MiMalloc;
#[cfg(feature = "simd-json")]
use common::simd_json::Json;
#[cfg(not(feature = "simd-json"))]
use rama::http::response::Json;
use rama::http::service::web::response::Json;

use dotenv::dotenv;
use mongodb::{
Expand Down
14 changes: 9 additions & 5 deletions frameworks/Rust/rama/src/main_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ mod pg;
use dotenv::dotenv;
use mimalloc::MiMalloc;
use rama::http::{
IntoResponse, StatusCode,
service::web::{Router, extract::Query},
StatusCode,
service::web::{
Router,
extract::Query,
response::{Html, IntoResponse},
},
};
use rand::rng;
use yarte::Template;
Expand All @@ -16,13 +20,13 @@ static GLOBAL: MiMalloc = MiMalloc;
#[cfg(feature = "simd-json")]
use common::simd_json::Json;
#[cfg(not(feature = "simd-json"))]
use rama::http::response::Json;
use rama::http::service::web::response::Json;

mod server;

use common::{
get_env, random_id,
utils::{Params, Utf8Html, parse_params},
utils::{Params, parse_params},
};
use pg::database::{DatabaseConnection, PgConnection};
use pg::models::Fortune;
Expand Down Expand Up @@ -63,7 +67,7 @@ async fn fortunes(DatabaseConnection(conn): DatabaseConnection) -> impl IntoResp
.await
.expect("error loading fortunes");

Utf8Html(
Html(
FortunesTemplate {
fortunes: &fortunes,
}
Expand Down
14 changes: 9 additions & 5 deletions frameworks/Rust/rama/src/main_pg_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ mod pg_pool;
#[cfg(feature = "simd-json")]
use common::simd_json::Json;
#[cfg(not(feature = "simd-json"))]
use rama::http::response::Json;
use rama::http::service::web::response::Json;

use common::{SELECT_ALL_FORTUNES, SELECT_WORLD_BY_ID, UPDATE_WORLDS, random_ids};
use dotenv::dotenv;
use futures_util::{TryStreamExt, stream::FuturesUnordered};
use mimalloc::MiMalloc;
use rama::http::{
IntoResponse, StatusCode,
service::web::{Router, extract::Query},
StatusCode,
service::web::{
Router,
extract::Query,
response::{Html, IntoResponse},
},
};
use rand::{SeedableRng, rng, rngs::SmallRng};
use yarte::Template;
Expand All @@ -24,7 +28,7 @@ mod server;

use common::{
get_env, random_id,
utils::{Params, Utf8Html, parse_params},
utils::{Params, parse_params},
};
use pg_pool::database::{
DatabaseClient, PgError, create_pool, fetch_all_fortunes, fetch_world_by_id,
Expand Down Expand Up @@ -82,7 +86,7 @@ async fn fortunes(DatabaseClient(client): DatabaseClient) -> impl IntoResponse {

fortunes.sort_by(|a, b| a.message.cmp(&b.message));

Utf8Html(
Html(
FortunesTemplate {
fortunes: &fortunes,
}
Expand Down
22 changes: 14 additions & 8 deletions frameworks/Rust/rama/src/main_sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ use quick_cache::sync::Cache;
use rama::{
Context,
http::{
IntoResponse, StatusCode,
service::web::{Router, extract::Query},
StatusCode,
service::web::{
Router,
extract::Query,
response::{Html, IntoResponse},
},
},
};
use rand::{SeedableRng, rng, rngs::SmallRng};
Expand All @@ -22,15 +26,15 @@ use yarte::Template;
static GLOBAL: MiMalloc = MiMalloc;

#[cfg(not(feature = "simd-json"))]
use rama::http::response::Json;
use rama::http::service::web::response::Json;
#[cfg(feature = "simd-json")]
use rama::http::response::Json;
use rama::http::service::web::response::Json;

mod server;

use common::{
get_env, random_id, random_ids,
utils::{Params, Utf8Html, parse_params},
utils::{Params, parse_params},
};
use sqlx::database::create_pool;
use sqlx::models::Fortune;
Expand Down Expand Up @@ -91,7 +95,7 @@ async fn fortunes(ctx: Context<AppState>) -> impl IntoResponse {

fortunes.sort_by(|a, b| a.message.cmp(&b.message));

Utf8Html(
Html(
FortunesTemplate {
fortunes: &fortunes,
}
Expand All @@ -106,10 +110,12 @@ async fn cache(
) -> impl IntoResponse {
let count = parse_params(params);
let mut rng = SmallRng::from_rng(&mut rng());
let mut worlds: Vec<Option<World>> = Vec::with_capacity(count);
let mut worlds: Vec<World> = Vec::with_capacity(count);

for id in random_ids(&mut rng, count) {
worlds.push(ctx.state().cache.get(&id));
if let Some(world) = ctx.state().cache.get(&id) {
worlds.push(world);
}
}

(StatusCode::OK, Json(worlds))
Expand Down
Loading