-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.rs
163 lines (149 loc) · 4.8 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! An example web service using ructe with actix web.
use actix_web::body::Body;
use actix_web::dev::ServiceResponse;
use actix_web::http::header::{ContentType, Expires};
use actix_web::http::{header, StatusCode};
use actix_web::middleware::errhandlers::{
ErrorHandlerResponse, ErrorHandlers,
};
use actix_web::web::Path;
use actix_web::{web, App, HttpResponse, HttpServer, Result};
use std::io::{self, Write};
use std::time::{Duration, SystemTime};
use templates::statics::StaticFile;
#[macro_use]
mod actix_ructe;
/// Main program: Set up routes and start server.
#[actix_web::main]
async fn main() {
env_logger::init();
HttpServer::new(|| {
App::new()
.wrap(
ErrorHandlers::new()
.handler(StatusCode::NOT_FOUND, render_404)
.handler(StatusCode::INTERNAL_SERVER_ERROR, render_500),
)
.service(web::resource("/").to(home_page))
.service(web::resource("/static/{filename}").to(static_file))
.service(web::resource("/int/{i}").to(take_int))
.service(web::resource("/bad").to(make_error))
})
.bind("127.0.0.1:8088")
.unwrap()
.run()
.await
.expect("Run server");
}
/// Home page handler; just render a template with some arguments.
fn home_page() -> HttpResponse {
HttpResponse::Ok().body(render!(
templates::page,
&[("first", 3), ("second", 7), ("third", 2)]
))
}
/// Handler for static files.
/// Create a response from the file data with a correct content type
/// and a far expires header (or a 404 if the file does not exist).
fn static_file(path: Path<String>) -> HttpResponse {
let name = &path.0;
if let Some(data) = StaticFile::get(name) {
let far_expires = SystemTime::now() + FAR;
HttpResponse::Ok()
.set(Expires(far_expires.into()))
.set(ContentType(data.mime.clone()))
.body(data.content)
} else {
HttpResponse::NotFound()
.reason("No such static file.")
.finish()
}
}
async fn take_int(
args: Path<usize>,
) -> Result<HttpResponse, ExampleAppError> {
let i = args.0;
Ok(HttpResponse::Ok().body(render!(
templates::page,
&[(&format!("number {}", i), 1 + i % 7)],
)))
}
async fn make_error() -> Result<HttpResponse, ExampleAppError> {
let i = "three".parse()?;
Ok(HttpResponse::Ok().body(render!(templates::page, &[("first", i)])))
}
/// The error type that can be returned from resource handlers.
///
/// This needs to be convertible from any error types used with `?` in
/// handlers, and implement the actix ResponseError type.
#[derive(Debug)]
enum ExampleAppError {
ParseInt(std::num::ParseIntError),
}
impl std::fmt::Display for ExampleAppError {
fn fmt(&self, o: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(o, "{:?}", self)
}
}
impl std::error::Error for ExampleAppError {}
impl From<std::num::ParseIntError> for ExampleAppError {
fn from(e: std::num::ParseIntError) -> Self {
ExampleAppError::ParseInt(e)
}
}
impl actix_web::error::ResponseError for ExampleAppError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR)
}
}
/// This method can be used as a "template tag", i.e. a method that
/// can be called directly from a template.
fn footer(out: &mut impl Write) -> io::Result<()> {
templates::footer(
out,
&[
("ructe", "https://crates.io/crates/ructe"),
("actix-web", "https://crates.io/crates/actix-web"),
],
)
}
fn render_404(
res: ServiceResponse<Body>,
) -> Result<ErrorHandlerResponse<Body>> {
error_response(
res,
StatusCode::NOT_FOUND,
"The resource you requested can't be found.",
)
}
fn render_500(
res: ServiceResponse<Body>,
) -> Result<ErrorHandlerResponse<Body>> {
error_response(
res,
StatusCode::INTERNAL_SERVER_ERROR,
"Sorry, Something went wrong. This is probably not your fault.",
)
}
fn error_response(
mut res: ServiceResponse<Body>,
status_code: StatusCode,
message: &str,
) -> Result<ErrorHandlerResponse<Body>> {
res.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_str(mime::TEXT_HTML_UTF_8.as_ref())
.unwrap(),
);
Ok(ErrorHandlerResponse::Response(res.map_body(
|_head, _body| {
actix_web::dev::ResponseBody::Body(
render!(templates::error, status_code, message).into(),
)
},
)))
}
/// A duration to add to current time for a far expires header.
static FAR: Duration = Duration::from_secs(180 * 24 * 60 * 60);
// And finally, include the generated code for templates and static files.
include!(concat!(env!("OUT_DIR"), "/templates.rs"));