SeaBattle/src/main.rs

89 lines
2.6 KiB
Rust
Raw Normal View History

2022-09-10 13:11:12 +00:00
use actix_cors::Cors;
2022-09-11 14:48:20 +00:00
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web_actors::ws;
2022-09-10 12:37:47 +00:00
use clap::Parser;
use env_logger::Env;
2022-09-10 13:02:45 +00:00
use sea_battle_backend::data::{GameRules, PlayConfiguration};
2022-09-11 14:48:20 +00:00
use sea_battle_backend::human_player_ws::{HumanPlayerWS, StartMode};
2022-09-10 13:02:45 +00:00
2022-09-10 12:37:47 +00:00
/// Simple sea battle server
2022-09-10 13:11:12 +00:00
#[derive(Parser, Debug, Clone)]
2022-09-10 12:37:47 +00:00
#[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<String>,
}
/// 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")
}
2022-09-10 13:02:45 +00:00
/// Get bot configuration
async fn bot_configuration() -> impl Responder {
HttpResponse::Ok().json(PlayConfiguration::default())
}
2022-09-11 14:48:20 +00:00
/// Start bot game
async fn start_bot_play(
req: HttpRequest,
stream: web::Payload,
query: web::Query<GameRules>,
) -> Result<HttpResponse, actix_web::Error> {
let errors = query.0.get_errors();
if !errors.is_empty() {
return Ok(HttpResponse::BadRequest().json(errors));
}
let mut player_ws = HumanPlayerWS::default();
player_ws.start_mode = StartMode::Bot(query.0.clone());
let resp = ws::start(player_ws, &req, stream);
log::info!("New bot play with configuration: {:?}", &query.0);
resp
}
2022-09-10 13:02:45 +00:00
/// Multi-players configuration
async fn multi_players_config() -> impl Responder {
HttpResponse::Ok().json(GameRules::multi_players_rules())
}
2022-09-10 12:37:47 +00:00
#[actix_web::main]
2022-09-10 13:02:45 +00:00
async fn main() -> std::io::Result<()> {
2022-09-10 12:37:47 +00:00
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let args: Args = Args::parse();
2022-09-10 13:11:12 +00:00
let args_clone = args.clone();
2022-09-10 12:37:47 +00:00
2022-09-10 13:11:12 +00:00
HttpServer::new(move || {
let mut cors = Cors::default();
match args_clone.cors.as_deref() {
Some("*") => cors = cors.allow_any_origin(),
Some(orig) => cors = cors.allowed_origin(orig),
None => {}
}
2022-09-10 12:37:47 +00:00
App::new()
2022-09-10 13:11:12 +00:00
.wrap(cors)
2022-09-10 13:02:45 +00:00
.route("/bot/config", web::get().to(bot_configuration))
2022-09-11 14:48:20 +00:00
.route("/bot/play", web::get().to(start_bot_play))
2022-09-10 13:02:45 +00:00
.route("/random/config", web::get().to(multi_players_config))
2022-09-10 12:37:47 +00:00
.route("/", web::get().to(index))
.route("{tail:.*}", web::get().to(not_found))
})
2022-09-11 14:48:20 +00:00
.bind(args.listen_address)?
.run()
.await
2022-09-10 12:15:35 +00:00
}