-
Notifications
You must be signed in to change notification settings - Fork 274
/
responses.rs
147 lines (132 loc) · 3.24 KB
/
responses.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![allow(clippy::unused_async)]
use axum_extra::extract::cookie::Cookie;
use loco_rs::prelude::*;
use serde::Serialize;
use utoipa::{openapi, OpenApi, ToSchema};
#[derive(Serialize)]
pub struct Health {
pub ok: bool,
}
/// return an empty response
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn empty() -> Result<Response> {
format::empty()
}
/// return an text response
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn text() -> Result<Response> {
format::text("Loco")
}
/// return an JSON response
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn json() -> Result<Response> {
format::json(Health { ok: true })
}
/// return an empty JSON response
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn empty_json() -> Result<Response> {
format::empty_json()
}
/// return an HTML response
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn html() -> Result<Response> {
format::html("hello, world")
}
/// return an redirect response
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn redirect() -> Result<Response> {
format::redirect("/dashboard")
}
/// return an custom status code response
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn render_with_status_code() -> Result<Response> {
format::render().status(201).empty()
}
/// return response with ETag header
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn etag() -> Result<Response> {
format::render().etag("loco-etag")?.empty()
}
/// return response with cookie
///
/// # Errors
///
/// This function will return an error if result fails
pub async fn set_cookie() -> Result<Response> {
let cookie = Cookie::build(("loco-cookie-name", "loco-cookie-value"))
// .domain("localhost:5173")
.path("/")
.same_site(cookie::SameSite::Strict)
.secure(true)
.http_only(true)
.build();
format::render().cookies(&[cookie])?.json(())
}
#[derive(Serialize, Debug, ToSchema)]
pub struct Album {
title: String,
rating: u32,
}
//
// OpenAPI spec with `utoipa`
//
#[derive(OpenApi)]
#[openapi(paths(album))]
struct Spec;
/// Return an OpenAPI-spec'd response
///
/// # Errors
///
/// This function will return an error if it fails
#[utoipa::path(
get,
path = "/response/album",
responses(
(status = 200, description = "Album found", body = Album),
),
)]
pub async fn album() -> Result<Response> {
println!("{}", Spec::openapi().to_pretty_json().unwrap());
format::json(Album {
title: "VH II".to_string(),
rating: 10,
})
}
pub fn routes() -> Routes {
Routes::new()
.prefix("response")
.add("/empty", get(empty))
.add("/text", get(text))
.add("/json", get(json))
.add("/empty_json", get(empty_json))
.add("/html", get(html))
.add("/redirect", get(redirect))
.add("/render_with_status_code", get(render_with_status_code))
.add("/etag", get(etag))
.add("/album", get(album))
.add("/set_cookie", get(set_cookie))
}