Skip to content

Commit

Permalink
fix: cors
Browse files Browse the repository at this point in the history
  • Loading branch information
sargon64 committed Jan 14, 2024
1 parent f4a4a79 commit cfe93f4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
2 changes: 0 additions & 2 deletions src/cdn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ async fn cdn_handler(
// #[get("/cdn/{slug}@{version}/{type}")]
#[handler]
pub async fn cdn_get(
// db: web::Data<Database>,
// path: web::Path<(String, String, CdnType)>,
Path((slug, version, dl_type)): Path<(String, String, CdnType)>,
) -> impl IntoResponse {
let db = match DB_POOL.get() {
Expand Down
45 changes: 21 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,15 +36,6 @@ mod debug;

use crate::schema::Query;

/// GraphiQL playground UI
// async fn graphiql_route() -> Result<HttpResponse, Error> {
// juniper_actix::graphiql_handler("/graphql", None).await
// }

// async fn playground_route() -> Result<HttpResponse, Error> {
// juniper_actix::playground_handler("/graphql", None).await
// }

#[handler]
async fn graphiql_route() -> Response {
// Ok(HttpResponse::Ok()
Expand All @@ -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<Schema>,
// db: web::Data<Database>,
// ) -> Result<HttpResponse, Error> {
// juniper_actix::graphql_handler(&data, &db, req, payload).await
// }

#[derive(Clone, Copy)]
pub struct Key([u8; 1024]);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
49 changes: 31 additions & 18 deletions src/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ pub async fn _upload_mod(auth: &str, body: Vec<u8>) -> 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) => {
Expand Down Expand Up @@ -834,25 +834,38 @@ pub async fn _upload_mod(auth: &str, body: Vec<u8>) -> 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 {
Expand Down

0 comments on commit cfe93f4

Please sign in to comment.