mirror of https://github.com/zino-rs/zino
Version 0.2.1
This commit is contained in:
parent
0ec48f9e88
commit
38ec03b8f5
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "axum-app"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{Request, RequestContext, Response};
|
||||
use serde_json::json;
|
||||
|
||||
pub(crate) async fn index(req: Request) -> crate::Result {
|
||||
pub(crate) async fn index(req: Request) -> zino::Result {
|
||||
let mut res = Response::default();
|
||||
res.set_data(json!({
|
||||
"method": "GET",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{Model, Query, Rejection, Request, RequestContext, Response, Schema, User, Uuid};
|
||||
use serde_json::json;
|
||||
|
||||
pub(crate) async fn new(mut req: Request) -> crate::Result {
|
||||
pub(crate) async fn new(mut req: Request) -> zino::Result {
|
||||
let mut user = User::new();
|
||||
let mut res = req.model_validation(&mut user).await?;
|
||||
|
||||
|
@ -15,14 +15,14 @@ pub(crate) async fn new(mut req: Request) -> crate::Result {
|
|||
Ok(res.into())
|
||||
}
|
||||
|
||||
pub(crate) async fn update(mut req: Request) -> crate::Result {
|
||||
pub(crate) async fn update(mut req: Request) -> zino::Result {
|
||||
let mut user = User::new();
|
||||
let validation = req.parse_body().await.map(|body| user.read_map(body))?;
|
||||
let res = Response::from(validation);
|
||||
Ok(res.into())
|
||||
}
|
||||
|
||||
pub(crate) async fn list(req: Request) -> crate::Result {
|
||||
pub(crate) async fn list(req: Request) -> zino::Result {
|
||||
let mut query = Query::new();
|
||||
let mut res = req.query_validation(&mut query)?;
|
||||
|
||||
|
@ -36,7 +36,7 @@ pub(crate) async fn list(req: Request) -> crate::Result {
|
|||
Ok(res.into())
|
||||
}
|
||||
|
||||
pub(crate) async fn view(mut req: Request) -> crate::Result {
|
||||
pub(crate) async fn view(mut req: Request) -> zino::Result {
|
||||
let user_id = req.parse_params::<Uuid>().await?;
|
||||
let mut query = Query::new();
|
||||
let mut res = req.query_validation(&mut query)?;
|
||||
|
|
|
@ -2,7 +2,7 @@ mod controller;
|
|||
mod router;
|
||||
|
||||
/// Reexports.
|
||||
use zino::{AxumCluster, Request, Result};
|
||||
use zino::{AxumCluster, Request};
|
||||
use zino_core::{Application, Model, Query, Rejection, RequestContext, Response, Schema, Uuid};
|
||||
use zino_model::User;
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ use axum::{
|
|||
use std::collections::HashMap;
|
||||
|
||||
pub(crate) fn init() -> HashMap<&'static str, Router> {
|
||||
let mut parties = HashMap::new();
|
||||
let mut routes = HashMap::new();
|
||||
|
||||
// User controller.
|
||||
let controller = Router::new()
|
||||
|
@ -14,11 +14,11 @@ pub(crate) fn init() -> HashMap<&'static str, Router> {
|
|||
.route("/update", post(user::update))
|
||||
.route("/list", get(user::list))
|
||||
.route("/:id/view", get(user::view));
|
||||
parties.insert("/user", controller);
|
||||
routes.insert("/user", controller);
|
||||
|
||||
// Stats controller.
|
||||
let controller = Router::new().route("/", get(stats::index));
|
||||
parties.insert("/stats", controller);
|
||||
routes.insert("/stats", controller);
|
||||
|
||||
parties
|
||||
routes
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "zino-core"
|
||||
description = "Core types and traits for zino."
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
homepage = "https://github.com/photino/zino"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "zino-derive"
|
||||
description = "Derived traits for zino."
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
homepage = "https://github.com/photino/zino"
|
||||
|
@ -13,10 +13,10 @@ readme = "README.md"
|
|||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = { version = "1.0.49" }
|
||||
quote = { version = "1.0.23" }
|
||||
syn = { version = "1.0.107", features = ["full", "extra-traits"] }
|
||||
proc-macro2 = { version = "1.0.49" }
|
||||
|
||||
[dependencies.zino-core]
|
||||
path = "../zino-core"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
# zino-derive
|
||||
Derived traits for zino
|
||||
Derived traits for zino.
|
|
@ -8,11 +8,12 @@ use syn::{parse_macro_input, Data, DeriveInput, Fields};
|
|||
|
||||
mod parser;
|
||||
|
||||
/// Integer types
|
||||
static INTEGER_TYPES: [&str; 6] = ["u64", "i64", "u32", "i32", "u16", "i16"];
|
||||
|
||||
/// Derive the `Schema` trait.
|
||||
#[proc_macro_derive(Schema, attributes(schema))]
|
||||
pub fn schema_macro(item: TokenStream) -> TokenStream {
|
||||
/// Integer types
|
||||
const INTEGER_TYPES: [&str; 6] = ["u64", "i64", "u32", "i32", "u16", "i16"];
|
||||
|
||||
// Input
|
||||
let input = parse_macro_input!(item as DeriveInput);
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "zino-model"
|
||||
description = "Model types for zino."
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
homepage = "https://github.com/photino/zino"
|
||||
|
@ -11,12 +11,11 @@ readme = "README.md"
|
|||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
serde_json = { version = "1.0.91" }
|
||||
|
||||
[dependencies.zino-core]
|
||||
path = "../zino-core"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
|
||||
[dependencies.zino-derive]
|
||||
path = "../zino-derive"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
|
@ -1,2 +1,2 @@
|
|||
# zino-model
|
||||
Model types for zino
|
||||
Model types for zino.
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "zino"
|
||||
description = "A minimal MVC framework."
|
||||
version = "0.2.0"
|
||||
description = "A minimal web framework."
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
homepage = "https://github.com/photino/zino"
|
||||
|
@ -11,7 +11,7 @@ readme = "README.md"
|
|||
|
||||
[features]
|
||||
default = ["axum-server"]
|
||||
axum-server = ["axum", "tokio", "tokio-stream", "tower", "tower-http"]
|
||||
axum-server = ["dep:axum", "dep:tokio", "dep:tokio-stream", "dep:tower", "dep:tower-http"]
|
||||
|
||||
[dependencies]
|
||||
async-trait = { version = "0.1.60" }
|
||||
|
@ -23,12 +23,12 @@ serde = { version = "1.0.152", features = ["derive"] }
|
|||
serde_json = { version = "1.0.91" }
|
||||
serde_qs = { version = "0.10.1" }
|
||||
serde_urlencoded = { version = "0.7.1" }
|
||||
toml = { version = "0.5.10" }
|
||||
tokio = { version = "1.23.0", features = ["rt-multi-thread", "sync"], optional = true }
|
||||
tokio-stream = { version = "0.1.11", features = ["sync"], optional = true }
|
||||
toml = { version = "0.5.10" }
|
||||
tower = { version = "0.4.13", optional = true }
|
||||
tower-http = { version = "0.1.1", features = ["add-extension", "fs"], optional = true }
|
||||
|
||||
[dependencies.zino-core]
|
||||
path = "../zino-core"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
|
@ -1,2 +1,2 @@
|
|||
# zino
|
||||
A minimal MVC framework
|
||||
A minimal web framework.
|
|
@ -1,12 +1,18 @@
|
|||
use axum::{http::StatusCode, routing, Router, Server};
|
||||
use axum::{
|
||||
body::{Bytes, Full},
|
||||
http::{self, StatusCode},
|
||||
routing, Router, Server,
|
||||
};
|
||||
use futures::future;
|
||||
use std::{collections::HashMap, env, io, path::Path, time::Instant};
|
||||
use std::{
|
||||
collections::HashMap, convert::Infallible, env, io, net::SocketAddr, path::Path, time::Instant,
|
||||
};
|
||||
use tokio::runtime::Builder;
|
||||
use tower::layer;
|
||||
use tower_http::services::{ServeDir, ServeFile};
|
||||
use zino_core::{Application, State};
|
||||
use zino_core::{Application, Response, State};
|
||||
|
||||
/// An HTTP server cluster for axum.
|
||||
/// An HTTP server cluster for `axum`.
|
||||
pub struct AxumCluster {
|
||||
/// Start time.
|
||||
start_time: Instant,
|
||||
|
@ -83,14 +89,21 @@ impl Application for AxumCluster {
|
|||
for (path, route) in &routes {
|
||||
app = app.nest(path, route.clone());
|
||||
}
|
||||
app = app.route_layer(layer::layer_fn(
|
||||
crate::middleware::axum_context::ContextMiddleware::new,
|
||||
));
|
||||
app = app
|
||||
.fallback_service(tower::service_fn(|_| async {
|
||||
let res = Response::new(StatusCode::NOT_FOUND);
|
||||
Ok::<http::Response<Full<Bytes>>, Infallible>(res.into())
|
||||
}))
|
||||
.layer(layer::layer_fn(
|
||||
crate::middleware::axum_context::ContextMiddleware::new,
|
||||
));
|
||||
|
||||
let addr = listener
|
||||
.parse()
|
||||
.inspect(|addr| println!("listen on {addr}"))
|
||||
.unwrap_or_else(|_| panic!("invalid socket address: {listener}"));
|
||||
Server::bind(&addr).serve(app.into_make_service())
|
||||
Server::bind(&addr)
|
||||
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
|
||||
});
|
||||
for result in future::join_all(servers).await {
|
||||
if let Err(err) = result {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
//! A minimal MVC framework.
|
||||
//! A minimal web framework.
|
||||
|
||||
#![feature(async_fn_in_trait)]
|
||||
#![feature(once_cell)]
|
||||
#![feature(result_option_inspect)]
|
||||
|
||||
mod channel;
|
||||
mod cluster;
|
||||
|
@ -16,10 +17,10 @@ pub use cluster::axum_cluster::AxumCluster;
|
|||
pub use request::axum_request::AxumExtractor;
|
||||
|
||||
#[cfg(feature = "axum-server")]
|
||||
/// A specialized request extractor for axum.
|
||||
/// A specialized request extractor for `axum`.
|
||||
pub type Request = AxumExtractor<axum::http::Request<axum::body::Body>>;
|
||||
|
||||
#[cfg(feature = "axum-server")]
|
||||
/// A specialized `Result` type for axum.
|
||||
/// A specialized `Result` type for `axum`.
|
||||
pub type Result<T = axum::http::Response<axum::body::Full<axum::body::Bytes>>> =
|
||||
std::result::Result<T, T>;
|
||||
|
|
|
@ -15,7 +15,7 @@ use std::{
|
|||
use toml::value::Table;
|
||||
use zino_core::{CloudEvent, Context, Map, Rejection, RequestContext, State, Validation};
|
||||
|
||||
/// An HTTP request extractor for axum.
|
||||
/// An HTTP request extractor for `axum`.
|
||||
pub struct AxumExtractor<T>(pub T);
|
||||
|
||||
impl<T> Deref for AxumExtractor<T> {
|
||||
|
|
Loading…
Reference in New Issue