Skip to main content

a3s_boot/routing/route/
response.rs

1use super::definition::RouteDefinition;
2use crate::{BootResponse, BoxFuture, ExecutionContext, Interceptor, Result};
3
4impl RouteDefinition {
5    pub fn with_response_header(self, name: impl Into<String>, value: impl Into<String>) -> Self {
6        self.with_interceptor(StaticResponseHeader {
7            name: name.into(),
8            value: value.into(),
9        })
10    }
11
12    pub fn with_redirect(self, location: impl Into<String>) -> Self {
13        self.with_redirect_status(302, location)
14    }
15
16    pub fn with_redirect_status(self, status: u16, location: impl Into<String>) -> Self {
17        self.with_interceptor(StaticRedirect {
18            status,
19            location: location.into(),
20        })
21    }
22}
23
24struct StaticResponseHeader {
25    name: String,
26    value: String,
27}
28
29impl Interceptor for StaticResponseHeader {
30    fn after(
31        &self,
32        _context: ExecutionContext,
33        response: BootResponse,
34    ) -> BoxFuture<'static, Result<BootResponse>> {
35        let name = self.name.clone();
36        let value = self.value.clone();
37        Box::pin(async move { Ok(response.with_header(name, value)) })
38    }
39}
40
41struct StaticRedirect {
42    status: u16,
43    location: String,
44}
45
46impl Interceptor for StaticRedirect {
47    fn after(
48        &self,
49        _context: ExecutionContext,
50        _response: BootResponse,
51    ) -> BoxFuture<'static, Result<BootResponse>> {
52        let status = self.status;
53        let location = self.location.clone();
54        Box::pin(async move { Ok(BootResponse::redirect_with_status(status, location)) })
55    }
56}