Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5/?] Add ability to run code examples in the playground: Add CORS exception for tutorial.ponylang.io to route /evaluate.json #212

Merged
merged 8 commits into from
Jun 30, 2024
11 changes: 10 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ use axum::{

use crate::routes::{compile, create_gist, evaluate, static_css, static_html, static_js};
use crate::GithubClient;
use http::HeaderValue;
shaedrich marked this conversation as resolved.
Show resolved Hide resolved
use std::net::SocketAddr;
use tower_http::cors::CorsLayer;

const layer: CorsLayer = CorsLayer::new().allow_origin(
shaedrich marked this conversation as resolved.
Show resolved Hide resolved
"https://tutorial.ponylang.io"
.parse::<HeaderValue>()
.unwrap(),
);

/// serve the api
pub async fn serve(addr: SocketAddr, github_client: GithubClient) -> Result<()> {
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
Loading