use poem::{listener::TcpListener, Route, Server};
use poem::endpoint::StaticFilesEndpoint;
use poem_openapi::{param::Query, OpenApi, OpenApiService, payload::Json};
use quilt_rs::Client;
use serde_json::Value;
mod quilt;
use quilt::quilt_uri;
struct Api;
#[OpenApi]
impl Api {
fn extract_uri(
scheme: Query<Option<String>>,
registry: Query<Option<String>>,
package: Query<Option<String>>
) -> String {
let s = scheme.0.unwrap_or("s3".to_string());
let r = registry.0.unwrap_or("quilt-example".to_string());
let p = package.0.unwrap_or("akarve/test_dest".to_string());
quilt_uri(s, r, p)
}
#[oai(path = "/uri", method = "get")]
async fn uri(
&self,
scheme: Query<Option<String>>,
registry: Query<Option<String>>,
package: Query<Option<String>>
) -> Json<String> {
let uri = Api::extract_uri(scheme, registry, package);
Json(uri)
}
#[oai(path = "/manifest", method = "get")]
async fn manifest(
&self,
scheme: Query<Option<String>>,
registry: Query<Option<String>>,
package: Query<Option<String>>
) -> Json<Value> {
let uri = Api::extract_uri(scheme, registry, package);
println!("manifest.uri: {}", uri);
let client = Client::new().await;
let manifest_wrap = client.manifest3_from_uri(uri).await;
let manifest = manifest_wrap.unwrap();
println!("manifest: {:?}", manifest);
Json(serde_json::to_value(manifest).unwrap())
}
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let asset_path = std::env::current_dir().unwrap().join("assets");
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "poem=debug");
}
tracing_subscriber::fmt::init();
let api_service =
OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000/api");
let ui = api_service.swagger_ui();
let explorer = api_service.openapi_explorer();
let rapidoc = api_service.rapidoc();
let redoc = api_service.redoc();
let index = StaticFilesEndpoint::new(asset_path)
.show_files_listing()
.index_file("index.html");
Server::new(TcpListener::bind("0.0.0.0:3000")).run(
Route::new()
.nest("/", index)
.nest("/api", api_service)
.nest("/ui", ui)
.nest("/rapid", rapidoc)
.nest("/doc", redoc)
.nest("/explore", explorer)
).await
}