libreddit/src/main.rs

212 lines
6.3 KiB
Rust
Raw Normal View History

2020-10-25 21:25:59 +01:00
// Reference local files
mod post;
2020-11-30 03:50:29 +01:00
mod proxy;
2021-01-01 00:54:13 +01:00
mod search;
2021-01-06 03:04:49 +01:00
mod settings;
2020-10-25 21:25:59 +01:00
mod subreddit;
2020-10-26 04:57:19 +01:00
mod user;
2020-11-25 22:53:30 +01:00
mod utils;
2020-10-25 21:25:59 +01:00
2021-02-14 00:02:38 +01:00
// Import Crates
use tide::{
utils::{async_trait, After},
Middleware, Next, Request, Response,
};
use utils::{error, redirect};
// Build middleware
struct HttpsRedirect<HttpsOnly>(HttpsOnly);
struct NormalizePath;
#[async_trait]
impl<State, HttpsOnly> Middleware<State> for HttpsRedirect<HttpsOnly>
where
State: Clone + Send + Sync + 'static,
HttpsOnly: Into<bool> + Copy + Send + Sync + 'static,
{
async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> tide::Result {
let secure = request.url().scheme() == "https";
if self.0.into() && !secure {
let mut secured = request.url().to_owned();
secured.set_scheme("https").unwrap_or_default();
2021-02-14 00:02:38 +01:00
Ok(redirect(secured.to_string()))
} else {
Ok(next.run(request).await)
}
}
}
#[async_trait]
impl<State: Clone + Send + Sync + 'static> Middleware<State> for NormalizePath {
async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> tide::Result {
if !request.url().path().ends_with('/') {
Ok(Response::builder(301).header("Location", format!("{}/", request.url().path())).build())
} else {
Ok(next.run(request).await)
}
}
}
2020-10-25 21:25:59 +01:00
// Create Services
async fn style(_req: Request<()>) -> tide::Result {
2021-02-14 00:02:38 +01:00
Ok(Response::builder(200).content_type("text/css").body(include_str!("../static/style.css")).build())
2020-10-25 21:25:59 +01:00
}
// Required for creating a PWA
async fn manifest(_req: Request<()>) -> tide::Result {
Ok(
Response::builder(200)
.content_type("application/json")
.body(include_str!("../static/manifest.json"))
.build(),
)
}
// Required for the manifest to be valid
async fn pwa_logo(_req: Request<()>) -> tide::Result {
2021-02-14 00:02:38 +01:00
Ok(Response::builder(200).content_type("image/png").body(include_bytes!("../static/logo.png").as_ref()).build())
}
// Required for iOS App Icons
async fn iphone_logo(_req: Request<()>) -> tide::Result {
Ok(
Response::builder(200)
.content_type("image/png")
.body(include_bytes!("../static/touch-icon-iphone.png").as_ref())
.build(),
)
}
async fn robots(_req: Request<()>) -> tide::Result {
Ok(
Response::builder(200)
.content_type("text/plain")
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.body("User-agent: *\nAllow: /")
.build(),
)
2020-11-21 04:33:38 +01:00
}
async fn favicon(_req: Request<()>) -> tide::Result {
Ok(
Response::builder(200)
.content_type("image/vnd.microsoft.icon")
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.body(include_bytes!("../static/favicon.ico").as_ref())
.build(),
)
2020-10-25 21:25:59 +01:00
}
#[async_std::main]
async fn main() -> tide::Result<()> {
2020-11-23 04:21:07 +01:00
let mut address = "0.0.0.0:8080".to_string();
2021-01-17 07:04:03 +01:00
let mut force_https = false;
2020-11-23 04:21:07 +01:00
2021-01-10 22:08:36 +01:00
for arg in std::env::args().collect::<Vec<String>>() {
match arg.split('=').collect::<Vec<&str>>()[0] {
"--address" | "-a" => address = arg.split('=').collect::<Vec<&str>>()[1].to_string(),
2021-01-17 07:04:03 +01:00
"--redirect-https" | "-r" => force_https = true,
_ => (),
2020-11-23 04:21:07 +01:00
}
}
// Start HTTP server
2021-01-01 21:55:09 +01:00
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), &address);
2020-11-19 03:50:59 +01:00
let mut app = tide::new();
// Redirect to HTTPS if "--redirect-https" enabled
app.with(HttpsRedirect(force_https));
// Append trailing slash and remove double slashes
app.with(NormalizePath);
// Apply default headers for security
app.with(After(|mut res: Response| async move {
res.insert_header("Referrer-Policy", "no-referrer");
res.insert_header("X-Content-Type-Options", "nosniff");
res.insert_header("X-Frame-Options", "DENY");
res.insert_header(
"Content-Security-Policy",
"default-src 'none'; manifest-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; base-uri 'none'; img-src 'self' data:; form-action 'self'; frame-ancestors 'none';",
);
Ok(res)
}));
// Read static files
app.at("/style.css/").get(style);
app.at("/favicon.ico/").get(favicon);
app.at("/robots.txt/").get(robots);
app.at("/manifest.json/").get(manifest);
app.at("/logo.png/").get(pwa_logo);
app.at("/touch-icon-iphone.png/").get(iphone_logo);
2021-02-09 21:08:38 +01:00
app.at("/apple-touch-icon.png/").get(iphone_logo);
// Proxy media through Libreddit
app.at("/proxy/*url/").get(proxy::handler);
// Browse user profile
app.at("/u/:name/").get(user::profile);
app.at("/u/:name/comments/:id/:title/").get(post::item);
app.at("/u/:name/comments/:id/:title/:comment_id/").get(post::item);
app.at("/user/:name/").get(user::profile);
2021-02-09 19:11:39 +01:00
app.at("/user/:name/comments/:id/").get(post::item);
app.at("/user/:name/comments/:id/:title/").get(post::item);
app.at("/user/:name/comments/:id/:title/:comment_id/").get(post::item);
// Configure settings
app.at("/settings/").get(settings::get).post(settings::set);
app.at("/settings/restore/").get(settings::restore);
// Subreddit services
// See posts and info about subreddit
app.at("/r/:sub/").get(subreddit::page);
// Handle subscribe/unsubscribe
app.at("/r/:sub/subscribe/").post(subreddit::subscriptions);
app.at("/r/:sub/unsubscribe/").post(subreddit::subscriptions);
// View post on subreddit
2021-02-09 19:11:39 +01:00
app.at("/r/:sub/comments/:id/").get(post::item);
app.at("/r/:sub/comments/:id/:title/").get(post::item);
app.at("/r/:sub/comments/:id/:title/:comment_id/").get(post::item);
// Search inside subreddit
app.at("/r/:sub/search/").get(search::find);
// View wiki of subreddit
app.at("/r/:sub/w/").get(subreddit::wiki);
app.at("/r/:sub/w/:page/").get(subreddit::wiki);
app.at("/r/:sub/wiki/").get(subreddit::wiki);
app.at("/r/:sub/wiki/:page/").get(subreddit::wiki);
// Sort subreddit posts
app.at("/r/:sub/:sort/").get(subreddit::page);
// Front page
app.at("/").get(subreddit::page);
// View Reddit wiki
app.at("/w/").get(subreddit::wiki);
app.at("/w/:page/").get(subreddit::wiki);
app.at("/wiki/").get(subreddit::wiki);
app.at("/wiki/:page/").get(subreddit::wiki);
// Search all of Reddit
app.at("/search/").get(search::find);
app.at("/:id/").get(|req: Request<()>| async {
2021-02-09 21:08:38 +01:00
match req.param("id") {
// Sort front page
Ok("best") | Ok("hot") | Ok("new") | Ok("top") | Ok("rising") | Ok("controversial") => subreddit::page(req).await,
// Short link for post
Ok(id) if id.len() > 4 && id.len() < 7 => post::item(req).await,
2021-02-14 00:02:38 +01:00
_ => error("Nothing here".to_string()).await,
}
});
// Default service in case no routes match
2021-02-14 00:02:38 +01:00
app.at("*").get(|_| error("Nothing here".to_string()));
2021-02-09 18:54:13 +01:00
app.listen(address).await?;
Ok(())
2020-10-26 04:57:19 +01:00
}