libreddit/src/main.rs

69 lines
1.9 KiB
Rust
Raw Normal View History

2020-10-25 21:25:59 +01:00
// Import Crates
2020-12-15 01:35:04 +01:00
use actix_web::{web, get, App, HttpResponse, HttpServer, middleware::NormalizePath};
2020-10-25 21:25:59 +01:00
// Reference local files
mod popular;
mod post;
2020-11-30 03:50:29 +01:00
mod proxy;
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 {
2020-11-29 22:46:53 +01:00
HttpResponse::Ok().body(include_str!("../static/robots.txt"))
2020-11-21 04:33:38 +01:00
}
2020-10-25 21:25:59 +01:00
#[get("/favicon.ico")]
async fn favicon() -> HttpResponse {
HttpResponse::Ok().body("")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2020-11-23 04:21:07 +01: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 03:25:22 +01:00
for arg in args {
if arg.starts_with("--address=") || arg.starts_with("-a=") {
let split: Vec<&str> = arg.split("=").collect();
address = split[1].to_string();
}
2020-11-23 04:21:07 +01:00
}
}
2020-10-26 04:57:19 +01:00
// start http server
2020-12-06 06:29:25 +01:00
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), address.clone());
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()
2020-12-15 01:35:04 +01:00
// TRAILING SLASH MIDDLEWARE
.wrap(NormalizePath::default())
2020-10-26 04:57:19 +01:00
// GENERAL SERVICES
2020-12-15 01:35:04 +01:00
.route("/style.css/", web::get().to(style))
.route("/favicon.ico/", web::get().to(|| HttpResponse::Ok()))
.route("/robots.txt/", web::get().to(robots))
2020-11-23 05:22:51 +01:00
// PROXY SERVICE
2020-12-15 01:35:04 +01:00
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
// USER SERVICES
.route("/u/{username}/", web::get().to(user::page))
.route("/user/{username}/", web::get().to(user::page))
2020-10-26 04:57:19 +01:00
// SUBREDDIT SERVICES
2020-12-15 01:35:04 +01:00
.route("/r/{sub}/", web::get().to(subreddit::page))
2020-10-26 04:57:19 +01:00
// POPULAR SERVICES
2020-12-15 01:35:04 +01:00
.route("/", web::get().to(popular::page))
// POST SERVICES
.route("/{id:.{5,6}}/", web::get().to(post::short))
.route("/r/{sub}/comments/{id}/{title}/", web::get().to(post::page))
2020-10-26 04:57:19 +01:00
})
2020-11-23 05:22:51 +01:00
.bind(address.clone())
.expect(format!("Cannot bind to the address: {}", address).as_str())
2020-10-26 04:57:19 +01:00
.run()
.await
}