diff --git a/Cargo.toml b/Cargo.toml index 56d38d8..f1c4d7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,4 @@ actix-files = "0.1.0-alpha.6" actix = "0.8.1" diesel_migrations = "1.4.0" futures = "0.1.26" +http = "0.1.17" diff --git a/src/routers/article.rs b/src/routers/article.rs index 25545b8..81f6a9f 100644 --- a/src/routers/article.rs +++ b/src/routers/article.rs @@ -43,6 +43,10 @@ pub fn single_article( } let article1 = article.unwrap(); + if let Some(to) = article1.url { + return RubbleResponder::Redirect(format!("/{}", to)); + } + let view = ArticleView::from(&article1); let settings = Setting::load(&connection); diff --git a/src/routers/mod.rs b/src/routers/mod.rs index 7d2bb69..49076fd 100644 --- a/src/routers/mod.rs +++ b/src/routers/mod.rs @@ -3,6 +3,7 @@ use actix_web::{HttpRequest, HttpResponse, Responder}; pub mod article; use actix_web::error::Error; use futures::future::{err, ok, FutureResult}; + //pub mod admin; //pub mod rss; //pub mod catacher; @@ -12,6 +13,7 @@ pub enum RubbleResponder { Html(String), Redirect(String), NotFound, + RedirectPermanently(String), } impl Responder for RubbleResponder { @@ -23,10 +25,13 @@ impl Responder for RubbleResponder { RubbleResponder::Html(content) => ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") .body(content)), - RubbleResponder::Redirect(content) => ok(HttpResponse::Ok() - .content_type("text/html") - .body("redirect")), + RubbleResponder::Redirect(to) => ok(HttpResponse::Found() + .header(http::header::LOCATION, to) + .finish()), RubbleResponder::NotFound => ok(HttpResponse::NotFound().finish()), + RubbleResponder::RedirectPermanently(to) => ok(HttpResponse::MovedPermanently() + .header(http::header::LOCATION, to) + .finish()), } } }