Add basic server
This commit is contained in:
parent
87089e8be8
commit
481bfe14f4
1292
Cargo.lock
generated
1292
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -6,3 +6,8 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "3.2.17", features = ["derive"] }
|
||||||
|
log = "0.4.17"
|
||||||
|
env_logger = "0.9.0"
|
||||||
|
actix-web = "4.1.0"
|
||||||
|
actix-cors = "0.6.2"
|
45
src/main.rs
45
src/main.rs
@ -1,3 +1,44 @@
|
|||||||
fn main() {
|
use actix_web::{App, HttpResponse, HttpServer, Responder, web};
|
||||||
println!("Hello, world!");
|
use clap::Parser;
|
||||||
|
use env_logger::Env;
|
||||||
|
|
||||||
|
/// 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[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("/", 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