2022-09-10 12:37:47 +00:00
|
|
|
use actix_web::{App, HttpResponse, HttpServer, Responder, web};
|
|
|
|
use clap::Parser;
|
|
|
|
use env_logger::Env;
|
|
|
|
|
2022-09-10 13:02:45 +00:00
|
|
|
use sea_battle_backend::data::{GameRules, PlayConfiguration};
|
|
|
|
|
2022-09-10 12:37:47 +00:00
|
|
|
/// 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<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())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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();
|
|
|
|
|
|
|
|
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
2022-09-10 13:02:45 +00:00
|
|
|
.route("/bot/config", web::get().to(bot_configuration))
|
|
|
|
|
|
|
|
.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))
|
|
|
|
})
|
|
|
|
.bind(args.listen_address)?
|
|
|
|
.run()
|
|
|
|
.await
|
2022-09-10 12:15:35 +00:00
|
|
|
}
|