feat(api): add CORS middleware for browser fetch

This commit is contained in:
2026-04-08 02:23:37 +02:00
parent 0444d11fb4
commit 2483ed5a08

View File

@@ -1,8 +1,9 @@
mod routes; mod routes;
use axum::{routing::post, Router}; use axum::{Router, routing::post};
use routes::tabs::{parse_tab, AppState}; use routes::tabs::{AppState, parse_tab};
use std::sync::Arc; use std::sync::Arc;
use tower_http::cors::{Any, CorsLayer};
use ug_parser::{UgHtmlParser, UgTabFetcher}; use ug_parser::{UgHtmlParser, UgTabFetcher};
#[tokio::main] #[tokio::main]
@@ -14,11 +15,17 @@ async fn main() {
parser: Box::new(UgHtmlParser), parser: Box::new(UgHtmlParser),
}); });
let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any);
let app = Router::new() let app = Router::new()
.route("/tabs/parse", post(parse_tab)) .route("/tabs/parse", post(parse_tab))
.layer(cors)
.with_state(state); .with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
tracing::info!("listening on {}", listener.local_addr().unwrap()); tracing::info!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }