2019-06-19 06:20:50 +02:00
|
|
|
use actix_web::{web, App};
|
2019-06-17 19:46:21 +02:00
|
|
|
// <override>
|
2019-12-28 16:26:17 +01:00
|
|
|
use actix_http::ResponseBuilder;
|
|
|
|
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
2019-06-17 19:46:21 +02:00
|
|
|
use failure::Fail;
|
|
|
|
|
|
|
|
#[derive(Fail, Debug)]
|
|
|
|
enum MyError {
|
|
|
|
#[fail(display = "internal error")]
|
|
|
|
InternalError,
|
|
|
|
#[fail(display = "bad request")]
|
|
|
|
BadClientData,
|
|
|
|
#[fail(display = "timeout")]
|
|
|
|
Timeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::ResponseError for MyError {
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
2019-12-28 16:26:17 +01:00
|
|
|
ResponseBuilder::new(self.status_code())
|
|
|
|
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
|
|
|
.body(self.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn status_code(&self) -> StatusCode {
|
2019-06-17 19:46:21 +02:00
|
|
|
match *self {
|
2019-12-28 16:26:17 +01:00
|
|
|
MyError::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
MyError::BadClientData => StatusCode::BAD_REQUEST,
|
|
|
|
MyError::Timeout => StatusCode::GATEWAY_TIMEOUT,
|
2019-06-17 19:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:26:17 +01:00
|
|
|
async fn index() -> Result<&'static str, MyError> {
|
2019-06-17 19:46:21 +02:00
|
|
|
Err(MyError::BadClientData)
|
|
|
|
}
|
|
|
|
// </override>
|
2019-06-19 06:20:50 +02:00
|
|
|
|
2019-12-28 16:26:17 +01:00
|
|
|
async fn error2() -> Result<&'static str, MyError> {
|
2019-06-19 06:20:50 +02:00
|
|
|
Err(MyError::InternalError)
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:26:17 +01:00
|
|
|
async fn error3() -> Result<&'static str, MyError> {
|
2019-06-19 06:20:50 +02:00
|
|
|
Err(MyError::Timeout)
|
|
|
|
}
|
2019-06-25 03:18:30 +02:00
|
|
|
|
2019-12-28 16:26:17 +01:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2019-06-25 03:18:30 +02:00
|
|
|
use actix_web::HttpServer;
|
|
|
|
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.route("/", web::get().to(index))
|
|
|
|
.route("/e2", web::get().to(error2))
|
|
|
|
.route("/e3", web::get().to(error3))
|
|
|
|
})
|
2019-12-28 16:26:17 +01:00
|
|
|
.bind("127.0.0.1:8088")?
|
2019-06-25 03:18:30 +02:00
|
|
|
.run()
|
2019-12-28 16:26:17 +01:00
|
|
|
.await
|
2019-06-25 03:18:30 +02:00
|
|
|
}
|