Serve base files

This commit is contained in:
2022-04-15 11:30:02 +02:00
parent f03eee9fc2
commit f902fa14c5
4 changed files with 2212 additions and 2 deletions

View File

@ -1,3 +1,46 @@
fn main() {
println!("Hello, world!");
use actix_web::{App, HttpResponse, HttpServer, Responder, web};
use clap::Parser;
/// Simple swagger UI server - serves OpenAPI file
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Path to schema to serve
#[clap(short, long)]
schema: String,
/// Listen address
#[clap(short, long, default_value = "0.0.0.0:8000")]
listen_address: String,
}
#[derive(serde::Deserialize)]
struct HandlerPath {
filename: Option<String>,
}
async fn handler(path: web::Path<HandlerPath>) -> impl Responder {
let filename = path.filename.as_deref().unwrap_or("index.html");
match swagger_ui::Assets::get(filename) {
Some(f) => HttpResponse::Ok().body(f.to_vec()),
None => HttpResponse::NotFound().body("404 not found"),
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let args: Args = Args::parse();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("Will listen on http://{}/", args.listen_address);
HttpServer::new(|| {
App::new()
.route("/", web::get().to(handler))
.route("/{filename}", web::get().to(handler))
})
.bind(args.listen_address)?
.run()
.await
}