Add CORS and authorized origins to server

This commit is contained in:
2023-01-04 14:42:55 +01:00
parent 57bc676ec1
commit a2a2863d62
5 changed files with 75 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
use super::super::schema::{langandagents, languages};
use diesel::prelude::*;
use super::super::schema::{languages, langandagents};
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Release"]
@@ -7,7 +7,7 @@ pub enum Release {
Public,
NonCommercial,
Research,
Private
Private,
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
@@ -19,14 +19,14 @@ pub enum DictGenre {
Specialized,
Historical,
Orthography,
Terminology
Terminology,
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Agentlanguagerelation"]
pub enum AgentLanguageRelation {
Publisher,
Author
Author,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]

View File

@@ -1,11 +1,11 @@
use diesel::prelude::*;
use super::super::schema::{wordrelation, words};
use diesel::prelude::*;
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Wordrelationship"]
pub enum WordRelationship {
Definition,
Related
Related,
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
@@ -27,7 +27,7 @@ pub enum PartOfSpeech {
SubjConj,
Symbol,
Verb,
Other
Other,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]

View File

@@ -1,5 +1,58 @@
#![warn(clippy::style, clippy::pedantic)]
mod db;
fn main() {
println!("Hello, world!");
use std::{env, error::Error};
use dotenvy::dotenv;
use tracing::{debug, info, Level};
use tracing_subscriber::FmtSubscriber;
fn setup_logging() {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::DEBUG)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("Setting default subscriber failed");
}
fn make_cors() -> Result<rocket_cors::Cors, rocket_cors::Error> {
use rocket_cors::{AllowedHeaders, AllowedOrigins};
use std::str::FromStr;
rocket_cors::CorsOptions {
allowed_methods: ["Get", "Post"]
.iter()
.map(|s| FromStr::from_str(s).unwrap())
.collect(),
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
allowed_origins: match env::var("ORDABOK_HOSTS") {
Ok(val) => {
if val.is_empty() {
AllowedOrigins::all()
} else {
AllowedOrigins::some_exact(
val.split(',').collect::<Vec<_>>().as_ref(),
)
}
}
Err(_) => AllowedOrigins::all(),
},
..Default::default()
}
.to_cors()
}
#[rocket::main]
async fn main() -> Result<(), Box<dyn Error>> {
setup_logging();
info!("Reading environment variables");
dotenv().ok();
let cors = make_cors()?;
debug!("CORS: {:?}", cors);
#[allow(clippy::let_underscore_drop)]
let _ = rocket::build().attach(cors).launch().await?;
Ok(())
}