Skip to content

Commit

Permalink
[5/?] Add ability to run code examples in the playground: Add CORS ex…
Browse files Browse the repository at this point in the history
…ception for tutorial.ponylang.io to route /evaluate.json (#212)

* Add CORS exception for tutorial.ponylang.io to rout /evaluate.json

* Change let to const

Co-authored-by: Joe Eli McIlvain <[email protected]>

* Add a type to layer

* Formatting

* Fix HeaderValue import

Co-authored-by: Matthias Wahl <[email protected]>

* Add dependency

Co-authored-by: Matthias Wahl <[email protected]>

As per #212 (review)

* Change const to let

Co-authored-by: Matthias Wahl <[email protected]>

As per #212 (review)

* Move variable inside serve function

Co-authored-by: Matthias Wahl <[email protected]>

As per #212 (comment)

---------

Co-authored-by: Joe Eli McIlvain <[email protected]>
Co-authored-by: Matthias Wahl <[email protected]>
  • Loading branch information
3 people authored Jun 30, 2024
1 parent 3c40027 commit 2297589
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde_json = "1.0"
tokio = { version = "1.38", features = ["full"] }
wait-timeout = "0.2"
url = { version = "2.5", "features" = ["serde"] }
tower-http = { version = "0.5", features = ["cors"] }

[dev-dependencies]
anyhow = "1.0"
Expand Down
11 changes: 10 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ use axum::{

use crate::routes::{compile, create_gist, evaluate, static_css, static_html, static_js};
use crate::GithubClient;
use axum::http::HeaderValue;
use std::net::SocketAddr;
use tower_http::cors::CorsLayer;

/// 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::<HeaderValue>()
.unwrap(),
);

let static_routes = Router::new()
.route(
"/web.css",
Expand All @@ -24,11 +32,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)
Expand Down

0 comments on commit 2297589

Please sign in to comment.