Skip to content

Commit c31b18b

Browse files
committed
0.4.15
1 parent 702476d commit c31b18b

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.4.15
2+
## New
3+
1. `Response` allows for multiple `builder`.
4+
2. `Response` add several convenient methods.
5+
16
# 0.4.14
27
## New
38
1. Default memory database now support basic capacity and key eviction.

actix-cloud/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "actix-cloud"
3-
version = "0.4.14"
3+
version = "0.4.15"
44
edition = "2021"
55
authors = ["MXWXZ <matrixwxz@gmail.com>"]
66
description = "Actix Cloud is an all-in-one web framework based on Actix Web."
@@ -145,7 +145,7 @@ actix-web = { version = "4", features = ["secure-cookies"], optional = true }
145145
# memorydb
146146
glob = { version = "0.3", optional = true }
147147
parking_lot = { version = "0.12", optional = true }
148-
priority-queue = { version = "2.5", optional = true }
148+
priority-queue = { version = "2.6", optional = true }
149149

150150
# chrono
151151
chrono = { version = "0.4", features = ["serde"], optional = true }

actix-cloud/src/response.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::{self, Display};
22

33
use actix_web::{
44
http::{
5-
header::{ContentDisposition, DispositionParam, DispositionType},
5+
header::{self, ContentDisposition, DispositionParam, DispositionType},
66
StatusCode,
77
},
88
HttpResponse, HttpResponseBuilder,
@@ -51,7 +51,7 @@ pub struct Response<T> {
5151
pub code: i64,
5252
pub message: String,
5353
pub data: Option<T>,
54-
pub builder: Option<ResponseBuilderFn>,
54+
pub builder: Vec<ResponseBuilderFn>,
5555
#[cfg(feature = "i18n")]
5656
pub translate: bool,
5757
}
@@ -66,7 +66,7 @@ impl<T> Response<T> {
6666
code: r.code(),
6767
message: r.message().to_owned(),
6868
data: None,
69-
builder: None,
69+
builder: Vec::new(),
7070
#[cfg(feature = "i18n")]
7171
translate: true,
7272
}
@@ -78,7 +78,7 @@ impl<T> Response<T> {
7878
code: 0,
7979
message: String::new(),
8080
data: None,
81-
builder: None,
81+
builder: Vec::new(),
8282
#[cfg(feature = "i18n")]
8383
translate: false,
8484
}
@@ -88,15 +88,26 @@ impl<T> Response<T> {
8888
Self::new_code(400).message(s)
8989
}
9090

91+
pub fn forbidden() -> Self {
92+
Self::new_code(403)
93+
}
94+
9195
pub fn not_found() -> Self {
9296
Self::new_code(404)
9397
}
9498

99+
pub fn redirect<S: Into<String>>(code: u16, s: S) -> Self {
100+
let s: String = s.into();
101+
Self::new_code(code).builder(move |r| {
102+
r.insert_header((header::LOCATION, s.clone()));
103+
})
104+
}
105+
95106
pub fn builder<F>(mut self, f: F) -> Self
96107
where
97108
F: Fn(&mut HttpResponseBuilder) + 'static,
98109
{
99-
self.builder = Some(Box::new(f));
110+
self.builder.push(Box::new(f));
100111
self
101112
}
102113

@@ -190,15 +201,17 @@ impl actix_web::Responder for JsonResponse {
190201
let mut rsp =
191202
HttpResponse::build(actix_web::http::StatusCode::from_u16(self.http_code).unwrap());
192203
rsp.content_type(actix_web::http::header::ContentType::json());
193-
if let Some(builder) = self.builder {
204+
for builder in self.builder {
194205
builder(&mut rsp);
195206
}
196207
rsp.message_body(body).unwrap().map_into_left_body()
197208
} else {
198-
HttpResponse::build(actix_web::http::StatusCode::from_u16(self.http_code).unwrap())
199-
.message_body(self.message)
200-
.unwrap()
201-
.map_into_left_body()
209+
let mut rsp =
210+
HttpResponse::build(actix_web::http::StatusCode::from_u16(self.http_code).unwrap());
211+
for builder in self.builder {
212+
builder(&mut rsp);
213+
}
214+
rsp.message_body(self.message).unwrap().map_into_left_body()
202215
}
203216
}
204217
}

0 commit comments

Comments
 (0)