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
60 changes: 31 additions & 29 deletions frameworks/Rust/xitca-web/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions frameworks/Rust/xitca-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ codegen-units = 1
panic = "abort"

[patch.crates-io]
xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "1bd39ac" }
xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "270fe35" }
xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "b6d1922" }
xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "1ce7536" }

toasty = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
toasty-core = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
Expand All @@ -108,9 +108,9 @@ toasty-sql = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
# personal fork of tokio-uring with tokio local runtime enabled
tokio-uring = { git = "http://github.com/fakeshadow/tokio-uring", rev = "c3d5887" }

xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" }
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" }
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" }
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" }
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" }
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" }
xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" }
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" }
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" }
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" }
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" }
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" }
1 change: 1 addition & 0 deletions frameworks/Rust/xitca-web/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"db_url": "/db",
"fortune_url": "/fortunes",
"query_url": "/queries?q=",
"update_url": "/updates?q=",
"port": 8080,
"approach": "realistic",
"classification": "fullstack",
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/xitca-web/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Exec {
let mut fortunes = Vec::with_capacity(16);

while let Some(row) = res.try_next().await? {
fortunes.push(Fortune::new(row.get(0), row.get::<String>(1)));
fortunes.push(Fortune::new(row.get(0), row.get_zc(1)));
}

Ok(Fortunes::new(fortunes))
Expand Down
4 changes: 3 additions & 1 deletion frameworks/Rust/xitca-web/src/db_unrealistic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub struct Client {

impl Client {
pub async fn create() -> HandleResult<Self> {
let (cli, mut drv) = xitca_postgres::Postgres::new(DB_URL).connect().await?;
let (cli, drv) = xitca_postgres::Postgres::new(DB_URL).connect().await?;

let mut drv = drv.try_into_tcp().expect("raw tcp is used for database connection");

tokio::task::spawn(async move {
while drv.try_next().await?.is_some() {}
Expand Down
59 changes: 48 additions & 11 deletions frameworks/Rust/xitca-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ mod util;

use xitca_http::{
HttpServiceBuilder,
body::Once,
bytes::Bytes,
h1::RequestBody,
http::{StatusCode, header::SERVER},
http::{
self, IntoResponse as _, RequestExt, StatusCode,
const_header_value::{TEXT_HTML_UTF8, TEXT_UTF8},
header::{CONTENT_TYPE, SERVER},
},
util::{
middleware::context::{Context, ContextBuilder},
service::{
Expand All @@ -22,51 +28,64 @@ use xitca_http::{
};
use xitca_service::{Service, ServiceExt, fn_service};

use ser::{IntoResponse, Message, Request, Response, error_response};
use util::{QueryParse, SERVER_HEADER_VALUE};
use ser::{HELLO_BYTES, Message};
use util::{HandleResult, QueryParse, SERVER_HEADER_VALUE};

type Request<B> = http::Request<RequestExt<B>>;

type Response = http::Response<Once<Bytes>>;

type Ctx<'a> = Context<'a, Request<RequestBody>, db_pool::Client>;

fn main() -> std::io::Result<()> {
let service = Router::new()
.insert(
"/plaintext",
get(fn_service(async |ctx: Ctx| Ok(ctx.into_parts().0.text_response()))),
get(fn_service(async |ctx: Ctx| {
let (req, _) = ctx.into_parts();
let mut res = req.into_response(const { Bytes::from_static(HELLO_BYTES) });
res.headers_mut().insert(CONTENT_TYPE, TEXT_UTF8);
Ok(res)
})),
)
.insert(
"/json",
get(fn_service(async |ctx: Ctx| {
ctx.into_parts().0.json_response(&Message::new())
let (req, _) = ctx.into_parts();
json_response(req, &Message::new())
})),
)
.insert(
"/db",
get(fn_service(async |ctx: Ctx| {
let (req, cli) = ctx.into_parts();
cli.db().await.and_then(|w| req.json_response(&w))
cli.db().await.and_then(|w| json_response(req, &w))
})),
)
.insert(
"/fortunes",
get(fn_service(async |ctx: Ctx| {
let (req, cli) = ctx.into_parts();
cli.fortunes().await?.render_once().map(|f| req.html_response(f))
let fortunes = cli.fortunes().await?.render_once()?;
let mut res = req.into_response(Bytes::from(fortunes));
res.headers_mut().insert(CONTENT_TYPE, TEXT_HTML_UTF8);
Ok(res)
})),
)
.insert(
"/queries",
get(fn_service(async |ctx: Ctx| {
let (req, cli) = ctx.into_parts();
let num = req.uri().query().parse_query();
cli.queries(num).await.and_then(|w| req.json_response(&w))
cli.queries(num).await.and_then(|w| json_response(req, &w))
})),
)
.insert(
"/updates",
get(fn_service(async |ctx: Ctx| {
let (req, cli) = ctx.into_parts();
let num = req.uri().query().parse_query();
cli.updates(num).await.and_then(|w| req.json_response(&w))
cli.updates(num).await.and_then(|w| json_response(req, &w))
})),
)
.enclosed(ContextBuilder::new(db_pool::Client::create))
Expand All @@ -85,12 +104,30 @@ fn main() -> std::io::Result<()> {
#[cold]
#[inline(never)]
fn error_handler(e: RouterError<util::Error>) -> Response {
error_response(match e {
let status = match e {
RouterError::Match(_) => StatusCode::NOT_FOUND,
RouterError::NotAllowed(_) => StatusCode::METHOD_NOT_ALLOWED,
RouterError::Service(e) => {
eprintln!("Internal Error: {e}");
StatusCode::INTERNAL_SERVER_ERROR
}
})
};
http::Response::builder()
.status(status)
.body(Once::new(Bytes::new()))
.unwrap()
}

#[cfg(any(feature = "json", feature = "perf-json"))]
fn json_response<Ext>(req: Request<Ext>, val: &impl serde_core::Serialize) -> HandleResult<Response> {
let mut buf = xitca_http::bytes::BytesMut::new();
#[cfg(all(feature = "json", not(feature = "perf-json")))]
serde_json::to_writer(xitca_http::bytes::BufMutWriter(&mut buf), val)?;

#[cfg(all(feature = "perf-json", not(feature = "json")))]
sonic_rs::to_writer(xitca_http::bytes::BufMut::writer(&mut buf), val)?;

let mut res = req.into_response(buf.freeze());
res.headers_mut().insert(CONTENT_TYPE, http::const_header_value::JSON);
Ok(res)
}
Loading
Loading