redsunlib/src/main.rs

70 lines
1.6 KiB
Rust
Raw Normal View History

2020-10-26 09:25:59 +13:00
// Import Crates
2020-11-19 13:31:46 +13:00
use actix_web::{get, App, HttpResponse, HttpServer};
2020-10-26 09:25:59 +13:00
// Reference local files
mod popular;
mod post;
mod subreddit;
2020-10-26 16:57:19 +13:00
mod user;
2020-11-23 16:21:07 +13:00
mod proxy;
2020-11-26 10:53:30 +13:00
mod utils;
2020-10-26 09:25:59 +13:00
// Create Services
#[get("/style.css")]
2020-11-19 13:31:46 +13:00
async fn style() -> HttpResponse {
2020-11-23 16:21:07 +13:00
let file = std::fs::read_to_string("static/style.css").expect("ERROR: Could not read style.css");
HttpResponse::Ok().content_type("text/css").body(file)
2020-10-26 09:25:59 +13:00
}
2020-11-21 16:33:38 +13:00
#[get("/robots.txt")]
async fn robots() -> HttpResponse {
2020-11-23 16:21:07 +13:00
let file = std::fs::read_to_string("static/robots.txt").expect("ERROR: Could not read robots.txt");
2020-11-21 16:33:38 +13:00
HttpResponse::Ok().body(file)
}
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=") {
let split: Vec<&str> = arg.split("=").collect();
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-11-23 17:22:51 +13:00
println!("Running Libreddit on {}!", 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()
// GENERAL SERVICES
.service(style)
.service(favicon)
2020-11-21 16:33:38 +13:00
.service(robots)
2020-11-23 17:22:51 +13:00
// PROXY SERVICE
.service(proxy::handler)
2020-10-26 16:57:19 +13:00
// POST SERVICES
.service(post::short)
.service(post::page)
// SUBREDDIT SERVICES
.service(subreddit::page)
// POPULAR SERVICES
.service(popular::page)
// USER SERVICES
.service(user::page)
})
2020-11-23 17:22:51 +13:00
.bind(address.clone())
.expect(format!("Cannot bind to the address: {}", address).as_str())
2020-10-26 16:57:19 +13:00
.run()
.await
}