Browse Source

main: add cors

feature/pictrs-v2.5
eiknat 2 years ago
parent
commit
6f2cca76d5
  1. 15
      server/Cargo.lock
  2. 1
      server/Cargo.toml
  3. 12
      server/src/main.rs

15
server/Cargo.lock

@ -88,6 +88,20 @@ dependencies = [
"webpki",
]
[[package]]
name = "actix-cors"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f3a3d5493dbc9b8769fe88c030d057ef8d2edc5728e5e26267780e8fc5db0be"
dependencies = [
"actix-web",
"derive_more",
"futures-util",
"log",
"once_cell",
"tinyvec 1.0.1",
]
[[package]]
name = "actix-files"
version = "0.3.0"
@ -1914,6 +1928,7 @@ dependencies = [
"activitystreams",
"activitystreams-ext",
"actix",
"actix-cors",
"actix-files",
"actix-rt",
"actix-web",

1
server/Cargo.toml

@ -25,6 +25,7 @@ lemmy_rate_limit = { path = "./lemmy_rate_limit" }
activitystreams = "0.7.0-alpha.3"
activitystreams-ext = "0.1.0-alpha.2"
actix = "0.10.0-alpha.2"
actix-cors = "0.5.3"
actix-files = "0.3.0-alpha.1"
actix-rt = "1.1.1"
actix-web = { version = "3.0.0-alpha.3", features = ["rustls"] }

12
server/src/main.rs

@ -31,6 +31,7 @@ use lemmy_utils::{settings::Settings, LemmyError, CACHE_CONTROL_REGEX};
use reqwest::Client;
use std::sync::Arc;
use tokio::sync::Mutex;
use actix_cors::Cors;
lazy_static! {
// static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 365 * 24 * 60 * 60);
@ -95,11 +96,20 @@ async fn main() -> Result<(), LemmyError> {
activity_queue.to_owned(),
cache_handler.clone(),
);
let cors = Cors::default()
.allowed_origin(&format!("http://{}", "localhost:4444"))
.allowed_origin(&format!("https://{}", Settings::get().hostname))
.allowed_methods(vec!["GET", "POST", "PUT"])
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
.allowed_header(http::header::CONTENT_TYPE)
.max_age(3600);
let settings = Settings::get();
let rate_limiter = rate_limiter.clone();
App::new()
.wrap_fn(add_cache_headers)
.wrap(middleware::DefaultHeaders::new().header("Access-Control-Allow-Origin", "*"))
.wrap(cors)
.wrap(middleware::Logger::default())
.data(context)
// The routes

Loading…
Cancel
Save