libreddit/src/main.rs

116 lines
3.7 KiB
Rust
Raw Normal View History

2020-10-25 21:25:59 +01:00
// Import Crates
2021-01-13 05:18:20 +01:00
use actix_web::{middleware, web, App, HttpResponse, HttpServer}; // dev::Service
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
// Create Services
2020-11-19 01:31:46 +01:00
async fn style() -> HttpResponse {
2020-11-29 22:46:53 +01:00
HttpResponse::Ok().content_type("text/css").body(include_str!("../static/style.css"))
2020-10-25 21:25:59 +01:00
}
2020-11-21 04:33:38 +01:00
async fn robots() -> HttpResponse {
2021-01-06 23:19:10 +01:00
HttpResponse::Ok()
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.body("User-agent: *\nAllow: /")
2020-11-21 04:33:38 +01:00
}
2020-10-25 21:25:59 +01:00
async fn favicon() -> HttpResponse {
2021-01-13 05:18:20 +01:00
HttpResponse::Ok()
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.body(include_bytes!("../static/favicon.ico").as_ref())
2020-10-25 21:25:59 +01:00
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2020-11-23 04:21:07 +01:00
let mut address = "0.0.0.0:8080".to_string();
2021-01-10 22:08:36 +01:00
// let mut 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(),
// "--redirect-https" | "-r" => https = true,
_ => (),
2020-11-23 04:21:07 +01:00
}
}
2020-10-26 04:57:19 +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
2020-10-25 21:25:59 +01:00
HttpServer::new(|| {
2020-10-26 04:57:19 +01:00
App::new()
// Redirect to HTTPS
// .wrap_fn(|req, srv| { let fut = srv.call(req); async { let mut res = fut.await?; if https {} Ok(res) } })
// Append trailing slash and remove double slashes
2021-01-10 22:20:47 +01:00
.wrap(middleware::NormalizePath::default())
// Default service in case no routes match
2021-01-14 18:53:54 +01:00
.default_service(web::get().to(|| utils::error("Nothing here".to_string())))
// Read static files
2020-12-15 01:35:04 +01:00
.route("/style.css/", web::get().to(style))
2021-01-13 05:18:20 +01:00
.route("/favicon.ico/", web::get().to(favicon))
2020-12-15 01:35:04 +01:00
.route("/robots.txt/", web::get().to(robots))
// Proxy media through Libreddit
2020-12-15 01:35:04 +01:00
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
// Browse user profile
2021-01-14 19:57:50 +01:00
.service(
web::scope("/{scope:user|u}").service(
web::scope("/{username}").route("/", web::get().to(user::profile)).service(
web::scope("/comments/{id}/{title}")
.route("/", web::get().to(post::item))
.route("/{comment_id}/", web::get().to(post::item)),
),
),
)
// Configure settings
.service(web::resource("/settings/").route(web::get().to(settings::get)).route(web::post().to(settings::set)))
// Subreddit services
.service(
web::scope("/r/{sub}")
// See posts and info about subreddit
.route("/", web::get().to(subreddit::page))
.route("/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
// View post on subreddit
.service(
web::scope("/comments/{id}/{title}")
.route("/", web::get().to(post::item))
.route("/{comment_id}/", web::get().to(post::item)),
)
// Search inside subreddit
.route("/search/", web::get().to(search::find))
// View wiki of subreddit
.service(
web::scope("/wiki")
.route("/", web::get().to(subreddit::wiki))
.route("/{page}/", web::get().to(subreddit::wiki)),
),
)
// Universal services
.service(
web::scope("")
// Front page
.route("/", web::get().to(subreddit::page))
.route("/{sort:best|hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
// View Reddit wiki
.service(
web::scope("/wiki")
.route("/", web::get().to(subreddit::wiki))
.route("/{page}/", web::get().to(subreddit::wiki)),
)
// Search all of Reddit
2021-01-14 18:53:54 +01:00
.route("/search/", web::get().to(search::find))
// Short link for post
.route("/{id:.{5,6}}/", web::get().to(post::item)),
)
2020-10-26 04:57:19 +01:00
})
2021-01-01 21:55:09 +01:00
.bind(&address)
2021-01-10 22:08:36 +01:00
.unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e))
2020-10-26 04:57:19 +01:00
.run()
.await
}