feat: rss supported

This commit is contained in:
Kilerd Chan 2018-12-06 16:02:49 +08:00
parent 0612c1adb9
commit 7b80f0eb34
4 changed files with 47 additions and 4 deletions

View File

@ -23,4 +23,5 @@ rust-crypto = "^0.2"
juniper = "0.10"
juniper_codegen = "0.10"
juniper_rocket = "0.1.3"
rand = "0.6.0"
rand = "0.6.0"
rss = "1.6.1"

View File

@ -39,7 +39,7 @@ use std::collections::HashMap;
use crate::graphql::{Schema, Query, Mutation};
fn main() {
use crate::routers::{admin, article, catacher, graphql};
use crate::routers::{admin, article, catacher, graphql, rss};
dotenv().ok();
let database_url = std::env::var("DATABASE_URL").expect("database_url must be set");
@ -56,6 +56,8 @@ fn main() {
article::get_article_by_url,
article::static_content,
rss::rss,
graphql::graphql_authorization,
graphql::graphiql,
graphql::get_graphql_handler,

View File

@ -1,7 +1,5 @@
pub mod article;
pub mod admin;
pub mod rss;
pub mod catacher;
pub mod graphql;

View File

@ -0,0 +1,42 @@
use rss::ChannelBuilder;
use rss::Channel;
use rss::{Item, ItemBuilder};
use rocket::response::content;
use crate::guard::SettingMap;
use crate::pg_pool::DbConn;
use crate::models::Article;
use crate::response::ArticleResponse;
use std::collections::HashMap;
#[get("/rss")]
pub fn rss(setting: SettingMap, conn: DbConn) -> content::Xml<String> {
let result = Article::load_all(false, &conn);
let article_responses: Vec<ArticleResponse> = result.iter().map(ArticleResponse::from).collect();
let items: Vec<Item> = article_responses.into_iter().map(|item| {
ItemBuilder::default()
.title(item.article.title.clone())
.link(item.article.url.clone())
.description(item.description)
.content(item.markdown_content)
.pub_date(item.article.publish_at.to_string())
.build()
.unwrap()
}).collect();
let mut namespaces: HashMap<String, String> = HashMap::new();
namespaces.insert("dc".to_string(), "http://purl.org/dc/elements/1.1/".to_string());
namespaces.insert("content".to_string(), "http://purl.org/rss/1.0/modules/content/".to_string());
namespaces.insert("atom".to_string(), "http://www.w3.org/2005/Atom".to_string());
namespaces.insert("media".to_string(), "http://search.yahoo.com/mrss/".to_string());
let channel: Channel = ChannelBuilder::default()
.title(setting.title)
.description(setting.description)
.generator("Rubble".to_string())
.items(items)
.namespaces(namespaces)
.build()
.unwrap();
content::Xml(channel.to_string())
}