2020-10-25 13:25:59 -07:00
|
|
|
// Import Crates
|
2021-01-12 20:18:20 -08:00
|
|
|
use actix_web::{middleware, web, App, HttpResponse, HttpServer}; // dev::Service
|
2020-10-25 13:25:59 -07:00
|
|
|
|
|
|
|
// Reference local files
|
|
|
|
mod post;
|
2020-11-29 18:50:29 -08:00
|
|
|
mod proxy;
|
2020-12-31 15:54:13 -08:00
|
|
|
mod search;
|
2021-01-05 18:04:49 -08:00
|
|
|
mod settings;
|
2020-10-25 13:25:59 -07:00
|
|
|
mod subreddit;
|
2020-10-25 20:57:19 -07:00
|
|
|
mod user;
|
2020-11-25 13:53:30 -08:00
|
|
|
mod utils;
|
2020-10-25 13:25:59 -07:00
|
|
|
|
|
|
|
// Create Services
|
2020-11-18 16:31:46 -08:00
|
|
|
async fn style() -> HttpResponse {
|
2020-11-29 13:46:53 -08:00
|
|
|
HttpResponse::Ok().content_type("text/css").body(include_str!("../static/style.css"))
|
2020-10-25 13:25:59 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:33:38 -08:00
|
|
|
async fn robots() -> HttpResponse {
|
2021-01-06 14:19:10 -08:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
|
|
|
.body(include_str!("../static/robots.txt"))
|
2020-11-20 19:33:38 -08:00
|
|
|
}
|
|
|
|
|
2020-10-25 13:25:59 -07:00
|
|
|
async fn favicon() -> HttpResponse {
|
2021-01-12 20:18:20 -08:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
|
|
|
.body(include_bytes!("../static/favicon.ico").as_ref())
|
2020-10-25 13:25:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2020-11-22 19:21:07 -08:00
|
|
|
let mut address = "0.0.0.0:8080".to_string();
|
2021-01-10 13:08:36 -08:00
|
|
|
// let mut https = false;
|
2020-11-22 19:21:07 -08:00
|
|
|
|
2021-01-10 13:08:36 -08: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-22 19:21:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 20:57:19 -07:00
|
|
|
// start http server
|
2021-01-01 12:55:09 -08:00
|
|
|
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), &address);
|
2020-11-18 18:50:59 -08:00
|
|
|
|
2020-10-25 13:25:59 -07:00
|
|
|
HttpServer::new(|| {
|
2020-10-25 20:57:19 -07:00
|
|
|
App::new()
|
2021-01-10 13:08:36 -08:00
|
|
|
// REDIRECT TO HTTPS
|
|
|
|
// .wrap(middleware::DefaultHeaders::new().header("Strict-Transport-Security", "max-age=31536000"))
|
|
|
|
// .wrap_fn(|req, srv| {
|
|
|
|
// let fut = srv.call(req);
|
|
|
|
// async {
|
|
|
|
// let mut res = fut.await?;
|
|
|
|
// if https {
|
|
|
|
// res.headers_mut().insert(
|
|
|
|
// actix_web::http::header::STRICT_TRANSPORT_SECURITY, actix_web::http::HeaderValue::from_static("max-age=31536000;"),
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// Ok(res)
|
|
|
|
// }
|
|
|
|
// })
|
2020-12-14 16:35:04 -08:00
|
|
|
// TRAILING SLASH MIDDLEWARE
|
2021-01-10 13:20:47 -08:00
|
|
|
.wrap(middleware::NormalizePath::default())
|
2020-12-31 21:03:44 -08:00
|
|
|
// DEFAULT SERVICE
|
2021-01-08 21:11:20 -08:00
|
|
|
.default_service(web::get().to(|| utils::error("Nothing here".to_string())))
|
2020-10-25 20:57:19 -07:00
|
|
|
// GENERAL SERVICES
|
2020-12-14 16:35:04 -08:00
|
|
|
.route("/style.css/", web::get().to(style))
|
2021-01-12 20:18:20 -08:00
|
|
|
.route("/favicon.ico/", web::get().to(favicon))
|
2020-12-14 16:35:04 -08:00
|
|
|
.route("/robots.txt/", web::get().to(robots))
|
2021-01-02 20:50:23 -08:00
|
|
|
// SETTINGS SERVICE
|
2021-01-05 18:04:49 -08:00
|
|
|
.route("/settings/", web::get().to(settings::get))
|
|
|
|
.route("/settings/", web::post().to(settings::set))
|
2020-11-22 20:22:51 -08:00
|
|
|
// PROXY SERVICE
|
2020-12-14 16:35:04 -08:00
|
|
|
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
|
2020-12-31 15:54:13 -08:00
|
|
|
// SEARCH SERVICES
|
2020-12-31 21:03:44 -08:00
|
|
|
.route("/search/", web::get().to(search::find))
|
|
|
|
.route("r/{sub}/search/", web::get().to(search::find))
|
2020-12-14 16:35:04 -08:00
|
|
|
// USER SERVICES
|
2020-12-31 15:54:13 -08:00
|
|
|
.route("/u/{username}/", web::get().to(user::profile))
|
|
|
|
.route("/user/{username}/", web::get().to(user::profile))
|
2021-01-01 22:21:43 -08:00
|
|
|
// WIKI SERVICES
|
2021-01-02 10:58:21 -08:00
|
|
|
.route("/wiki/", web::get().to(subreddit::wiki))
|
|
|
|
.route("/wiki/{page}/", web::get().to(subreddit::wiki))
|
2021-01-01 22:21:43 -08:00
|
|
|
.route("/r/{sub}/wiki/", web::get().to(subreddit::wiki))
|
|
|
|
.route("/r/{sub}/wiki/{page}/", web::get().to(subreddit::wiki))
|
2020-10-25 20:57:19 -07:00
|
|
|
// SUBREDDIT SERVICES
|
2020-12-14 16:35:04 -08:00
|
|
|
.route("/r/{sub}/", web::get().to(subreddit::page))
|
2021-01-03 21:00:36 -08:00
|
|
|
.route("/r/{sub}/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
|
2020-10-25 20:57:19 -07:00
|
|
|
// POPULAR SERVICES
|
2020-12-31 15:54:13 -08:00
|
|
|
.route("/", web::get().to(subreddit::page))
|
2021-01-03 21:00:36 -08:00
|
|
|
.route("/{sort:best|hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
|
2020-12-14 16:35:04 -08:00
|
|
|
// POST SERVICES
|
2020-12-31 15:54:13 -08:00
|
|
|
.route("/{id:.{5,6}}/", web::get().to(post::item))
|
|
|
|
.route("/r/{sub}/comments/{id}/{title}/", web::get().to(post::item))
|
|
|
|
.route("/r/{sub}/comments/{id}/{title}/{comment_id}/", web::get().to(post::item))
|
2020-10-25 20:57:19 -07:00
|
|
|
})
|
2021-01-01 12:55:09 -08:00
|
|
|
.bind(&address)
|
2021-01-10 13:08:36 -08:00
|
|
|
.unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e))
|
2020-10-25 20:57:19 -07:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|