Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MXWXZ committed Aug 30, 2024
1 parent 9fe99a4 commit ca28e1f
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 41 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 0.4.0
## New
1. `generate_response` now support custom import prefix.
2. Feature: `all`.

## Fix
1. `actix_cloud::main` can be used as re-export.
2. `chrono` is re-exported.
3. Middleware error can be handled correctly.

# 0.3.0
## Breaking changes
1. `Logger::init` no longer consumes builder.
Expand Down
2 changes: 1 addition & 1 deletion actix-cloud-codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-cloud-codegen"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["MXWXZ <[email protected]>"]
description = "Proc macros for Actix Cloud."
Expand Down
6 changes: 2 additions & 4 deletions actix-cloud-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod i18n;
#[proc_macro_attribute]
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
let mut output: TokenStream = (quote! {
#[::actix_cloud::actix_web::rt::main(system = "::actix_cloud::actix_web::rt::System")]
#[actix_cloud::actix_web::rt::main(system = "actix_cloud::actix_web::rt::System")]
})
.into();

Expand Down Expand Up @@ -64,8 +64,6 @@ pub fn i18n(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
///
/// Automatically generate `created_at` and `updated_at` on create and update.
///
/// Crate `chrono` will be used.
///
/// # Examples
/// ```ignore
/// pub struct Model {
Expand All @@ -83,7 +81,7 @@ pub fn entity_timestamp(_: TokenStream, input: TokenStream) -> TokenStream {
entity.items.push(syn::parse_quote!(
fn entity_timestamp(&self, e: &mut Self, insert: bool) {
let tm: sea_orm::ActiveValue<i64> =
sea_orm::ActiveValue::set(chrono::Utc::now().timestamp_millis());
sea_orm::ActiveValue::set(actix_cloud::chrono::Utc::now().timestamp_millis());
if insert {
e.created_at = tm.clone();
e.updated_at = tm.clone();
Expand Down
17 changes: 15 additions & 2 deletions actix-cloud/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-cloud"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["MXWXZ <[email protected]>"]
description = "Actix Cloud is an all-in-one web framework based on Actix Web."
Expand All @@ -17,6 +17,19 @@ categories = [

[features]
default = ["logger"]
all = [
"i18n",
"redis",
"session",
"config-yaml",
"config-json",
"config-toml",
"response-json",
"traceid",
"rustls",
"seaorm",
"csrf",
]
serde = ["dep:serde", "dep:serde_json", "dep:serde_with"]
memorydb = ["dep:glob"]
redis = ["dep:redis", "memorydb"]
Expand Down Expand Up @@ -54,7 +67,7 @@ anyhow = "1.0.86"
rand = "0.8.5"
hex = "0.4.3"

actix-cloud-codegen = { version = "0.2.0", path = "../actix-cloud-codegen" }
actix-cloud-codegen = { version = "0.2.1", path = "../actix-cloud-codegen" }

# serde
serde = { version = "1.0.208", features = ["derive"], optional = true }
Expand Down
23 changes: 7 additions & 16 deletions actix-cloud/src/csrf.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{future::Future, rc::Rc};

use actix_web::{
body::EitherBody,
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
HttpMessage, HttpRequest, HttpResponse,
HttpMessage, HttpRequest,
};
use futures::future::{ready, LocalBoxFuture, Ready};
use qstring::QString;
Expand Down Expand Up @@ -48,7 +47,7 @@ where
F: Fn(HttpRequest, String) -> Fut + 'static,
Fut: Future<Output = Result<bool, actix_web::Error>>,
{
type Response = ServiceResponse<EitherBody<B>>;
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type InitError = ();
type Transform = MiddlewareService<S, F>;
Expand Down Expand Up @@ -124,7 +123,7 @@ where
F: Fn(HttpRequest, String) -> Fut + 'static,
Fut: Future<Output = Result<bool, actix_web::Error>>,
{
type Response = ServiceResponse<EitherBody<B>>;
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

Expand Down Expand Up @@ -152,20 +151,12 @@ where
Self::check_csrf(&req, &cookie, &header, checker, true).await
}
CSRFType::Disabled => Ok(true),
};
if ret.is_err() {
return Ok(req.into_response(
HttpResponse::InternalServerError()
.finish()
.map_into_right_body(),
));
}
if ret.is_ok_and(|x| !x) {
return Ok(req
.into_response(HttpResponse::BadRequest().finish().map_into_right_body()));
}?;
if !ret {
return Err(actix_web::error::ErrorBadRequest("CSRF check failed"));
}
}
Ok(srv.call(req).await?.map_into_left_body())
srv.call(req).await
})
}
}
1 change: 1 addition & 0 deletions actix-cloud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use anyhow::bail;
pub use anyhow::Error;
pub use anyhow::Result;
pub use async_trait::async_trait;
pub use chrono;
#[cfg(feature = "config")]
pub use config;
pub use macros::main;
Expand Down
10 changes: 7 additions & 3 deletions actix-cloud/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ pub enum BuildError {
/// ```no_run
/// use actix_cloud::response::generate_response;
///
/// generate_response("response", "response.rs").unwrap();
/// generate_response("", "response", "response.rs").unwrap();
/// ```
pub fn generate_response(input: &str, output: &str) -> anyhow::Result<()> {
pub fn generate_response(import_prefix: &str, input: &str, output: &str) -> anyhow::Result<()> {
use std::io::Write;

let outfile = std::path::Path::new(&std::env::var("OUT_DIR")?).join(output);
let mut output = std::fs::File::create(&outfile)?;
writeln!(output, "use actix_cloud::response::ResponseCodeTrait;")?;
writeln!(
output,
"use {}actix_cloud::response::ResponseCodeTrait;",
import_prefix
)?;
for entry in walkdir::WalkDir::new(input) {
let entry = entry?;
if entry.file_type().is_file() {
Expand Down
21 changes: 7 additions & 14 deletions actix-cloud/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use std::{
#[cfg(feature = "csrf")]
use actix_web::HttpMessage;
use actix_web::{
body::EitherBody,
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
web::ServiceConfig,
HttpResponse, Route,
Route,
};
use anyhow::Result;
use async_trait::async_trait;
Expand Down Expand Up @@ -86,7 +85,7 @@ where
S::Future: 'static,
B: 'static + Debug,
{
type Response = ServiceResponse<EitherBody<B>>;
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type InitError = ();
type Transform = RouterGuardMiddleware<S>;
Expand Down Expand Up @@ -115,7 +114,7 @@ where
S::Future: 'static,
B: 'static + Debug,
{
type Response = ServiceResponse<EitherBody<B>>;
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

Expand All @@ -131,21 +130,15 @@ where
match checker.check(&mut req).await {
Ok(ok) => {
if ok {
Ok(srv.call(req).await?.map_into_left_body())
srv.call(req).await
} else {
Ok(req.into_response(
HttpResponse::Forbidden().finish().map_into_right_body(),
))
Err(actix_web::error::ErrorForbidden("Checker failed"))
}
}
Err(_) => Ok(req.into_response(
HttpResponse::InternalServerError()
.finish()
.map_into_right_body(),
)),
Err(e) => Err(actix_web::error::ErrorInternalServerError(e)),
}
} else {
Ok(srv.call(req).await?.map_into_left_body())
srv.call(req).await
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/response/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_cloud::response::generate_response;

fn main() {
generate_response("response", "response.rs").unwrap();
generate_response("", "response", "response.rs").unwrap();
}

0 comments on commit ca28e1f

Please sign in to comment.