|
use std::ops::RangeInclusive; |
|
|
|
use websurfx::server::routes; |
|
|
|
use actix_files as fs; |
|
use actix_web::{web, App, HttpServer}; |
|
use clap::{command, Parser}; |
|
use handlebars::Handlebars; |
|
|
|
#[derive(Parser, Debug, Default)] |
|
#[clap(author = "neon_arch", version, about = "Websurfx server application")] |
|
#[command(propagate_version = true)] |
|
struct CliArgs { |
|
#[clap(default_value_t = 8080, short, long,value_parser = is_port_in_range)] |
|
|
|
port: u16, |
|
} |
|
|
|
const PORT_RANGE: RangeInclusive<usize> = 1024..=65535; |
|
|
|
fn is_port_in_range(s: &str) -> Result<u16, String> { |
|
let port: usize = s |
|
.parse() |
|
.map_err(|_| format!("`{s}` is not a valid port number"))?; |
|
if PORT_RANGE.contains(&port) { |
|
Ok(port as u16) |
|
} else { |
|
Err(format!( |
|
"port not found in range {}-{}", |
|
PORT_RANGE.start(), |
|
PORT_RANGE.end() |
|
)) |
|
} |
|
} |
|
|
|
|
|
#[actix_web::main] |
|
async fn main() -> std::io::Result<()> { |
|
let args = CliArgs::parse(); |
|
|
|
println!("started server on port {}", args.port); |
|
|
|
let mut handlebars: Handlebars = Handlebars::new(); |
|
|
|
handlebars |
|
.register_templates_directory(".html", "./public/templates") |
|
.unwrap(); |
|
|
|
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars); |
|
|
|
HttpServer::new(move || { |
|
App::new() |
|
.app_data(handlebars_ref.clone()) |
|
|
|
.service(fs::Files::new("/static", "./public/static").show_files_listing()) |
|
.service(fs::Files::new("/images", "./public/images").show_files_listing()) |
|
.service(routes::robots_data) |
|
.service(routes::index) |
|
.service(routes::search) |
|
.service(routes::about) |
|
.service(routes::settings) |
|
.default_service(web::route().to(routes::not_found)) |
|
}) |
|
|
|
.bind(("127.0.0.1", args.port))? |
|
.run() |
|
.await |
|
} |
|
|