use actix_web::{App, HttpResponse, HttpServer, Responder, web}; use clap::Parser; use env_logger::Env; use sea_battle_backend::data::{GameRules, PlayConfiguration}; /// Simple sea battle server #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct Args { /// The address this server will listen to #[clap(short, long, value_parser, default_value = "0.0.0.0:7000")] listen_address: String, /// CORS (allowed origin) set to '*' to allow all origins #[clap(short, long, value_parser)] cors: Option, } /// The default '/' route async fn index() -> impl Responder { HttpResponse::Ok().json("Sea battle backend") } /// The default 404 route async fn not_found() -> impl Responder { HttpResponse::NotFound().json("You missed your strike lol") } /// Get bot configuration async fn bot_configuration() -> impl Responder { HttpResponse::Ok().json(PlayConfiguration::default()) } /// Multi-players configuration async fn multi_players_config() -> impl Responder { HttpResponse::Ok().json(GameRules::multi_players_rules()) } #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); let args: Args = Args::parse(); HttpServer::new(|| { App::new() .route("/bot/config", web::get().to(bot_configuration)) .route("/random/config", web::get().to(multi_players_config)) .route("/", web::get().to(index)) .route("{tail:.*}", web::get().to(not_found)) }) .bind(args.listen_address)? .run() .await }