Reorganize code
This commit is contained in:
parent
3ad384c85a
commit
b6be982295
14
src/args.rs
Normal file
14
src/args.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use clap::Parser;
|
||||
|
||||
/// Simple sea battle server
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
pub struct Args {
|
||||
/// The address this server will listen to
|
||||
#[clap(short, long, value_parser, default_value = "0.0.0.0:7000")]
|
||||
pub listen_address: String,
|
||||
|
||||
/// CORS (allowed origin) set to '*' to allow all origins
|
||||
#[clap(short, long, value_parser)]
|
||||
pub cors: Option<String>,
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
pub mod args;
|
||||
pub mod consts;
|
||||
pub mod data;
|
||||
pub mod game;
|
||||
pub mod human_player;
|
||||
pub mod human_player_ws;
|
||||
pub mod random_bot;
|
||||
pub mod server;
|
||||
|
74
src/main.rs
74
src/main.rs
@ -1,82 +1,14 @@
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web_actors::ws;
|
||||
use clap::Parser;
|
||||
use env_logger::Env;
|
||||
|
||||
use sea_battle_backend::data::{GameRules, PlayConfiguration};
|
||||
use sea_battle_backend::human_player_ws::{HumanPlayerWS, StartMode};
|
||||
|
||||
/// Simple sea battle server
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[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")
|
||||
}
|
||||
|
||||
/// Get bot configuration
|
||||
async fn game_configuration() -> impl Responder {
|
||||
HttpResponse::Ok().json(PlayConfiguration::default())
|
||||
}
|
||||
|
||||
/// 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
|
||||
}
|
||||
use sea_battle_backend::args::Args;
|
||||
use sea_battle_backend::server::start_server;
|
||||
|
||||
#[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();
|
||||
let args_clone = args.clone();
|
||||
|
||||
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 => {}
|
||||
}
|
||||
|
||||
App::new()
|
||||
.wrap(cors)
|
||||
.route("/config", web::get().to(game_configuration))
|
||||
.route("/play/bot", web::get().to(start_bot_play))
|
||||
.route("/", web::get().to(index))
|
||||
.route("{tail:.*}", web::get().to(not_found))
|
||||
})
|
||||
.bind(args.listen_address)?
|
||||
.run()
|
||||
.await
|
||||
start_server(&args).await
|
||||
}
|
||||
|
63
src/server.rs
Normal file
63
src/server.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use crate::args::Args;
|
||||
use crate::data::{GameRules, PlayConfiguration};
|
||||
use crate::human_player_ws::{HumanPlayerWS, StartMode};
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web_actors::ws;
|
||||
|
||||
/// 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 game_configuration() -> impl Responder {
|
||||
HttpResponse::Ok().json(PlayConfiguration::default())
|
||||
}
|
||||
|
||||
/// 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
|
||||
}
|
||||
|
||||
pub async fn start_server(args: &Args) -> std::io::Result<()> {
|
||||
let args_clone = args.clone();
|
||||
|
||||
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 => {}
|
||||
}
|
||||
|
||||
App::new()
|
||||
.wrap(cors)
|
||||
.route("/config", web::get().to(game_configuration))
|
||||
.route("/play/bot", web::get().to(start_bot_play))
|
||||
.route("/", web::get().to(index))
|
||||
.route("{tail:.*}", web::get().to(not_found))
|
||||
})
|
||||
.bind(&args.listen_address)?
|
||||
.run()
|
||||
.await
|
||||
}
|
Loading…
Reference in New Issue
Block a user