Skip to content

Commit

Permalink
feat(cors): support cors fairing
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Nov 20, 2024
1 parent 3be6c0b commit 406e199
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/cors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::{Header, Method, Status};
use rocket::{Request, Response};

pub struct CORS;

#[rocket::async_trait]
impl Fairing for CORS {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
kind: Kind::Response,
}
}

async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
if request.method() == Method::Options {
response.set_status(Status::NoContent);
response.set_header(Header::new(
"Access-Control-Allow-Methods",
"POST, PUT, GET, DELETE",
));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
}

response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ pub mod utils {
pub mod problem;
pub mod session;
}

pub mod routes {
pub mod account;
pub mod index;
}

pub mod cors;

pub use crate::routes::index::rocket;
11 changes: 7 additions & 4 deletions src/models/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use surrealdb::sql::Thing;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub enum Role {
#[default]
User,
SuperAdmin,
Admin,
#[default]
User,
Reserve,
}

Expand All @@ -21,10 +21,11 @@ pub struct Account {
pub signature: Option<String>,
pub links: Option<Vec<String>>,

pub name: Option<String>,
pub nickname: Option<String>,
pub sex: Option<bool>,
pub birthday: Option<chrono::NaiveDateTime>,

pub name: Option<String>,
pub student_id: Option<String>,
pub school: Option<String>,
pub college: Option<String>,
Expand All @@ -48,12 +49,14 @@ pub struct Profile {
pub links: Option<Vec<String>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub nickname: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sex: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub birthday: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub student_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 1 addition & 1 deletion src/routes/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct RegisterResponse {
pub token: String,
}

#[post("/register", data = "<register>")]
#[post("/create", data = "<register>")]
pub async fn register(
db: &State<Surreal<Client>>,
register: Json<RegisterData>,
Expand Down
3 changes: 2 additions & 1 deletion src/routes/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use crate::routes::account;
use crate::{cors::CORS, routes::account};
use anyhow::Result;
use rocket::fs::NamedFile;
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, Surreal};
Expand Down Expand Up @@ -31,6 +31,7 @@ pub async fn rocket() -> rocket::Rocket<rocket::Build> {
.expect("Failed to authenticate");

rocket::build()
.attach(CORS)
.mount("/", routes![index, files])
.mount("/account", account::routes())
.manage(db)
Expand Down
1 change: 1 addition & 0 deletions tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async fn test_register() -> Result<()> {
avatar: None,
signature: None,
links: None,
nickname: None,
name: Some("苏向夜".into()),
sex: None,
birthday: None,
Expand Down

0 comments on commit 406e199

Please sign in to comment.