Add CORS support for debug mode. (#2884)

This commit is contained in:
Dessalines 2023-05-29 17:14:00 -04:00 committed by GitHub
parent d30839bea1
commit af3eb6e27b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

16
Cargo.lock generated
View file

@ -102,6 +102,21 @@ dependencies = [
"tokio-util 0.7.4", "tokio-util 0.7.4",
] ]
[[package]]
name = "actix-cors"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b340e9cfa5b08690aae90fb61beb44e9b06f44fe3d0f93781aaa58cfba86245e"
dependencies = [
"actix-utils",
"actix-web",
"derive_more",
"futures-util",
"log",
"once_cell",
"smallvec",
]
[[package]] [[package]]
name = "actix-form-data" name = "actix-form-data"
version = "0.7.0-beta.0" version = "0.7.0-beta.0"
@ -2732,6 +2747,7 @@ version = "0.17.1"
dependencies = [ dependencies = [
"activitypub_federation", "activitypub_federation",
"actix", "actix",
"actix-cors",
"actix-web", "actix-web",
"clokwerk", "clokwerk",
"console-subscriber", "console-subscriber",

View file

@ -141,3 +141,4 @@ console-subscriber = { version = "0.1.8", optional = true }
opentelemetry-otlp = { version = "0.10.0", optional = true } opentelemetry-otlp = { version = "0.10.0", optional = true }
pict-rs = { version = "0.4.0-beta.9", optional = true } pict-rs = { version = "0.4.0-beta.9", optional = true }
tokio.workspace = true tokio.workspace = true
actix-cors = "0.6.4"

View file

@ -8,6 +8,7 @@ pub mod telemetry;
use crate::{code_migrations::run_advanced_migrations, root_span_builder::QuieterRootSpanBuilder}; use crate::{code_migrations::run_advanced_migrations, root_span_builder::QuieterRootSpanBuilder};
use activitypub_federation::config::{FederationConfig, FederationMiddleware}; use activitypub_federation::config::{FederationConfig, FederationMiddleware};
use actix::Actor; use actix::Actor;
use actix_cors::Cors;
use actix_web::{middleware, web::Data, App, HttpServer, Result}; use actix_web::{middleware, web::Data, App, HttpServer, Result};
use doku::json::{AutoComments, CommentsStyle, Formatting, ObjectsStyle}; use doku::json::{AutoComments, CommentsStyle, Formatting, ObjectsStyle};
use lemmy_api_common::{ use lemmy_api_common::{
@ -150,8 +151,15 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {
.build() .build()
.expect("configure federation"); .expect("configure federation");
let cors_config = if cfg!(debug_assertions) {
Cors::permissive()
} else {
Cors::default()
};
App::new() App::new()
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.wrap(cors_config)
.wrap(TracingLogger::<QuieterRootSpanBuilder>::new()) .wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
.app_data(Data::new(context)) .app_data(Data::new(context))
.app_data(Data::new(rate_limit_cell.clone())) .app_data(Data::new(rate_limit_cell.clone()))