From ea47808d6b22f7a295bd0540d0f59f7eaf702a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Tue, 30 Apr 2024 10:36:18 +0200 Subject: [PATCH 1/8] Add CORS exception for tutorial.ponylang.io to rout /evaluate.json --- src/api.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index 6c28cf3..eca172e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -7,6 +7,12 @@ use axum::{ use crate::routes::{compile, create_gist, evaluate, static_css, static_html, static_js}; use crate::GithubClient; use std::net::SocketAddr; +use http::HeaderValue; +use tower_http::cors::CorsLayer; + +let layer = CorsLayer::new().allow_origin( + "https://tutorial.ponylang.io".parse::().unwrap(), +); /// serve the api pub async fn serve(addr: SocketAddr, github_client: GithubClient) -> Result<()> { @@ -24,11 +30,12 @@ pub async fn serve(addr: SocketAddr, github_client: GithubClient) -> Result<()> get(|| async { static_js(include_bytes!("../static/mode-pony.js")) }), ); let router = Router::new() + .route("/evaluate.json", post(evaluate)) + .layer(layer) // applies to every route() call before on `router` .route( "/", get(|| async { static_html(include_bytes!("../static/web.html")) }), ) - .route("/evaluate.json", post(evaluate)) .route("/compile.json", post(compile)) .route("/gist.json", post(create_gist)) .with_state(github_client) From 570f0b513f3d47dbdd8d2735a5ba69f881caee2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 25 May 2024 13:30:30 +0200 Subject: [PATCH 2/8] Change let to const Co-authored-by: Joe Eli McIlvain --- src/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index eca172e..e5eb3f4 100644 --- a/src/api.rs +++ b/src/api.rs @@ -10,7 +10,7 @@ use std::net::SocketAddr; use http::HeaderValue; use tower_http::cors::CorsLayer; -let layer = CorsLayer::new().allow_origin( +const layer = CorsLayer::new().allow_origin( "https://tutorial.ponylang.io".parse::().unwrap(), ); From e34dff7d4973f1c9ee9d05a7ec28baf9102923b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Fri, 31 May 2024 20:27:15 +0200 Subject: [PATCH 3/8] Add a type to layer --- src/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index e5eb3f4..c11ae47 100644 --- a/src/api.rs +++ b/src/api.rs @@ -10,7 +10,7 @@ use std::net::SocketAddr; use http::HeaderValue; use tower_http::cors::CorsLayer; -const layer = CorsLayer::new().allow_origin( +const layer: CorsLayer = CorsLayer::new().allow_origin( "https://tutorial.ponylang.io".parse::().unwrap(), ); From 7b0af26785a4d872b4a5a9f73d86dfef20d90d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Wed, 12 Jun 2024 05:32:17 +0200 Subject: [PATCH 4/8] Formatting --- src/api.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/api.rs b/src/api.rs index c11ae47..5bae6ac 100644 --- a/src/api.rs +++ b/src/api.rs @@ -6,12 +6,14 @@ use axum::{ use crate::routes::{compile, create_gist, evaluate, static_css, static_html, static_js}; use crate::GithubClient; -use std::net::SocketAddr; use http::HeaderValue; +use std::net::SocketAddr; use tower_http::cors::CorsLayer; const layer: CorsLayer = CorsLayer::new().allow_origin( - "https://tutorial.ponylang.io".parse::().unwrap(), + "https://tutorial.ponylang.io" + .parse::() + .unwrap(), ); /// serve the api From 4d8e628ab58e7b99f2c4f9b206e2009c84df58e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:11:43 +0200 Subject: [PATCH 5/8] Fix HeaderValue import Co-authored-by: Matthias Wahl --- src/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index 5bae6ac..3bcd2d5 100644 --- a/src/api.rs +++ b/src/api.rs @@ -6,7 +6,7 @@ use axum::{ use crate::routes::{compile, create_gist, evaluate, static_css, static_html, static_js}; use crate::GithubClient; -use http::HeaderValue; +use axum::http::HeaderValue; use std::net::SocketAddr; use tower_http::cors::CorsLayer; From 905c4af83076fac7c3bf03f2c478115a5dffe3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:20:25 +0200 Subject: [PATCH 6/8] Add dependency Co-authored-by: Matthias Wahl As per https://github.com/ponylang/pony-playground/pull/212#pullrequestreview-2141020901 --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index c6c0016..2b1c691 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ serde_json = "1.0" tokio = { version = "1.37", features = ["full"] } wait-timeout = "0.2" url = { version = "2.5", "features" = ["serde"] } +tower-http = { version = "0.5", features = ["cors"] } [dev-dependencies] anyhow = "1.0" From 6f72699588def1bae899548226950cdd23b8745c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:21:14 +0200 Subject: [PATCH 7/8] Change const to let Co-authored-by: Matthias Wahl As per https://github.com/ponylang/pony-playground/pull/212#pullrequestreview-2141020901 --- src/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index 3bcd2d5..925b74e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -10,7 +10,7 @@ use axum::http::HeaderValue; use std::net::SocketAddr; use tower_http::cors::CorsLayer; -const layer: CorsLayer = CorsLayer::new().allow_origin( +let layer: CorsLayer = CorsLayer::new().allow_origin( "https://tutorial.ponylang.io" .parse::() .unwrap(), From 89f40defae8438cf07f4629a68575704eefea97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:26:02 +0200 Subject: [PATCH 8/8] Move variable inside serve function Co-authored-by: Matthias Wahl As per https://github.com/ponylang/pony-playground/pull/212#discussion_r1654319376 --- src/api.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/api.rs b/src/api.rs index 925b74e..672136b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -10,14 +10,14 @@ use axum::http::HeaderValue; use std::net::SocketAddr; use tower_http::cors::CorsLayer; -let layer: CorsLayer = CorsLayer::new().allow_origin( - "https://tutorial.ponylang.io" - .parse::() - .unwrap(), -); - /// serve the api pub async fn serve(addr: SocketAddr, github_client: GithubClient) -> Result<()> { + let layer: CorsLayer = CorsLayer::new().allow_origin( + "https://tutorial.ponylang.io" + .parse::() + .unwrap(), + ); + let static_routes = Router::new() .route( "/web.css",