2020-10-26 09:25:59 +13:00
|
|
|
// Import Crates
|
2020-12-20 16:54:46 +13:00
|
|
|
use actix_web::{get, middleware::NormalizePath, web, App, HttpResponse, HttpServer};
|
2020-10-26 09:25:59 +13:00
|
|
|
|
|
|
|
// Reference local files
|
|
|
|
mod post;
|
2020-11-30 15:50:29 +13:00
|
|
|
mod proxy;
|
2021-01-01 12:54:13 +13:00
|
|
|
mod search;
|
2020-10-26 09:25:59 +13:00
|
|
|
mod subreddit;
|
2020-10-26 16:57:19 +13:00
|
|
|
mod user;
|
2020-11-26 10:53:30 +13:00
|
|
|
mod utils;
|
2020-10-26 09:25:59 +13:00
|
|
|
|
|
|
|
// Create Services
|
2020-11-19 13:31:46 +13:00
|
|
|
async fn style() -> HttpResponse {
|
2020-11-30 10:46:53 +13:00
|
|
|
HttpResponse::Ok().content_type("text/css").body(include_str!("../static/style.css"))
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2020-11-21 16:33:38 +13:00
|
|
|
async fn robots() -> HttpResponse {
|
2020-11-30 10:46:53 +13:00
|
|
|
HttpResponse::Ok().body(include_str!("../static/robots.txt"))
|
2020-11-21 16:33:38 +13:00
|
|
|
}
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
#[get("/favicon.ico")]
|
|
|
|
async fn favicon() -> HttpResponse {
|
|
|
|
HttpResponse::Ok().body("")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2020-11-23 16:21:07 +13:00
|
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
let mut address = "0.0.0.0:8080".to_string();
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
2020-11-24 15:25:22 +13:00
|
|
|
for arg in args {
|
|
|
|
if arg.starts_with("--address=") || arg.starts_with("-a=") {
|
2021-01-02 09:33:57 +13:00
|
|
|
let split: Vec<&str> = arg.split('=').collect();
|
2020-11-24 15:25:22 +13:00
|
|
|
address = split[1].to_string();
|
|
|
|
}
|
2020-11-23 16:21:07 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:57:19 +13:00
|
|
|
// start http server
|
2020-12-06 18:29:25 +13:00
|
|
|
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), address.clone());
|
2020-11-19 15:50:59 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
HttpServer::new(|| {
|
2020-10-26 16:57:19 +13:00
|
|
|
App::new()
|
2020-12-15 13:35:04 +13:00
|
|
|
// TRAILING SLASH MIDDLEWARE
|
|
|
|
.wrap(NormalizePath::default())
|
2021-01-01 18:03:44 +13:00
|
|
|
// DEFAULT SERVICE
|
|
|
|
.default_service(web::get().to(utils::error))
|
2020-10-26 16:57:19 +13:00
|
|
|
// GENERAL SERVICES
|
2020-12-15 13:35:04 +13:00
|
|
|
.route("/style.css/", web::get().to(style))
|
2021-01-02 09:33:57 +13:00
|
|
|
.route("/favicon.ico/", web::get().to(HttpResponse::Ok))
|
2020-12-15 13:35:04 +13:00
|
|
|
.route("/robots.txt/", web::get().to(robots))
|
2020-11-23 17:22:51 +13:00
|
|
|
// PROXY SERVICE
|
2020-12-15 13:35:04 +13:00
|
|
|
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
|
2021-01-01 12:54:13 +13:00
|
|
|
// SEARCH SERVICES
|
2021-01-01 18:03:44 +13:00
|
|
|
.route("/search/", web::get().to(search::find))
|
|
|
|
.route("r/{sub}/search/", web::get().to(search::find))
|
2020-12-15 13:35:04 +13:00
|
|
|
// USER SERVICES
|
2021-01-01 12:54:13 +13:00
|
|
|
.route("/u/{username}/", web::get().to(user::profile))
|
|
|
|
.route("/user/{username}/", web::get().to(user::profile))
|
2020-10-26 16:57:19 +13:00
|
|
|
// SUBREDDIT SERVICES
|
2020-12-15 13:35:04 +13:00
|
|
|
.route("/r/{sub}/", web::get().to(subreddit::page))
|
2021-01-01 12:54:13 +13:00
|
|
|
.route("/r/{sub}/{sort}/", web::get().to(subreddit::page))
|
2021-01-01 18:03:44 +13:00
|
|
|
// WIKI SERVICES
|
|
|
|
// .route("/r/{sub}/wiki/index", web::get().to(subreddit::wiki))
|
2020-10-26 16:57:19 +13:00
|
|
|
// POPULAR SERVICES
|
2021-01-01 12:54:13 +13:00
|
|
|
.route("/", web::get().to(subreddit::page))
|
|
|
|
.route("/{sort:best|hot|new|top|rising}/", web::get().to(subreddit::page))
|
2020-12-15 13:35:04 +13:00
|
|
|
// POST SERVICES
|
2021-01-01 12:54:13 +13: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-26 16:57:19 +13:00
|
|
|
})
|
2020-11-23 17:22:51 +13:00
|
|
|
.bind(address.clone())
|
2021-01-02 09:33:57 +13:00
|
|
|
.unwrap_or_else(|_| panic!("Cannot bind to the address: {}", address))
|
2020-10-26 16:57:19 +13:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|