diff --git a/src/cdn.rs b/src/cdn.rs index c602a22..c707e41 100644 --- a/src/cdn.rs +++ b/src/cdn.rs @@ -117,8 +117,6 @@ async fn cdn_handler( // #[get("/cdn/{slug}@{version}/{type}")] #[handler] pub async fn cdn_get( - // db: web::Data, - // path: web::Path<(String, String, CdnType)>, Path((slug, version, dl_type)): Path<(String, String, CdnType)>, ) -> impl IntoResponse { let db = match DB_POOL.get() { diff --git a/src/main.rs b/src/main.rs index dc72fad..071f892 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,8 @@ use async_graphql::{ }; use async_graphql_poem::GraphQL; use poem::{ - get, handler, http::StatusCode, listener::TcpListener, post, EndpointExt, IntoResponse, - Response, Route, + get, handler, http::StatusCode, listener::TcpListener, middleware::CorsEndpoint, post, + EndpointExt, IntoResponse, Response, Route, }; use rand::Rng; use search::MeiliMigrator; @@ -36,15 +36,6 @@ mod debug; use crate::schema::Query; -/// GraphiQL playground UI -// async fn graphiql_route() -> Result { -// juniper_actix::graphiql_handler("/graphql", None).await -// } - -// async fn playground_route() -> Result { -// juniper_actix::playground_handler("/graphql", None).await -// } - #[handler] async fn graphiql_route() -> Response { // Ok(HttpResponse::Ok() @@ -62,15 +53,6 @@ async fn playground_route() -> Response { .body(playground_source(GraphQLPlaygroundConfig::new("/graphql"))) } -// async fn graphql_route( -// req: actix_web::HttpRequest, -// payload: actix_web::web::Payload, -// data: web::Data, -// db: web::Data, -// ) -> Result { -// juniper_actix::graphql_handler(&data, &db, req, payload).await -// } - #[derive(Clone, Copy)] pub struct Key([u8; 1024]); @@ -195,6 +177,18 @@ async fn index() -> impl IntoResponse { .into_response() } +#[handler] +fn options() -> impl IntoResponse { + Response::builder() + .status(StatusCode::OK) + .header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") + .header( + "Access-Control-Allow-Headers", + "Content-Type, Authorization, Access-Control-Allow-Origin", + ) + .body("") +} + #[tokio::main] async fn main() -> anyhow::Result<()> { dotenv::dotenv().ok(); @@ -267,17 +261,20 @@ async fn main() -> anyhow::Result<()> { let app = Route::new() .at( "/graphql", - get(GraphQL::new(schema.clone())).post(GraphQL::new(schema)), + get(GraphQL::new(schema.clone())) + .post(GraphQL::new(schema)) + .options(options), ) .at("/graphiql", get(graphiql_route)) .at("/playground", get(playground_route)) - .at("/cdn/:slug@:version/:type", get(cdn::cdn_get)) - .at("/cdn/:slug@:version", get(cdn::cdn_get_typeless)) + .at("/cdn/:slug/:version/:type", get(cdn::cdn_get)) + .at("/cdn/:slug/:version", get(cdn::cdn_get_typeless)) .at("/mods", post(mods::upload_mod)) .at("/auth/github", post(users::user_auth)) .at("/me", get(users::get_me)) .at("/", get(index)) - .with(poem::middleware::Tracing); + .with(poem::middleware::Tracing) + .with(poem::middleware::SetHeader::new().appending("Access-Control-Allow-Origin", "*")); info!("starting HTTP server on port 8080"); info!("GraphiQL playground: http://localhost:8080/graphiql"); diff --git a/src/mods.rs b/src/mods.rs index 026257e..1e3d265 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -509,7 +509,7 @@ pub async fn _upload_mod(auth: &str, body: Vec) -> Response { // .id; let version = match sqlx::query!("INSERT INTO versions (mod_id, version, stats, artifact_hash, download_url) VALUES ($1, $2, $3, $4, $5) RETURNING id", db_mod,manifest.version.clone().to_string(), - version_stats, "", format!("{}/cdn/{}@{}", + version_stats, "", format!("{}/cdn/{}/{}", match std::env::var("BF_PUBLIC_URL") { Ok(url) => url, Err(e) => { @@ -834,25 +834,38 @@ pub async fn _upload_mod(auth: &str, body: Vec) -> Response { // .await // .unwrap() // .id; - let version = match sqlx::query!("INSERT INTO versions (mod_id, version, stats, artifact_hash, download_url) VALUES ($1, $2, $3, $4, $5) RETURNING id",db_mod,manifest.version.clone().to_string(),version_stats,"",format!("{}/cdn/{}@{}",match std::env::var("BF_PUBLIC_URL") { - Ok(url) => url, - Err(e) => { - warn!("{}", e); + let cdn_url = format!( + "{}/cdn/{}/{}", + match std::env::var("BF_PUBLIC_URL") { + Ok(url) => url, + Err(e) => { + error!("{}",e); - return Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body("Internal Server Error"); - } - },forgemod.manifest._id,manifest.version.clone().to_string())).fetch_one(&mut*trans).await { - Ok(record) => {record}, - Err(e) => { - warn!("{}", e); + return Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body("Internal Server Error"); + } + }, + forgemod.manifest._id, + manifest.version.clone() + ); - return Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body("Internal Server Error"); - }, - }.id; + let version = match sqlx::query!("INSERT INTO versions (mod_id, version, stats, artifact_hash, download_url) VALUES ($1, $2, $3, $4, $5) RETURNING id", + db_mod, + manifest.version.clone().to_string(), + version_stats, + "", + cdn_url, + ).fetch_one(&mut*trans).await { + Ok(record) => {record}, + Err(e) => { + warn!("{}", e); + + return Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body("Internal Server Error"); + }, + }.id; for v in &vers { // let _ = entity::version_beat_saber_versions::ActiveModel {