mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-04-17 18:47:39 -05:00
chg: Add correct CORS to routes
This commit is contained in:
parent
f283991740
commit
d7204d6622
3 changed files with 29 additions and 0 deletions
17
actix/Cargo.lock
generated
17
actix/Cargo.lock
generated
|
@ -19,6 +19,21 @@ dependencies = [
|
||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "actix-cors"
|
||||||
|
version = "0.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f9e772b3bcafe335042b5db010ab7c09013dad6eac4915c91d8d50902769f331"
|
||||||
|
dependencies = [
|
||||||
|
"actix-utils",
|
||||||
|
"actix-web",
|
||||||
|
"derive_more 0.99.18",
|
||||||
|
"futures-util",
|
||||||
|
"log",
|
||||||
|
"once_cell",
|
||||||
|
"smallvec",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "actix-files"
|
name = "actix-files"
|
||||||
version = "0.6.6"
|
version = "0.6.6"
|
||||||
|
@ -479,6 +494,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
name = "chhoto-url"
|
name = "chhoto-url"
|
||||||
version = "5.6.1"
|
version = "5.6.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"actix-cors",
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-session",
|
"actix-session",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
|
@ -738,6 +754,7 @@ dependencies = [
|
||||||
"futures-task",
|
"futures-task",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
|
"slab",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -29,6 +29,7 @@ categories = ["web-programming"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.5.1"
|
actix-web = "4.5.1"
|
||||||
actix-files = "0.6.5"
|
actix-files = "0.6.5"
|
||||||
|
actix-cors = "0.7.0"
|
||||||
rusqlite = { version = "0.32.0", features = ["bundled"] }
|
rusqlite = { version = "0.32.0", features = ["bundled"] }
|
||||||
regex = "1.10.3"
|
regex = "1.10.3"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
|
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
|
||||||
use actix_web::{cookie::Key, middleware, web, App, HttpServer};
|
use actix_web::{cookie::Key, middleware, web, App, HttpServer};
|
||||||
|
use actix_cors::Cors;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use std::{env, io::Result};
|
use std::{env, io::Result};
|
||||||
|
|
||||||
|
@ -40,6 +41,7 @@ async fn main() -> Result<()> {
|
||||||
.ok()
|
.ok()
|
||||||
.filter(|s| !s.trim().is_empty());
|
.filter(|s| !s.trim().is_empty());
|
||||||
|
|
||||||
|
|
||||||
// If an API key is set, check the security
|
// If an API key is set, check the security
|
||||||
if let Ok(key) = env::var("api_key") {
|
if let Ok(key) = env::var("api_key") {
|
||||||
if !auth::is_key_secure() {
|
if !auth::is_key_secure() {
|
||||||
|
@ -82,9 +84,18 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
// Actually start the server
|
// Actually start the server
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
|
// Define cors
|
||||||
|
let cors = Cors::default()
|
||||||
|
.allow_any_origin()
|
||||||
|
.supports_credentials()
|
||||||
|
.allowed_methods(vec!["GET", "POST", "DELETE"])
|
||||||
|
.allowed_header("X-API-Key")
|
||||||
|
.max_age(3600);
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.wrap(middleware::Compress::default())
|
.wrap(middleware::Compress::default())
|
||||||
|
.wrap(cors)
|
||||||
.wrap(
|
.wrap(
|
||||||
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
||||||
.cookie_same_site(actix_web::cookie::SameSite::Strict)
|
.cookie_same_site(actix_web::cookie::SameSite::Strict)
|
||||||
|
|
Loading…
Reference in a new issue